覆盖 Backbone 的 Collection-fetch [英] override Backbone's Collection-fetch
问题描述
我想以非 RESTful 方式获取我的集合,所以我决定用
I want to get my collection in a NON-RESTful way, so I decide to override the Collection.fetch
with
App.carClc = Backbone.Collection.extend({
model : App.cardModel,
url : 'http://localhost/bbtest/data.php',
fetch : function() {
$.ajax({
type : 'GET',
url : this.url,
success : function(data) {
console.log(data);
}
});
}
});
我不知道如何将我的收藏设置为响应.我是 BackboneJS 的新手,谢谢大家!
I don't know how to set my collection to the response. I'm new to BackboneJS, thanks all of you!
推荐答案
如果你想给 fetch
添加一个自定义的装饰器",但又不想完全覆盖它,试试:
If you want to add a custom "decorator" to fetch
, but not override it completely, try:
var MyCollection = Backbone.Collection.extend({
//custom methods
fetch: function(options) {
//do specific pre-processing
//Call Backbone's fetch
return Backbone.Collection.prototype.fetch.call(this, options);
}
});
在这里,您不必推出自己的 $.ajax
Here, you don't have to roll out your own $.ajax
此外,如果您想使用由 Backbone 的 fetch
方法返回的 jQuery 承诺,请不要忘记最后一行中的 return
.
Also, don't forget the return
in the last line if you want to use the jQuery promise returned by Backbone's fetch
method.
参见 http://japhr.blogspot.in/2011/10/overriding-url-and-fetch-in-backbonejs.html 了解更多详情.
See http://japhr.blogspot.in/2011/10/overriding-url-and-fetch-in-backbonejs.html for more details.
这篇关于覆盖 Backbone 的 Collection-fetch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!