骨干视图呈现与多模型提取 [英] Backbone view render with multiple model fetch

查看:141
本文介绍了骨干视图呈现与多模型提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要花费1个模型和1集合在我的html模板与骨干。但有时候,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.

提前感谢

推荐答案

您可以将抓取返回的请求的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆