简体   繁体   English

Backbone.js-一个视图有多个模型

[英]Backbone.js - multiple models for one view

I have a template that needs to receive data from two different API endpoints (URLs): cart and user . 我有一个模板,该模板需要从两个不同的API端点(URL)接收数据: cartuser

I want the two endpoints to act as one model or collection so that I can do something like .changedAttributes() or sync , or fetch . 我希望两个端点充当一个模型或集合,以便我可以执行.changedAttributes()syncfetch

I know Backbone is very permissive, but I am really lost. 我知道Backbone非常宽容,但我真的迷路了。


Playground: 操场:

I've created a codepen to see what I've done so far: http://codepen.io/anything/pen/AXoBoa 我已经创建了一个Codepen来查看到目前为止我所做的事情: http ://codepen.io/anything/pen/AXoBoa


Desired result should be something like: 所需的结果应类似于:

initialize: function(){
  var self = this;
  collection.fetch({
  success: function(data){
      self.collection = data;
    }
  })
},

render: function(){
    var self = this;
    var source = $("#template").html();
    var template = Handlebars.compile(source);
    var htmlToRender = template(self.collection.toJSON());
}  

You could create an event concentrator listening to its registered objects and retrigger the events you catch. 您可以创建一个事件集中器来监听其注册对象,然后重新触发捕获的事件。

Something like 就像是

var aggregate = _.extend({}, Backbone.Events);
aggregate.register = function(m) {
    var self = this;

    this.listenTo(m, 'all', function() {
        this.trigger.apply(this, arguments);
    });
};

You would then use it like this 然后,您将像这样使用它

aggregate.on('change', function(m) {
    // do what you have to do when one of the models change
    console.log('Change on ', m.toJSON());
});
aggregate.on('sync', function(m) {
    //same thing for syncs
    console.log('sync ', m.toJSON());
});

var m1 = new Backbone.Model({id: 1});
var m2 = new Backbone.Model({id: 2});
aggregate.register(m1);
aggregate.register(m2);

m1.fetch();
m2.set({data: 2});

And a demo http://jsfiddle.net/nikoshr/hm0xc79z/ 和演示http://jsfiddle.net/nikoshr/hm0xc79z/

A slightly different approach to nikoshr based on http://backbonejs.org/#Events . 基于http://backbonejs.org/#Events的 nikoshr的方法略有不同。 The basic idea is that you set up an event object that can be referenced in both views. 基本思想是设置一个可以在两个视图中引用的事件对象。 You can name the events whatever you want and they can triggered and listened to wherever the object is available. 您可以根据需要命名事件,事件可以在对象可用的任何地方触发和监听。

  1. Create event object. 创建事件对象。 In your case, add it to your main appshell object. 在您的情况下,将其添加到主appshell对象中。

     appShell.Events = _.extend({}, Backbone.Events); 
  2. When user performs action in user view like logout, trigger an event. 当用户在用户视图中执行操作(例如注销)时,触发事件。

     appShell.Events.trigger('user:logout'); 
  3. Listen to the event in another view and perform action based off of it. 在另一个视图中收听事件,然后根据事件执行操作。

     this.listenTo(appShell.Events, 'user:logout', this.doSomething); 
  4. Put logic in the doSomething to do whatever you need to do on the other view. 将逻辑放在doSomething中,以执行您需要在其他视图上执行的所有操作。

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

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