admin管理员组文章数量:1332352
I have a Marionette CollectionView that renders an ItemView into a <select>
menu, with each ItemView rendering as an <option>
. My question is, when I call the 'change' event on the CollectionView (meaning the user has selected an option), how do I get the model of the selected <option>
from the ItemView?
var singleView = Marionette.ItemView.extend({
template: '#optionTemplate',
tagName: 'option'
});
var listView = Marionette.CollectionView.extend({
itemView: singleView,
tagName: 'select',
id: 'my-selector',
events: {
'change': 'optionSelected'
},
optionSelected: function() {
// I need to get the model from within this function
}
});
I have a Marionette CollectionView that renders an ItemView into a <select>
menu, with each ItemView rendering as an <option>
. My question is, when I call the 'change' event on the CollectionView (meaning the user has selected an option), how do I get the model of the selected <option>
from the ItemView?
var singleView = Marionette.ItemView.extend({
template: '#optionTemplate',
tagName: 'option'
});
var listView = Marionette.CollectionView.extend({
itemView: singleView,
tagName: 'select',
id: 'my-selector',
events: {
'change': 'optionSelected'
},
optionSelected: function() {
// I need to get the model from within this function
}
});
Share
Improve this question
asked Jul 23, 2013 at 1:17
bentedderbentedder
8061 gold badge7 silver badges22 bronze badges
3 Answers
Reset to default 5The cleanest approach is to render the option
tag with the ID of the corresponding model as the value: <option value="42">Forty Two</option>
. Then, on change, get that value and use it to retrieve the model from the collection by ID with collection.get(id)
. Some elements like option
have a natural attribute that makes sense to be the model ID, but for other elements you can just use a data-id="42"
attribute.
Another approach that is viable although I think less simple and clean than the above would be to use jQuery's .data
method or equivalent to associate the model instance with the element.
No need to add custom IDs and such, and also danmactough's answer doesn't work so best ignore it. Even childEvents
in Marionette won't work with select/option tags.
My method would be to just bind to the change
event on the CollectionView
and match the index of model within the collection and the :selected
element within the dropdown:
var listView = Marionette.CollectionView.extend({
itemView: singleView,
tagName: 'select',
id: 'my-selector',
events: {
'change': 'optionSelected'
},
optionSelected: function() {
// The selected model:
var model = this.collection.at($(':selected', this.$el).index()));
}
});
I think a better, Marionette-specific solution would be to use the modelEvents
or collectionEvents
configuration hash available in a CollectionView or CompositeView.
var listView = Marionette.CollectionView.extend({
itemView: singleView,
tagName: 'select',
id: 'my-selector',
modelEvents: {
'change': 'modelChanged'
},
modelChanged: function(e) {
// "e" is the event
// "this" is the ItemView
// "this.model" is the model
}
});
本文标签:
版权声明:本文标题:javascript - How do I get the model for the selected <option> in a Marionette.js CollectionView? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742322406a2453053.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论