问题描述
我想在Backbone Collection上触发fetch
方法,该方法将传递一个与Model.fetch(id)
I want to fire fetch
method on Backbone Collection which would pass an Id parameter similar to what happens in Model.fetch(id)
例如
var someFoo= new Foo({id: '1234'});// Where Foo is a Backbone Model
someFoo.fetch();
我的骨干收藏:-
var tasks = backbone.Collection.extend({
model: taskModel,
url: '/MyController/GetTasks',
initialize: function () {
return this;
}
});
当我尝试获取数据时,在我的视图中:-
In my View when I try to fetch data:-
var _dummyId = 10; //
// Tried approach 1 && It calls an api without any `id` parameter, so I get 500 (Internal Server Error).
this.collection.fetch(_dummyId);
// Tried approach 2 && which fires API call passing Id, but just after that
// I am getting error as below:- Uncaught TypeError: object is not a function
this.collection.fetch({
data: {
id: _dummyId
}
});
发现得很晚:为了简化上述故事,我想要类似.
Found it very late : To cut short the above story I want something like Get /collection/id in backbone.
推荐答案
感谢您的回答,最后我从.
Thank you for your answers, finally I got the solution from Backbone.js collection options.
抱歉,我无法正确解释问题,而对于其他要求,其他人却聪明出色地做到了.
Apologies that I couldn't explain the question properly while for same requirement others have done brilliantly and smartly.
解决方案:我可以使用类似:-
Solution : I can have something like :-
var Messages = Backbone.Collection.extend({
initialize: function(models, options) {
this.id = options.id;
},
url: function() {
return '/messages/' + this.id;
},
model: Message,
});
var collection = new Messages([], { id: 2 });
collection.fetch();
感谢nrabinowitz
. 链接到答案
这篇关于如何调用传递ID的Backbone Collection的获取方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!