backbone.js - Backbone Collection from JSON file won't pass to the view -
i've been trying figure out hours, can't seem working. json file being loaded in "friends" collection won't populate in view.
var friend = backbone.model.extend({ defaults: { "name": "unknown", "job": "unknown" } }); var friend = new friend({ }); var friends = backbone.collection.extend({ model: friend, url: '/friends.json', initialize: function() { this.fetch(); } }); var friends = new friends(); var friendsview = backbone.view.extend({ tagname: "ul", template: _.template('<li><%= name %> <%= job %></li>'), render: function(){ this.$el.html(this.template(this.model.tojson())); } }); var friendsview = new friendsview({ model: friend }); friendsview.render(); $('#here').html(friendsview.el);
the json looks this:
{ "name": "timmy", "job": 'sky diver' }, { "name": "michael", "job": 'hat salesman' }
i can see in console json file loading via ajax. in advance.
var friend = backbone.model.extend({ defaults: { "name": "unknown", "job": "unknown" } }); var friends = backbone.collection.extend({ model: friend, //url: '/friends.json', }); var friends = new friends([{ "name": "timmy", "job": 'sky diver' }, { "name": "michael", "job": 'hat salesman' }]); var friendsview = backbone.view.extend({ template: _.template('<li><%= name %> <%= job %></li>'), render: function(){ this.collection.each(function(model){ this.$el.append(this.template(model.tojson())); }, this) } }); var friendsview = new friendsview({ el: $('#here'), collection: friends }); friendsview.render(); // though fetching remote data you'd want.. // friends.fetch({ success: friendsview.render })
Comments
Post a Comment