简体   繁体   中英

Target a particular collection from a JSON file in Backbone.js

I'm a beginner and learning Backbone.js. Any suggestions would be appreciated.

Colletion:

app.Collections.UserCollection = Backbone.Collection.extend({
    model: app.Models.IdModel,
    url: "/test/test_data.json"
})


var profileDataCollection = new app.Collections.UserCollection();

profileDataCollection.fetch({
    success: function(data){
        console.log(data); // returns JSON data
    }
});

Returned data from fetch():

{  
   "msg":[  
      {  
         "firstname":"Abc",
         "lastname":"Xyz"
      },
      {  
         "firstname":"Test",
         "lastname":"Test"
      },
      {  
         "firstname":"Klm",
         "lastname":"Nop"
      }
   ],
   "flash_message":"",
   "log":[  

   ]
}

Just wondering how can I get the collection here for "msg" property? So that I can pass the collection to my view like this:-

new app.Views.UsersView( { collection: profileDataCollection });

You can add a parse method to the collection like

parse: function(response){
   return response.msg;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM