2013-02-22 131 views
0

我有一個主鏈集合的形式骨幹收集抓取並請求

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

我也有ID的數組:

變種IDS = [1,2,3,4] ;

按的文檔,我叫上取東西,像這樣:

this.collection.fetch({$ .PARAM({IDS:exercise_ids.join( 「」)})} );

這將請求發送到形式的服務器:

/材料/ IDS = 1,2,3,4

這工作,但我不能?對請求的形式感到滿意。有沒有你的幫助辦法,我可以返回以下形式的請求(即不使用查詢字符串)

/材料/ 1,2,3,4

感謝(提前) 。

+0

該代碼不能工作(見http://jsfiddle.net/EXwmp/1/)。你是否錯誤地複製了它?你能發佈你的收藏的所有代碼嗎? – 2013-02-23 00:22:36

回答

0

假設當您執行/ stuff/[param]時,後端將[param]看作id,那麼功能沒有區別。這些請求是在幕後進行的,不會影響瀏覽器的地址欄,因此這裏沒有任何關注。如果您想格式化您的網址,你可以在你的骨幹集合定義URL作爲功能

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()