From 0b78552d0ee8a1e9d58a8d9dc52b49ced68c3107 Mon Sep 17 00:00:00 2001 From: Eric Dobbertin Date: Thu, 22 Jan 2015 00:43:50 -0600 Subject: [PATCH] fix reactive value updating --- autoform-select2.js | 19 +++++++++++++++---- themes/bootstrap3.js | 19 +++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/autoform-select2.js b/autoform-select2.js index f6ae83b..a2e1291 100644 --- a/autoform-select2.js +++ b/autoform-select2.js @@ -97,10 +97,21 @@ Template.afSelect2.helpers({ }); Template.afSelect2.rendered = function () { + var template = this; + // instanciate select2 - this.$('select').select2(this.data.atts.select2Options || {}); -}; + template.$('select').select2(template.data.atts.select2Options || {}); + + template.autorun(function () { + var data = Template.currentData(); + + var values = []; + _.each(data.items, function (item) { + if (item.selected) { + values.push(item.value); + } + }); -Template.afSelect2.destroyed = function () { - this.$('select').select2('destroy'); + template.$('select').select2("val", values); + }); }; diff --git a/themes/bootstrap3.js b/themes/bootstrap3.js index da27f77..e71383f 100644 --- a/themes/bootstrap3.js +++ b/themes/bootstrap3.js @@ -21,10 +21,21 @@ Template.afSelect2_bootstrap3.helpers({ }); Template.afSelect2_bootstrap3.rendered = function () { + var template = this; + // instanciate select2 - this.$('select').select2(this.data.atts.select2Options || {}); -}; + template.$('select').select2(template.data.atts.select2Options || {}); + + template.autorun(function () { + var data = Template.currentData(); + + var values = []; + _.each(data.items, function (item) { + if (item.selected) { + values.push(item.value); + } + }); -Template.afSelect2_bootstrap3.destroyed = function () { - this.$('select').select2('destroy'); + template.$('select').select2("val", values); + }); };