简体   繁体   English

Backbone.js多个视图,一个集合,一个获取

[英]Backbone.js multiple views, one collection, one fetch

I am trying to generate multiple views using one collection and one fetch every 5 seconds. 我试图使用一个集合生成一个视图,每5秒获取一个。

Below is a working example, but both views are refreshed when fetched. 以下是一个有效的示例,但是在获取两个视图时都会刷新它们。 I could splice the response into multiple urls, but i want to minimize the aumount of requests. 我可以将响应拼接为多个URL,但是我想最大程度地减少请求的数量。

My current problem is that i dont want all views to re-render every 5 seconds when the collection is re-fetched, only the associated view that changed. 我当前的问题是,我不希望在重新获取集合时每5秒重新渲染所有视图,而只更改相关联的视图。 I have tried creating multiple models inside the collection and adding the correct object in the parse function without any luck. 我尝试在集合内创建多个模型,并在没有任何运气的情况下在parse函数中添加正确的对象。

Response: 响应:

{
  "json-1": {
    "sub_1": "3",
    "sub_2": [],
  },
  "json-2": {
    "sub_1": [],
    "sub_2": "1",
  },
}

// Client //客户

const APICollection = Backbone.Collection.extend({
    initialize: (models, options) => {
        this.id = options.id;
    },
    url: () => {
        return 'https://url.url/' + this.id;
    },
    model: APIModel,
        parse: (resp) => {
        return resp;
    },
});

const ViewOne = Backbone.View.extend({
    initialize: function () {
        this.collection.bind('sync', this.render, this);
        this.update();
        _.bindAll(this, 'update');
    },
    render: function (n, collection) {
        // Render view
    },
    update: function () {
        let self = this;
        this.collection.fetch({
            update: true, remove: false, success: function () {
                setTimeout(self.update, 5000);
            }
        });
    }
});

// Also updates when re-fetched
const ViewTwo = Backbone.View.extend({
    initialize: function () {
        this.collection.bind('sync', this.render, this);
    },
    render: function (n, collection) {
        // Render function
    }
});

let col = APICollection([], {id: 'someid'});
new ViewOne({collection: col, el: $("#one")});
new ViewTwo({collection: col, el: $("#two")});

**Update **更新

To clarify: "only the associated view that changed". 澄清一下:“仅更改了关联的视图”。 By this i mean that 'ViewOne' should only be re-rendered when 'json-1' has changed, and 'ViewTwo' shouldn't re-render. 我的意思是,仅当更改“ json-1”时才应重新渲染“ ViewOne”,而不应重新渲染“ ViewTwo”。 currently the full response is sent to both views. 目前,完整回复已发送到两个视图。

When dealing with an API which returns an Object, not an array of Objects, the best approach is to use a Backbone.Model directly. 处理返回一个Object而不是Objects数组的API时,最好的方法是直接使用Backbone.Model

update: function () {
    let self = this;
    this.model.fetch({
        update: true, remove: false, success: function () {
            setTimeout(self.update, 5000);
        }
    });
}

The model is still fetched the same way as the collection, but the Views can listen to specific attributes on the model, instead of: 仍以与集合相同的方式获取模型,但视图可以侦听模型上的特定属性,而不是:

this.collection.bind('sync', this.render, this);

The following can be used: 可以使用以下内容:

this.model.bind('change:json-1', this.render, this);

Tip: Better to listenTo rather than bind, it is safer (see docs ) 提示:最好使用listenTo而不是bind,它更安全(请参阅docs

this.listenTo(this.model, 'change:json-1', this.render);

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

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