【问题标题】:Backbone view render with multiple model fetch具有多个模型提取的主干视图渲染
【发布时间】:2014-11-27 17:28:10
【问题描述】:

我需要在我的带有主干的 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 集合。 我想确保我的模型和集合在模板之前加载。

提前谢谢你

【问题讨论】:

    标签: jquery backbone.js collections model fetch


    【解决方案1】:

    您可以将 fetch 返回的请求承诺与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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2014-09-18
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多