Skip to content

Commit

Permalink
fix reactive value updating
Browse files Browse the repository at this point in the history
  • Loading branch information
aldeed committed Jan 22, 2015
1 parent e35c0be commit 0b78552
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
19 changes: 15 additions & 4 deletions autoform-select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};
19 changes: 15 additions & 4 deletions themes/bootstrap3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};

6 comments on commit 0b78552

@comerc
Copy link
Contributor

@comerc comerc commented on 0b78552 Jan 28, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really need it? (May be choice reactivity as option).

@aldeed
Copy link
Collaborator Author

@aldeed aldeed commented on 0b78552 Jan 28, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll always want reactivity otherwise the wrong selected value can get stuck showing. But maybe AutoForm can provide a more specific reactive variable that doesn't trigger reruns unless the options have changed.

@comerc
Copy link
Contributor

@comerc comerc commented on 0b78552 Jan 29, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise the wrong selected value can get stuck showing

Explain please. I did not understand.

@aldeed
Copy link
Collaborator Author

@aldeed aldeed commented on 0b78552 Jan 29, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example if you have an update form and a list of documents, and you can click on a document to change the doc for the form reactively (like http://autoform.meteor.com/updateaf). Before this change, the value from the previous doc would remain selected. So this fixes that bug.

@comerc
Copy link
Contributor

@comerc comerc commented on 0b78552 Jan 31, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But native elements works without this routine. I do not understand how.

@aldeed
Copy link
Collaborator Author

@aldeed aldeed commented on 0b78552 Feb 1, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is related to the way the select2 library handles values. It doesn't notice when Blaze updates the options list selected attributes. Probably some internal caching or something. So we need to explicitly tell select2 what to select.

Please sign in to comment.