javascript - Backbone dropdown change doesn't update model in a collection -
javascript - Backbone dropdown change doesn't update model in a collection -
i'm new backbone , have problem while learning, trying solve time , get's annoying. i'm rendering 1 dropdown each of attributes in model. when alter selected value, update model. when seek grab alter event in orderview, not work. if in orderingview, works, need search current model.
what doing wrong here?
the situation: have model:
var order = backbone.model.extend({ defaults:{ column: '', order: '' } });
and corresponding collection having order model. views:
var orderview = backbone.view.extend({ model: new order(), initialize: function(){ this.template = _.template(order_template); }, render: function(){ var temp = this.template({model:this.model.tojson(),order_columns:this.order_columns}); this.$el.html(temp); homecoming this; }, events: { 'change .order_columns': 'change_column' }, change_column: function(){ console.log('column changed..'); } }); var orderingview = backbone.view.extend({ model: new ordercollection(), initialize: function(){ var = this; _(this.options.orderings).each(function(or){ that.model.add(new order({column: or.column, order: or.order})); }); this.model.on('add', this.render, this); this.model.on('change', this.changed_item,this); }, render: function(){ var = this; that.$el.html(''); var temp_order = ''; _.each(this.model.toarray(), function(ordering, i){ var obj = new orderview({model: ordering, order_columns: that.options.order_columns}).render().$el.html(); temp_order += obj; }); var temp = _.template(ordering_template,{element: temp_order}); that.$el.html(temp); $(document).ready(function(){ $('.add-ordering').click(function(){ var new_order = new order({column: 'none', order: 'ascending'}); that.model.add(new_order); }); }); homecoming this; }, });
and phone call different file using requirejs:
that.orderview = new orderingview({el:table, orderings: that.data.models.layers[0].orderings, order_columns:that.data.models.layers[0].columns});
also load html templates @ origin using requirejs , known order_template , ordering_template
templates:
class="snippet-code-html lang-html prettyprint-override"><div class="control-group"> <div class="controls" style="margin-left:30px;"> <select class="order_columns" name="columns" style="float:left;"> <option value='none' <%=model.column==='none'? 'selected': ''%>>none</option> <% _(order_columns).each(function(col){ %> <option value="<%=col.alias%>" <%=model.column===col.alias? 'selected': ''%>><%= col.name %></option> <% }); %> </select> <select class="ordering" name="ordering" style="float:left;margin-left:10px;"> <option value='ascending' <%=model.order === 'ascending'? 'selected':''%> >asc</option> <option value='descending' <%=model.order === 'descending'? 'selected':''%>>desc</option> </select> <span class="ui-icon ui-icon-circle-close ui-order-close <%=model.column%>" style="margin-left:10px;"></span> </div> </div>
and ordering_template
class="snippet-code-html lang-html prettyprint-override"><div class="row"> <form class="form-horizontal span12"> <fieldset> <br/> <div class="row"> <div class="span9 ordering_div" style="width:520px;"> <%=element%> </div> </div> </fieldset> </form> </div>
any suggestion?
javascript jquery backbone.js
Comments
Post a Comment