How to create Backbone Collection from a JSON API call that returns root parameters as well as array

后端 未结 1 1291
面向向阳花
面向向阳花 2021-02-05 15:44

Backbone.js noob here.

I want to create a collection, from a JSON API external to my application. Specifically, the api from Stackoverflow. I know I should set the url

相关标签:
1条回答
  • 2021-02-05 16:16

    If this is the only collection in your app to work with such api, all you have to do is to override parse method for it:

    App.Collections.Users = Backbone.Collection.extend({
        // ...
        parse: function(resp, xhr) {
            return resp.users
        }
    })
    

    If you also have to save your models, maybe you will need to override Backbone.sync. Don't hesitate to read backbone's source: it's thoroughly annotated and easy to follow.

    0 讨论(0)
提交回复
热议问题