Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Okay, i finally got this to work.</p> <p>I'm a bit dissapointed that i have to rely on a data- attribute for this, but this is the only way i found. took me long enough already :)</p> <p>Here's how i did it now:</p> <h2>Template code:</h2> <pre><code>&lt;script type="text/template" id="dropdown-item"&gt; &lt;option data-cid="&lt;@- cid @&gt;" value="&lt;@- code @&gt;" &lt;@ if(current) { @&gt; selected="selected" &lt;@}@&gt; &gt;&lt;@- name @&gt;&lt;/option&gt; &lt;/script&gt; &lt;script type="text/template" id="dropdown-collection"&gt; &lt;div class="configurator-ringsizes-chooser-ringsizes-region"&gt; &lt;select class="product_detail_ring_sizes"&gt;&lt;/select&gt; &lt;/div&gt; &lt;/script&gt; </code></pre> <h2>Code:</h2> <pre><code>Configurator.module('Views.Ringsizes', function(Views, Configurator, Backbone, Marionette, $, _) { Views.DropdownItem = Marionette.ItemView.extend({ tagName: 'option', template: "#dropdown-item", serializeData: function() { var data = { cid: this.model.cid, code: this.model.get('code'), name: this.model.get('name'), current: this.model.get('current') }; return data; }, onRender: function(){ this.$el = this.$el.children(); this.setElement(this.$el); } }); Views.DropdownView = Marionette.CompositeView.extend({ template: "#dropdown-collection", className: 'configurator-ringsizes-chooser', itemView: Views.DropdownItem, itemViewContainer: '.product_detail_ring_sizes', events: { "change select": "modelChanged" }, initialEvents: function(){}, initialize: function(){ this.collection = new Backbone.Collection(this.model.getRingsizes()); }, modelChanged: function(e) { var cid = $(e.currentTarget+"option:selected").data('cid'); var currentModel = this.collection.find(function(elem) { return elem.get('current'); }); var model = this.collection.find(function(elem) { return elem.cid === cid; }); currentModel.set({ current: false }); model.set({ current: true }); // AND here i'm doing my stuff, getting the overall model through this.model, the collection of options through this.collection and the currently selected model through currentModel. } }); Views.List = Marionette.CollectionView.extend({ className: 'configurator-ringsizes', itemView: Views.DropdownView, model: this.model }); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload