How to write a jQueryUI selectmenu with dynamic options into a Meteor Blaze template -


i having trouble making jqueryui selectmenu work in meteor app when options dynamic. using:

  • meteor 1.4.1
  • jquery 2.2.4
  • jqueryui 1.11.4
  • lodash 4.15.0

physiocoder said on different question, "the meteor reactivity force choose in charge of dom updates.".

i realize fundamental problem here. accordingly, there no problem if page/template can let meteor load page content/data , hand on dom control jqueryui's widgets. have case want have cake , eat -- want have meteor reactively feed options jqueryui widget (specifically selectmenu @ moment) still let jqueryui handle styling/theming.

initializing jqueryui widgets in template onrendered functions works fine, destroying jqueryui widgets, necessary, in template ondestroyed functions. calling selectmenu('refresh') on option template's onrendered function refresh selectmenu when new options available. however, cannot find anywhere can call refresh when options reactively removed such selectmenu refreshed new, correct ui state -- not @ end of event changes meteor data context, not on option template's ondestroyed function, , not tracker.autorun function tied appropriate data source.

html:

<head>   <title>proof of concept</title> </head>  <body>   <div id="myapp">     {{> myform}}   </div> </body>  <template name="myform">   <div>     <div id="selectedentries">       <h3>selected entries</h3>       <ul class="display-list">         {{#each entry in selectedentries}}           {{> myform_entry entry}}         {{/each}}       </ul>     </div>      <br/>      <form id="includeentry">       <select name="entrytoinclude" id="entrytoinclude">         {{#each potentialentry in availableentries}}           {{> myform_option potentialentry}}         {{/each}}       </select>       <input type="submit" value="include entry">     </form>   </div> </template>  <template name="myform_entry">   <li>     <div class="button removeentry" data-id="{{_id}}">x</div>     <span>{{name}}</span>   </li> </template>  <template name="myform_option">   <option value="{{_id}}">{{name}}</option> </template> 

javascript:

template.myform.helpers({   availableentries: function () {     return _.filter(session.get('someentries'), function(o) {       return session.get('selectedentryids').indexof(o._id) == -1;     });   },   selectedentries: function () {     return _.filter(session.get('someentries'), function(o) {       return session.get('selectedentryids').indexof(o._id) != -1;     });   } });  template.myform.events({   'submit #includeentry': function (event) {     event.preventdefault();      if (_.isempty(session.get('selectedentryids'))) {       session.set('selectedentryids', [$('#entrytoinclude').val()]);     } else {       let selectedentryids = session.get('selectedentryids');       selectedentryids.push($('#entrytoinclude').val());       session.set('selectedentryids', selectedentryids);     }     $('#entrytoinclude').selectmenu('refresh')   },   'click .removeentry': function (event) {     event.preventdefault();      let selectedentryids = session.get('selectedentryids');     selectedentryids = _.pull(selectedentryids, $(event.target).parent().attr('data-id'));     session.set('selectedentryids', selectedentryids);   } });  template.myform.oncreated(function () {   let someentries = [{     _id:'1',     name:'one'   },{     _id:'2',     name:'two'   },{     _id:'3',     name:'three'   },{     _id:'4',     name:'four'   },{     _id:'5',     name:'five'   },{     _id:'6',     name:'six'   }];   session.set('someentries', someentries);   session.set('selectedentryids', []); });  template.myform.onrendered(function () {   $('#entrytoinclude').selectmenu();   $('input:submit').button(); }); template.myform_entry.onrendered(function () {   $('.button').button(); }); template.myform_option.onrendered(function () {   if ($('#entrytoinclude').is(':ui-selectmenu')) {     $('#entrytoinclude').selectmenu('refresh');   } });  template.myform_option.ondestroyed(function () {   $('#entrytoinclude').selectmenu('refresh'); });  tracker.autorun(function () {   if (session.get('selectedentryids')) {     if ($('#entrytoinclude').is(':ui-selectmenu')) {       $('#entrytoinclude').selectmenu('refresh');     }   } }); 

the #entrytoinclude selectmenu continues include entry selected; selecting second entry numbered high or higher selects subsequent entry (e.g. selecting 4 5 selects 4 , 6) except selecting last entry after successful selection nothing refresh selectmenu.

adding refresh entry template's onrendered function makes work.

template.myform_entry.onrendered(function () {   $('.button').button();   if ($('#entrytoinclude').is(':ui-selectmenu')) {     $('#entrytoinclude').selectmenu('refresh');   } }); 

but better approach entire problem appreciated.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -