Eva*_*bbs 3 backbone.js backbone.js-collections
我有一个像这样的Backbone Collection:
var ThreadCollection = Backbone.Collection.extend({
url: '/api/rest/thread/getList'
});
var myCollection = new ThreadCollection();
Run Code Online (Sandbox Code Playgroud)
然后我使用数据对象从服务器获取它以附加查询参数(所以在这种情况下它出来'/ api/rest/thread/getList?userId = 487343')
myCollection.fetch({
data: {
userId: 487343
}
})
Run Code Online (Sandbox Code Playgroud)
我可能想要使用其他参数而不是userId(groupId,orgId等),但我理想情况下在初始化时定义数据参数,然后才能运行fetch()而不指定.像这样的东西:
var myCollection = new ThreadCollection({
data: {
userId: 487343
}
});
myCollection.fetch()
Run Code Online (Sandbox Code Playgroud)
但它不起作用.有谁知道有没有办法做到这一点?谢谢!
一种方法是fetch在集合上定义一个自定义方法,该fetch方法使用一些可覆盖的默认值调用super 方法:
var ThreadCollection = Backbone.Collection.extend({
url: '/api/rest/thread/getList',
fetch: function(options) {
return Backbone.Collection.prototype.fetch.call(this, _.extend({
data: {
userId: 48743
}
}, options));
}
});
var myCollection = new ThreadCollection();
myCollection.fetch();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3977 次 |
| 最近记录: |