0

我有一个骨干收藏

var Stuff = Backbone.Collection.extend({
    url: "stuff/" 
    model: StuffModel
});

我还有一个 id 数组:

var ids = [ 1, 2, 3, 4 ];

根据文档,我在 Stuff 上调用 fetch,如下所示:

this.collection.fetch( { $.param({ ids : exercise_ids.join( "," )})});

这会向表单的服务器发送一个请求:

/stuff/?ids=1,2,3,4

这可行,但我对请求的形式不满意。有没有办法可以使用以下形式发送请求(即不使用查询字符串)

/东西/1,2,3,4

在此先感谢您的帮助。

4

1 回答 1

0

假设您在执行 /stuff/[param] 时后端将 [param] 视为 id,那么功能上没有区别。这些请求是在幕后发出的,不会影响浏览器的地址栏,所以这里没有任何问题。如果要格式化 url,可以将 url 定义为 Backbone Collection 中的函数

var Stuff = Backbone.Collection.extend({

    initialize: function(models, options) {
        this.ids = options.ids

        //bind functions to 'this' so that you can access ids
        _.bind(this, 'setIds', 'url');
    },

    setIds: function(ids) {
        this.ids = ids;
        //return 'this' to allow chaining
        return this;
    },

    url: function() {
        return 'stuff/' + this.ids.join(',')
    }
});

myCollection.setIds([1,2,3,4]).fetch()
于 2013-02-23T00:51:42.677 回答