Waley Chen
Waley Chen

Reputation: 929

How to Work with a Backbone.js Model and Views or Rails with a Deeply Nested JSON Object

I'm new to Backbone.js and working with JSON.

I'm having trouble working with the following JSON: http://pastebin.com/FFEwc0Fb

What I want to achieve is to

  1. create a model of that JSON data
  2. bind the model to a view
  3. bind the view to a template and learn how to show certain attributes of the data e.g. feed->title and the titles of each playlists which is feed->entry->array->title

Could someone provide sample code of that does the above or point me in the right direction?

Possible Alternative Solutions: I'm also using Rails in my App so I could use try using Rails to turn the JSON into the JSON I want it to be ...

Upvotes: 0

Views: 212

Answers (1)

ryanmarc
ryanmarc

Reputation: 1680

look at the parse method on models and collections. After you make your fetch the response is always passed through a parse function that you define on your model or collection. In there you can go through the object and build up the attributes to be set to the model how you want them. For example:

parse: function(response) {
    var attrs = {
        title: response.feed.title,
        author: response.feed.author
    };

    return attrs;
}

That's a small example of how you could do it within backbone.

Upvotes: 1

Related Questions