1
我從Backbone.View
生成一個下拉列表。
將它附加到DOM後,更改事件不會被觸發。 delegateEvents
不修復它。有人能告訴我盲點在哪裏嗎?骨幹 - 事件沒有發生在選定元素髮生變化
型號和收集:
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);
}
});
瀏覽:
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');
}
});
渲染:
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)
'this.template = _.template(this.model.get(「值」));'評判這個路線,似乎你不明白JS模板是如何工作的。或者你真的把JS模板存儲在**模型**中? – hindmost