1

I'm quite new to backbone.js and currently struggling with structuring the data properly. I have JSON that I need to map to the backbone model/collection.

What's the best way to approach this kind of nested data? resultSet should be a collection of "result" models but how do I include other data like "resultURL", "clearKeywordUrl" etc. ?

"resultUrl":"",
"clearKeywordUrl":"",
"resultTotal": 10,
"nextPageUrl" : ""
"resultSet":[
{
    "id":"",
    "title":"",
    "subTitle":null,
    "summary":""
}]

2 Answers 2

2

If i get it right you want the content of resultSet to be your collections models, and the other keys are some kind of "meta" info for your collection? You could override the parse method of your collection to store the metadata in the collection object and use only the content of resultSet as your models: (untested concept proposal)

var coll = Backbone.Collection.extend({
  parse: function(data){

   this.resultUrl       = data.resultUrl;
   this.clearKeywordUrl = data.clearKeywordUrl;
   this.nextPageUrl     = data.nextPageUrl;

   return data.resultSet; //use only the resultSet content as models

  },

  initialize: function(options){
   console.log("resultUrl", this.resultUrl);
  }
});
Sign up to request clarification or add additional context in comments.

Comments

1

You should have a model with name Result And Result has following attributes:

resultUrl: '',
clearKeywordUrl: '',
...

And this model has a field called resultSet which will be the collection of results. And define a separate model with name resultSetItem with some defaults.

If you get some time, checkout the introductory video from Thomas Davis at Backbone Tutorials

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.