Backbone Collection jsonp ajax results not generating model correctly

后端 未结 1 1395
清歌不尽
清歌不尽 2021-01-03 13:29

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

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 B

相关标签:
1条回答
  • 2021-01-03 14:06

    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();
    
    0 讨论(0)
提交回复
热议问题