javascript - activate a class in java-script template rendered through backbone -


i have bootstrap-select's drop down filter in javascript template, rendered through backbone's view.

<script type="text/template" id="activities-template">             <div class="span22">                 <div class="members">                     <select class="selectpicker" multiple title='<i class="icon-filter"></i> filter by'>                         <% _.each(project_users, function(user){ %>                             <option id=<%= user.id %>><%= user.first_name %></option>                          <% }); %>                     </select>                 </div>             </div> </script> 

when ajax request made gets projects_users , enable selectpicker @ start drop-down options unselected.

@activities.fetch      success: (activities) ->         activities_html = that.template           activities: _.groupby activities.tojson()           has_more: that.activities.has_more         that.$el.html activities_html          $**('.selectpicker').selectpicker()**  //enable selectpicker 

when click on drop-down item, in checked state, makes ajax request data... , again selectpicker enabled ..thereby losing previous state ( instead of showing last selected option , shows drop-down option unselected).

the problem seems how enable select picker, once ?

the easiest way add class on select picker, , activate if class not present:

@activities.fetch   success: (activities) ->     activities_html = that.template       activities: _.groupby activities.tojson()       has_more: that.activities.has_more     that.$el.html activities_html      select_picker = $('.selectpicker')     unless select_picker.hasclass 'active'       select_picker.addclass('active').selectpicker()    

be aware work long don't re-render backbone view. if do, you'll losing 'active' class. it's possible real reason <select> being reset; if case, should keep track of current selection in view, , update template use when renders. instance, might pass in 'active_id' variable template; if it's present, set option shown, , add 'active' class <select>.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -