集合中的模型Backbone.js的更新 [英] Backbone.js updating of models in a collection

查看:108
本文介绍了集合中的模型Backbone.js的更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你正在建设一个Twitter克隆与Backbone.js的。你有微博的集合。每个鸣叫显然鸣叫模型的一个实例。

Let's say you are building a Twitter clone with Backbone.js. You have a collection of tweets. Each tweet is obviously an instance of the Tweet model.

您创建集合的一个实例,获取最新的鸣叫10,使它们添加到DOM。

You create an instance of the collection, fetch the latest 10 tweets, render them and add to the DOM.

到目前为止好。

如果你想使到服务器,几分钟后一个电话,看看是否有新的鸣叫来了吗?你怎么能新到的鸣叫添加到收藏?

What if you wanted to make a call to the server a few minutes later to see if any new tweets arrived? How can you add the newly arrived tweets to the collection?

如果您使用取()方法,你打相同的URL所有的时间。没关系。有一个聪明的办法,我可以用骨干/下划线来过滤那些并添加不属于集合集合中的鸣叫?

If you use the fetch() method, you are hitting the same URL all the time. That's fine. Is there a clever way that I can use Backbone/Underscore to filter those and add the tweets that aren't in the collection to the collection?

推荐答案

让我们假设你的鸣叫每个人都有一个唯一的标识符(如果没有,你应该创建一个)。

Lets assume that every one of your tweets has a unique identifier(if not, you should probably create one).

您可以在这样的结构后端走在默认情况下它可以让你10最新的鸣叫如果你调用 http://your.site.com /鸣叫的不带任何参数。

You can structure your backend in such away that by default it gets you 10 latest tweets if you call http://your.site.com/tweets without any arguments.

如果你打电话但<一个href=\"http://your.site.com/tweets?last_tweet_id=BLAblaBlA\">http://your.site.com/tweets?last_tweet_id=BLAblaBlA,它会给你所指定的last_tweet_id之后来到10最新的鸣叫。

If you however call http://your.site.com/tweets?last_tweet_id=BLAblaBlA, it will give you 10 latest tweets that came after the last_tweet_id that you specified.

您可以覆盖code,通过实施YourCollection.sync方法从后端数据进入你的收藏。

You can override the code that gets the data from the backend into your Collection by implementing YourCollection.sync method.

原因:Backbone.Collection首先尝试调用Collection.sync,如果它没有实现,它调用Backbone.sync功能,因此,如果您实现YourCollection.sync,它会被使用。下面是从Backbone.Collection.fetch功能的片段:

Reason: Backbone.Collection first tries to call Collection.sync and if its not implemented, it calls Backbone.sync function, so if you implement YourCollection.sync, it will be used. Here is the snippet from Backbone.Collection.fetch function:

(this.sync || Backbone.sync)('read', this, success, error);

让你同步将类似于

so your sync would be something like

var TweetCollection = Backbone.Collection.extend({
  model: TweetModel,
  sync: function(method, collection, success, error) {
    var requestData={};
    if(collection.length>0) {
        requestData.last_tweet_id=collection.last.id 
    }
    var params = {
        url:          "/tweet",
        type:         "POST",
        data:         requestData,
        success:      success,
        error:        error
    };
    $.ajax(params);
  }
}

您将不得不重写您的收藏解析功能,以确保响应被附加到模型的现有阵列。

You would have to override your collections parse function to make sure that the response is appended to the existing array of models.

这篇关于集合中的模型Backbone.js的更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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