ManuelDeLeon / viewmodel

MVVM for Meteor
https://viewmodel.org
MIT License
205 stars 23 forks source link

ViewModel.share functions not working #283

Open cafe4it opened 7 years ago

cafe4it commented 7 years ago

Hi, i try functions onCreated, autorun but not working

ViewModel.share({
  clock: {
    initialTime: new Date(),
    onCreated() {
      this.initialTime( new Date() );
      console.log('onCreated:' , this.initialTime())
    },
    autorun(){
      console.log('autorun:' , this.initialTime())
    }
  }
});

Template.example1.viewmodel({
  share: 'clock'
});
Template.example2.viewmodel({
  share: 'clock'
});
satyavh commented 7 years ago

This is indeed not working at all. If you try to do this:

Template.example2.viewmodel({
  share: 'clock'

  onCreated: function() {
     console.log(this.clock);
  }
});

it will throw an error saying _this.clock is not a function

arggh commented 7 years ago

@satyavh your code is incorrect, you should re-read the documentation of ViewModel.

share: 'clock' specifies which ViewModel Share you want to use in that vm. It's not a property in itself.

So instead, you should have something like this:

Template.example2.viewmodel({
  share: 'clock'

  onCreated: function() {
     console.log(this.initialTime());
  }
});

@cafe4it I wrote a repro with basically your code and all seems fine and working. What's the actual issue you are having?

https://github.com/arggh/viewmodel-share-test