简体   繁体   English

backbone.js集合获取变量

[英]backbone.js collection get vars

This seems like an obvious one, but I'm somehow missing it... 这看起来很明显,但我不知何故错过了......

How do you send options along with a backbone.js collection fetch()? 你如何发送选项以及backbone.js集合fetch()?

Or, from a broader point of view: I have a large dataset on the server, messages in this case, that I want to make accessible through a collection. 或者,从更广泛的角度来看:我在服务器上有一个大型数据集,在这种情况下是消息,我想通过集合访问它。 Seeing as there could be thousands of messages I don't want to simply fetch and store all of them at once, so my collection would have to at least understand limits and offsets. 看到可能有数以千计的消息我不想简单地一次获取和存储所有消息,因此我的集合必须至少理解限制和偏移。 Not to mention querying filtered or sorted lists. 更不用说查询过滤或排序列表了。

Is a backbone collection even the way to handle this? 骨干系列甚至可以处理这个问题吗?

Cheers 干杯

I've been messing with Backbone for a couple days now and I had to deal with this problem almost immediately and I looked at this solution, but I found it clunky. 我一直在乱用Backbone几天,我不得不立即解决这个问题而且我看了这个解决方案,但我发现它很笨重。 After reading some more backbone documentation I discovered you can actually override any of the jQuery.ajax options within the fetch() method. 在阅读了一些更多的主干文档后,我发现你实际上可以覆盖fetch()方法中的任何jQuery.ajax选项。 So for example 所以举个例子

Posts = Backbone.Collections.extend({
  model: Post,
  url: '/posts'
});

Now when you want to call fetch just send it whatever parameters you wish. 现在,当你想调用fetch时,只需发送你想要的任何参数即可。 eg 例如

var posts = new Posts();
posts.fetch({ data: { page: 3, pagesize: 10, sort: 'asc' } });

This will produce the following request: 这将产生以下请求:

http://yourdomain/posts?page=3&pagesize=10&sort=asc

Anyway, I know you've already accepted an answer, but hopefully this will help someone. 无论如何,我知道你已经接受了答案,但希望这会对某人有所帮助。

Consider this code, where when you instantiate your collection, you pass in preferences for its offset, limit, or sorting. 请考虑此代码,在实例化集合时,您会传入偏移量,限制或排序的首选项。 The initialize takes defaults if preferences are not provided. 如果未提供首选项,则初始化采用默认值。 And then the url function appends these into the call to the server. 然后url函数将这些附加到对服务器的调用中。 Thereafter, you would have to potentially update these values (probably just the offset), when processing responses from the server. 此后,在处理来自服务器的响应时,您可能必须更新这些值(可能只是偏移量)。

MessageList = Backbone.Collection.extend({

    initialize: function(models, options) {
        options || (options = {});
        this.offset = options.offset || 0;
        this.limit  = options.limit || MessageList.DEFAULT_LIMIT;
        this.sortBy = options.sortBy || MessageList.DEFAULT_SORTBY; 
    },

    url: function() {
        return '/messages?' + 
               'offset=' + encodeURIComponent(this.offset) + '&' +
               'limit=' + encodeURIComponent(this.limit) + '&' + 
               'sort_by=' + encodeURIComponent(this.sortBy);
    },

    parse: function(response) {
       // Parse models
       // Parse offset, limit, sort by, etc
    }

}, {

    DEFAULT_LIMIT: 100,
    DEFAULT_SORTBY: 'name_asc'

});

You can add an id to the url that can be used by the server to make a selection of the data send. 您可以在URL中添加一个ID,服务器可以使用该ID来选择发送数据。 Eg 例如

    var EventsCollection = Backbone.Collection.extend({

        model: Events,

    });

    var eventCollection = new EventsCollection();
    eventsCollection.url = 'foo?offset=10&limit=20';
    eventsCollection.fetch();

Override the "url" function of your collection and add the parameters (?param=xyz). 覆盖集合的“url”函数并添加参数(?param = xyz)。

The options parameter of fetch can also be used as it is the one passed along to the final jQuery ajax call. 也可以使用fetch的options参数,因为它是传递给最终jQuery ajax调用的参数。 So if you add a "data" param there it will be used. 因此,如果你在那里添加一个“数据”参数,它将被使用。

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

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