ManuelDeLeon / viewmodel-react

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

Autorun not working on scoped shared ViewModel #22

Closed davidsavoie1 closed 6 years ago

davidsavoie1 commented 7 years ago

Hi Manuel,

Autorun in a shared ViewModel doesn't get triggered if the sharing is scoped in a component. Here's an example:

ViewModel.share(
test: {
    arr: ['foo', 'bar'],
    new: '',
    addNew() {
        this.arr().push(this.new());
    },
    autorun() {
      this.arr().depend();
      console.log("Added 1");
      console.log(this.arr());
      this.new.reset();
    },
  }
});
App({
  share: {
    test: 'test'
  },
  render() {
      <input b="value: test.new, enter: test.addNew" />
  }
});

When I enter something in the input, nothing happens...

However, if I change the way I introduce the shared ViewModel and I don't scope it, the autorun gets triggered.

App({
  share: 'test',
  render() {
      <input b="value: new, enter: addNew" />
  }
});

I've been wondering for quite a while what was wrong with my code and the reactive arrays! I'd be glad if you could take a look at this issue.

ManuelDeLeon commented 7 years ago

I think there might be something wrong with the way "this" is bound for autoruns + share + scoped. I'll take a look.

btw, I fixed the throttle issue but haven't published it yet because I'm refactoring a few things.