Binding a dropdown using Backbone.js -
i have started poc on backbone.js , facing basic problem.i populating dropdown list using collection. dropdwon not getting populated static values defined in collection.
can please me out on this.
$(function () { var country = backbone.model.extend(); var countries = backbone.collection.extend({ model: country }); var countryview = backbone.view.extend({ tagname: "option", initialize: function () { _.bindall(this, 'render'); }, render: function () { $(this.el).attr('value', this.model.get('id')).html(this.model.get('name')); return this; } }); var countriesview = backbone.view.extend({ initialize: function () { _.bindall(this, 'addone', 'addall'); this.collection.bind('reset', this.addall); }, addone: function (country) { $(this.el).append( new countryview({ model: country }).render().el); }, addall: function () { this.collection.each(this.addone); } }); var countries = new countries([{ id: 'us', name: 'us' }, { id: 'india', name: 'india' }, { id: 'uk', name: 'uk' }]); var countriesview = new countriesview({ el: $("#country"), collection: countries }); //countries.fetch(); });
it looks render method never getting called because collection's reset
wasn't being fired. here's fiddle in i'm calling collection.reset
model data after countriesview
created. there console.log
statement in render, , should see fires.
countriesview.collection.reset([{ id: 'us', name: 'us' }, { id: 'india', name: 'india' }, { id: 'uk', name: 'uk' }]);
i think there event fires when view first created, , need render view in response that.
edit: actually, no, why there be? backbone's view that: view. logic encapsulates showing itself. doesn't know collection view, doesn't respond being instantiated collection. want add logic in constructor collection view started (or i've done here , call reset populate collection). i'm used using marionette in addition backbone. marionette gives enough specialization around views don't need worry creating custom backbone views handle collections, etc.
Comments
Post a Comment