我需要在带有主干的 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 返回的请求 promise 与 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;
}
关于jquery - 使用多个模型获取进行主干 View 渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27176171/