ManuelDeLeon / viewmodel-react

Create your React components with view models.
MIT License
24 stars 3 forks source link

Shared property is not updated #17

Closed softwarerero closed 7 years ago

softwarerero commented 7 years ago

When a shared object's property is reassigned reactivity is not triggered. A test repository is here: https://github.com/softwarerero/test-vm-share.

ManuelDeLeon commented 7 years ago

It is a bug. It's not firing the change event for shared (probably mixed too) dependencies that change inside an autorun. I'll fix it.

softwarerero commented 7 years ago

Trying to fix this I'm a little stuck. When I change https://github.com/ManuelDeLeon/viewmodel-react/blob/master/src/viewmodel.js#L96 to ViewModel.shared[key][prop] = ViewModel.prop(content, obj); the property is updated to the new value but I get an error TypeError: c.vmChange is not a function. (In 'c.vmChange()', 'c.vmChange' is undefined).

When I now try to fix this and change https://github.com/ManuelDeLeon/viewmodel-react/blob/master/src/viewmodel.js#L110 to if (c.vmChange !== undefined) c.vmChange(); the property is not updated anymore.

ManuelDeLeon commented 7 years ago

Alright, here's what's happening... ViewModel.share creates the shared properties immediately using whatever Tracker it has at the time. In your case you're adding the share before calling ViewModel.Tracker = Tracker;. So in the end the shared properties are using one tracker while the rest of the app is using another.

Your options are:

1) Move ViewModel.Tracker = Tracker; to the start of the shares.js file. 2) Don't use Meteor's tracker. If you use VM for everything you don't need to call ViewModel.Tracker = Tracker;.

softwarerero commented 7 years ago

Thanks Manuel for looking into this. Both methods seem to have their drwabacks.

  1. No data is loaded on page reload.
  2. Data is loaded on page reload but if I edit an input value it is immediately overwritten by the old value making it effectively read only.
ManuelDeLeon commented 7 years ago

I don't know what you mean. Can you create 2 branches in test-vm-share showing both cases?

Thanks

softwarerero commented 7 years ago

I just found that a scenario that woked for me. If I set ViewModel.Tracker to the Meteor tracker in main.js file and again in shares.js everything works as expected. So I think we can leave this ticekt closed.