骨干视图呈现多个模型获取

我需要在带有骨干的html模板中花费1个模型和1个集合。 但有时候,html在模型之后就绪了。 我有 :

var FormUtilisateurView = Backbone.View.extend({ initialize: function(id){ this.listeClient = new ClientsCollection(); this.utilisateur = new UtilisateurModel({ id : id }); }, render: function(){ var that = this; this.utilisateur.fetch(); this.listeClient.fetch().done(function(){ that.$el.html(FormTemplate({ clients : that.listeClient.models, user : that.utilisateur })); }); return this; } }); 

这里只加载了listeClient集合。 我想确保我的模型和集合在模板之前加载。

先感谢您

您可以将fetch返回的请求的promises与jquery.when结合使用,以同步渲染任务。 就像是:

 render: function(){ var that = this, promises = [], user = this.utilisateur, clients = this.listeClient; promises.push(user.fetch()); promises.push(clients.fetch()); $.when.apply(null, promises).done(function(){ that.$el.html(FormTemplate({clients: clients.models, user: user})); }); return this; }