Skip to content

Wrapper Component example didn't set corresponding item after component mounted #760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
arthow4n opened this issue Feb 13, 2017 · 2 comments

Comments

@arthow4n
Copy link
Contributor

Current behavior

Inside the JSFiddle example of Wrapper Component mentioned in Wrapper Component — Vue.js,

If I edit the JavaScript source in line 34, changing data.selected from 0 to 2, and run again, select2 doesn't set corresponding item World to selected after component mounted.

Performing setTimeout(() => { vm.selected = 2 }, 1000); at the end of script also didn't make select2 set corresponding item to selected.

Expected behavior

Should make select2 select corresponding item after component mounted and on model selected change.

Patched demo is here: https://jsfiddle.net/fruqrvdL/456/

--- old	2017-02-13 14:52:44.697543260 +0800
+++ new	2017-02-13 14:53:03.033136517 +0800
@@ -4,9 +4,10 @@
   mounted: function () {
     var vm = this
     $(this.$el)
-      .val(this.value)
       // init select2
       .select2({ data: this.options })
+      .val(this.value)
+      .trigger('change')
       // emit event on change.
       .on('change', function () {
         vm.$emit('input', this.value)
@@ -15,7 +16,7 @@
   watch: {
     value: function (value) {
       // update value
-      $(this.$el).val(value)
+      $(this.$el).val(value).trigger('change');
     },
     options: function (options) {
       // update options
@saazrai
Copy link

saazrai commented Mar 18, 2017

Thanks. Was facing the same issue. Your patch resolves the issue.

@chrisvfritz
Copy link
Contributor

@arthow4n This looks great! I'd accept a pull request if you want the credit. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants