vuejs / Discussion

Vue.js discussion
167 stars 17 forks source link

How works reactive store sync? #479

Closed renatorib closed 8 years ago

renatorib commented 8 years ago

Hello, In my project, I have this store:

var items = {
  data: [],
  fetch: function(query){
    doAsyncFetch(query, function(resData){
      Vue.set(this, 'data', resData);
    });
  }
}

And in my component, I've setted:

Vue.component('page-items', {
  data: function(){ 
    return {
      items: items.data
    }
  }
});

But when I fetch store with items.fetch(someQuery), the items.data are updated, but $vm.items don't update.

image

If I replace Vue.set(this, 'data', resData); for this.data = resData doesn't works too. The only way that works is when I use items.data.push({});, but I can't push an entire array, I need replace.

Help me understand how works reactive sync, please. Thanks!

renatorib commented 8 years ago

Well, It worked with jQuery extend (or underscore/lodash extend)

$.extend(this.data, resData);

But I still don't understand how it works.

Jokcy commented 8 years ago
var items = {
  data: [],
  fetch: function(query){
    doAsyncFetch(query, function(resData){
      Vue.set(this, 'data', resData);
    });
  }
}

are sure 'this' is really 'this'?

renatorib commented 8 years ago

Yes, it's only an example I've writed, but is really 'this'.

I already solved this problem in vue forums :D Thanks

narazin commented 7 years ago

Can you share your solve?

renatorib commented 7 years ago

1 year ago, I don't remember, sorry.