javascript - backbone - event not fired on selected element changed -
i'm generating drop down list backbone.view
.
after attaching dom, change event not fired. delegateevents
doesn't fixes it. can show me blind spot is?
model , collection:
app.models.dictionaryitem = backbone.model.extend({ default: { key: '', value: '', id: 0 } }); app.collections.dictionary = backbone.collection.extend({ model: app.models.dictionaryitem, initialize: function (models, options) { }, parse: function (data) { _.each(data, function (item) { // if(item){ var m = new app.models.dictionaryitem({ key: item.code, value: item.name }); this.add(m); // } }, this); } });
views:
app.views.itemview = backbone.view.extend({ tagname: 'option', attributes: function () { return { value: this.model.get('key') } }, initialize: function () { this.template = _.template(this.model.get('value')); }, render: function () { this.$el.html(this.template()); return this; } }); app.views.collectionview = backbone.view.extend({ tagname: 'select', attributes: { 'class': 'rangeslist' }, events: { 'change .rangeslist': 'onrangechanged' }, initialize: function (coll) { this.collection = coll; }, render: function () { _.each(this.collection.models, function (item) { this.$el.append(new app.views.itemview({ model: item }).render().el); }, this); // this.delegateevents(this.events); return this; }, selected: function () { return this.$el.val(); }, onrangechanged: function () { alert('changed'); } });
rendering:
var coll = new app.collections.dictionary(somedata, { parse: true }); var v= new app.views.collectionview(coll); var vv=v.render().el; // new app.views.collectionview(coll).render().el; $('body').append(vv)
the tagname
, attributes
on collectionview
:
tagname: 'select', attributes: { 'class': 'rangeslist' },
say el
<select class="rangeslist">
. events
:
events: { 'change .rangeslist': 'onrangechanged' },
are listening 'change'
events .rangeslist
inside view's el
. fine manual:
events written in format
{"event selector": "callback"}
. [...] omitting selector causes event bound view's root element (this.el
).
so you're trying listen events doesn't exist. if want listen events directly view's el
leave out selector:
events: { 'change': 'onrangechanged' }
Comments
Post a Comment