Backbone Collection jsonp ajax 结果未正确生成模型 [英] Backbone Collection jsonp ajax results not generating model correctly
问题描述
这是我第一次涉足 Backbone,我只是想从 Dribbble 获取 Api 调用.
This is my first foray into Backbone and I am simply trying to fetch an Api call from Dribbble.
我正在尝试以最 Backbone-native 的方式做到这一点.然而,它似乎在做一些有趣的事情,因为它制作了这个系列.
I'm trying to do it the most Backbone-native kind of way. However it seems to be doing something funny at the point it makes the collection.
此时我并不担心实际渲染,我只是希望集合能够为来自 Api 的每个响应正确设置一个模型.
I am not so bothered about actually rendering at this point, I just want the collection to setup properly with a model for each of the responses from the Api.
任何提示和方向都会很棒.我完全做错了吗?
Any hints and direction would be great. Am I completely doing this all wrong?
推荐答案
不确定您的问题是什么.重写一下你的集合类来分离关注点,我得到了完全有效的模型.
Not sure what your problem is. Rewriting a bit your collection class to separate the concerns, I get perfectly valid models.
Shot = Backbone.Model.extend({
initialize:function(opts) {
console.log("init shot : "+opts.id);
}
});
ShotsList = Backbone.Collection.extend({
model: Shot,
sync: function(method, model, options) {
var params = _.extend({
type: 'GET',
dataType: 'jsonp',
url: model.url(),
processData: false
}, options);
return $.ajax(params);
},
parse: function(response) {
return response.shots;
},
url: function() {
return "http://api.dribbble.com/players/" + encodeURIComponent(this.player) + "/shots?per_page=18";
}
});
s=new ShotsList();
s.bind("reset",function(collection) {
console.log(collection.models);
console.log(collection.pluck("image_teaser_url"));
});
s.player="jordan";
s.fetch();
这篇关于Backbone Collection jsonp ajax 结果未正确生成模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!