简体   繁体   English

Backbone.js:仅从JSON集合中获取新模型

[英]Backbone.js: Adding just the new models from JSON collection fetch

I have a webapp based on Backbone.js with a list. 我有一个基于Backbone.js的Web应用程序,带有一个列表。 The entries of the list are coming from a REST API. 列表的条目来自REST API。 This list (JSON array) updates from time to time. 此列表(JSON数组)会不时更新。 I want to update my list in the frontend too, without reloading the page. 我也想在前端更新我的列表,而无需重新加载页面。

I thought about using a poller to update the file list with every new object returned by the API. 我考虑过使用轮询器通过API返回的每个新对象来更新文件列表。 However, the poller is not the problem here, I first need a function to add just the new models to the file list. 但是,轮询器不是这里的问题,我首先需要一个函数,仅将模型添加到文件列表中。

The API returns a JSON list, based on this model: API根据以下模型返回JSON列表:

Xff = Backbone.Model.extend({
        defaults: {
                id: null,
                name: "",
                language: "en",
                timestamp: 0,
                status: null,
                progress: 10,
                duration: 0
        }
});

This is the collection. 这是集合。 restUri points to the REST API and with /files it gets the complete file list. restUri指向REST API,并使用/ files获取完整的文件列表。

XffCollection = Backbone.Collection.extend({
        model: Xff,
        comparator: function(a, b) {
             return (a.get("timestamp") > b.get("timestamp") ? -1 : 1);
        },
        url: restUri + "files"
});

This is the AppView object. 这是AppView对象。 It uses the XffCollection, as explained above. 如上所述,它使用XffCollection。

app = new AppView({
        collection: new XffCollection()
});

AppView is a regular backbone view... AppView是常规的主干视图...

AppView = Backbone.View.extend({ .... })

Using app.collection.fetch() I can fire the request (visible in firebug), but the list is not updated. 使用app.collection.fetch()我可以触发请求(在Firebug中可见),但列表不会更新。 I also have a addAll() function, but then it just appends the new file list to the old file list. 我也有一个addAll()函数,但是它只是将新文件列表追加到旧文件列表中。

The addAll: addAll:

addAll: function() {
  this.collection.chain().sort().each(this.addOne, this);
}

This is the addOne: 这是addOne:

addOne: function(xff) {
  var v = new XffView({model: xff});
  this.xffViews.push(v);
  $("#xffs").append(v.render().el);
}

How can I add just the new entries to the list? 如何仅将新条目添加到列表中?

UPDATE UPDATE

While kindasimples anwser works now, the filelist in the frontend is not sorted anymore using the comparator defined in the collection (with the timestamp). 虽然kindasimples anwser现在可以使用,但前端的文件列表不再使用集合中定义的比较器(带有时间戳)进行排序。 Using addAll() in the bottom of the comparator, the list is sorted. 使用比较器底部的addAll()对列表进行排序。

To provide additional information, here are more parts of the overall backbone code: http://pastebin.com/rR39x3Y1 为了提供更多信息,这里是整体骨干网代码的更多部分: http : //pastebin.com/rR39x3Y1

From the backbone.js docs: 从ribs.js文档中:

collection.sort([options]) Force a collection to re-sort itself. collection.sort([options])强制集合重新排序。 You don't need to call this under normal circumstances, as a collection with a comparator will sort itself whenever a model is added. 通常情况下,您无需调用此函数,因为只要添加模型,带有比较器的集合便会对其进行排序。 To disable sorting when adding a model, pass {sort: false} to add. 要在添加模型时禁用排序,请传递{sort:false}以添加。 Calling sort triggers a "sort" event on the collection. 调用sort会触发集合上的“ sort”事件。

But it does not sort itself. 但是它本身不会排序。 Also calling app.collection.sort() right after the fetch does not help. 在获取后立即调用app.collection.sort()也无济于事。

UPDATE 2 更新2

I fixed it by sorting the array in the API before returning it. 我通过在返回之前对API中的数组进行排序来修复它。 That's not how it was meant to be but at least it works now. 不是应该的样子,但至少现在可以了。

You have the right idea. 你有正确的主意。 addOne() will render individual items when you do your initial setup after a fetch to populate items. 在获取填充项后进行初始设置时,addOne()将呈现单个项。 You can add a listener to the collection events to add the new items. 您可以将侦听器添加到收集事件以添加新项目。 Collection.Fetch does what you want by adding new models to the collection and leaving the old in tact (as long as you don't pass the {reset:true} flag as a parameter) Collection.Fetch通过向集合中添加新模型并保持原有模型不变(只要您不将{reset:true}标志作为参数传递)来完成您想要的操作

So, on your view add the listener to the initialize hook 因此,在您的视图上,将侦听器添加到初始化挂钩

initialize: function() {
    this.listenTo(this.collection, "add", this.addOne)
}

You will probably want to define the idAttribute on your Xff Model so that backbone can identify new items properly. 您可能需要在Xff模型上定义idAttribute,以便主干网可以正确识别新项目。

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

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