Skip to content
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

ViewModel.share functions not working #283

Open
cafe4it opened this issue Dec 23, 2016 · 2 comments
Open

ViewModel.share functions not working #283

cafe4it opened this issue Dec 23, 2016 · 2 comments

Comments

@cafe4it
Copy link

cafe4it commented Dec 23, 2016

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
Copy link

satyavh commented Nov 19, 2017

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
Copy link

arggh commented Nov 20, 2017

@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

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