javascript - How to use Backbone to parse JSON? -
i'm learning backbone, little example. have json url, result looks :
{ help: "help", success: true, result: { count: 13, results: [ { name: "name 1", description: "desc 1", resources: [ { img: "url.jpg" } ] }, { name: "name 2", description: "desc 2", resources: [ { img: "url2.jpg" } ] } ] } }
now want results html div, used this:
$(function() { var dataset = backbone.model.extend(); var profilelist = backbone.collection.extend({ model: dataset, url: 'profiles.json' }); var profileview = backbone.view.extend({ el: "section#block-block-5", template: _.template($('#profiletemplate').html()), render: function(eventname) { _.each(this.model.models, function(profile){ var profiletemplate = this.template(profile.tojson()); $(this.el).append(profiletemplate); }, this); return this; } }); var profiles = new profilelist(); var profilesview = new profileview({model: profiles}); profiles.fetch({ success: function(){ profilesview.render(); } }); }); <script id="profiletemplate" type="text/template"> <div class="profile"> <div class="info"> <div class="name"> <%= name %> </div> <div class="description"> <%= description %> </div> <div class="image"> <%= resources.url %> </div> </div> </div> </script>
the problem don't know how access results
group in json response.
thanks help.
you should use parse function bind model results
property of result
element
var profilelist = backbone.collection.extend({ model: dataset, url: 'profiles.json', parse: function (response) { return response.result.results; } });
Comments
Post a Comment