骨干收集JSONP AJAX结果无法正确生成模式 [英] Backbone Collection jsonp ajax results not generating model correctly

查看:117
本文介绍了骨干收集JSONP AJAX结果无法正确生成模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://jsfiddle.net/nf8NM/3/

这是我首次涉足骨干,我只是想获取来自Dribbble API调用。

This is my first foray into Backbone and I am simply trying to fetch an Api call from Dribbble.

我试图做到这一点的最骨干本地的一种方式。但是似乎它使得集合点做一些有趣的。

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();

这篇关于骨干收集JSONP AJAX结果无法正确生成模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆