具有多个模型获取的主干视图渲染 [英] Backbone view render with multiple model fetch
本文介绍了具有多个模型获取的主干视图渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在我的带有主干的 html 模板中花费 1 个模型和 1 个集合.但有时,html 在模型之后就准备好了.我有:
I need to spend 1 model and 1 collection in my html template with backbone. But sometimes, html is ready after the model. I have :
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 集合.我想确保我的模型和集合在模板之前加载.
Here, only listeClient collection is loaded. I want to be sure my model and collection are loaded before the template.
提前谢谢你
推荐答案
你可以将fetch返回的requests'promise与jquery.when
来同步你的渲染任务.比如:
You can combine the requests' promises returned by fetch with jquery.when
to synchronize your rendering task. Something like:
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;
}
这篇关于具有多个模型获取的主干视图渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文