【发布时间】:2013-11-30 21:31:49
【问题描述】:
我想在 Backbone Collection 上触发 fetch 方法,该方法将传递一个类似于 Model.fetch(id) 中发生的 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;
}
});
在我尝试获取数据时的视图中:-
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
}
});
很晚才发现:为了缩短上面的故事,我想要Get /collection/id in backbone之类的东西。
【问题讨论】:
-
您为什么要通过在集合上调用
fetch()来获取单个模型的数据?这没有意义。 -
假设我有
Department Details(主干模型),它有List<task>,我不需要触发一个GetTaskapi,它将有department id作为参数并返回List<task>wrt DepartmentId..我可能对这个问题不太清楚..为此道歉!
标签: backbone.js backbone-views