简体   繁体   English

骨干获取多个模型

[英]Backbone fetching multiple models

I have been working with backbone a bit and I'm quickly finding out what I'm doing has become too tedious and repetitive. 我一直在使用骨干网,并且很快发现自己在做的事情变得过于繁琐和重复。

I will have multiple backbone views and each of those views will have a model that makes a call out to an api for data. 我将有多个主干视图,并且每个视图都将具有一个模型,该模型可以调用api来获取数据。 Once that is complete I want to render the results. 完成后,我要呈现结果。 Easy enough, however, I have to attach to each of these models data from another source (a messaging system) 但是,很容易,我必须将来自另一个源(消息传递系统)的数据附加到每个模型中

//this corresponds to the view
$.when(this.model.fetch(), this.msg.fetch()).done((result1, result2) => {
    this.model.set('msg', this.msgtoJSON(), { silent: true });
    this.renderTemplate(template, this.model.toJSON());
});

This works, but it's repeated, everywhere. 这可行,但是到处都是重复的。 I have dozens and dozens of views with this. 我对此有数十种意见。 This also prevents me from attaching object listeners for change events on the main model so that the template can be rendered anew. 这也阻止了我将对象侦听器附加到主模型上的更改事件,以便可以重新呈现模板。

Does anyone have any suggestions as to how I could asynchronously have this messaging dependency fetched and injected into my backbone models when those models are fetched? 有人对我如何异步获取此消息传递依赖项并将其注入到我的主干模型中提出任何建议吗?

You can make them dependent, merging the fetch of both models. 您可以使它们相互依赖,合并两个模型的fetch It's will break a bit native implementation of Backbone.Model.fetch , but will help you to avoid code repetition. 它会破坏Backbone.Model.fetch本机实现,但可以帮助您避免代码重复。 Kind of denormalize to normalize. 归一化到归一化的一种。

var Slave = Backbone.Model.extend({
     urlRoot: 'first/api',
     defaults: {
         val1: '',
         val2: ''
     }
});  

var Master = Backbone.Model.extend({
    initialize: function(options) {
        this.slave = options.slave;
    }, 
    urlRoot: 'second/api', 
    fetch: function(options) {
        var that = this,
            masterPromise = this.constructor.__super__.fetch.call(this, options);

        if (this.slave) {
            return $.when(masterPromise, this.slave.fetch()).done(function(result1, result2){ //merged promise
                that.set('msg', that.slave.toJSON(), { silent: true });                 
            });
        }
        return masterPromise;
    }
});

And then: 接着:

var slave  = new Slave(),
    master = new Master({ slave: slave });

master.fetch().done(function(){
   // render template here.
})

This implementation will work for both cases, with slave model or without. 无论有没有从属模型,此实现都将适用于两种情况。

A few things to note 注意事项

1.If you handle success callback to render the template like: 1.如果您处理成功回调以呈现模板,如下所示:

master.fetch({success: function() { // rendering here}});

in this case 'msg' will be undefined, cause success will not wait for slave's promise. 在这种情况下, 'msg'将是不确定的,因为success不会等待从属的承诺。 You need to avoid use of success . 您需要避免使用success

2.If one of the model's fetch will fail, and you will set the fail callback on merged promise it will fire whenever one of the model's fetch has failed. 2.如果模型的获取之一将失败,并且您将fail回调设置为合并的promise,则它将在模型的获取之一失败时触发。 If you need to do some staff individually for each model's fail case you need to attach them to the model's fetch before merged promise. 如果您需要为每个模型的失败案例单独安排一些人员,则需要在合并诺言之前将他们附加到模型的提取中。

PS. PS。 I haven't tested this code, so please let me know if something fails. 我尚未测试此代码,因此如果出现故障,请告知我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM