我想做的事:
渲染一个带有选项标签的下拉选择,当用户在下拉列表中选择一个选项时,获取新选择的模型并对其进行处理。
问题:
我很难在通过 CompositeView 调用的 ItemView 中触发更改事件。
出于某种原因,正在触发 CompositeView:change (log: Holy moses),但是它对我没有多大帮助,因为它不会给我选择的模型。
我尝试了很多东西,但都没有真正奏效。
任何帮助将不胜感激!
代码:
Configurator.module('Views.Ringsizes', function(Views, Configurator, Backbone, Marionette, $, _) {
Views.DropdownItem = Marionette.ItemView.extend({
tagName: 'option',
template: "#dropdown-item",
modelEvents: {
'change': 'modelChanged'
},
onRender: function(){
console.log('tnt');
this.$el = this.$el.children();
this.setElement(this.$el);
},
modelChanged: function(model) {
console.log("holy mary");
}
});
Views.DropdownView = Marionette.CompositeView.extend({
template: "#dropdown-collection",
className: 'configurator-ringsizes-chooser',
itemView: Views.DropdownItem,
itemViewContainer: '.product_detail_ring_sizes',
events: {
"change": "modelChanged"
},
initialEvents: function(){},
initialize: function(){
console.log(this.model);
this.collection = new Backbone.Collection(this.model.getRingsizes());
},
modelChanged: function(model) {
console.log("holy moses");
}
});
Views.List = Marionette.CollectionView.extend({
className: 'configurator-ringsizes',
itemView: Views.DropdownView
});
});
模板代码:(如果需要)
<script type="text/template" id="dropdown-item">
<option value="<@- code @>" <@ if(current) { @> selected="selected" <@}@> ><@- name @> </option>
</script>
<script type="text/template" id="dropdown-collection">
<div class="accordionContent accordionContent_ringsizes">
<div class="configurator-ringsizes-chooser-ringsizes-region">
<select class="product_detail_ring_sizes"></select>
</div>
</div>
</script>