ManuelDeLeon / viewmodel

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

Cannot access Meteor.user in property declaration #196

Closed avalanche1 closed 8 years ago

avalanche1 commented 8 years ago

Template.name.viewmodel({property:Meteor.userId()}) returns undefined. Even if I re-run this from console after template is fully rendered.

ManuelDeLeon commented 8 years ago

That's because you're invoking the method when the app is loaded. Don't use the parentheses for userId (haven't tried it but it should work) On Mar 2, 2016 7:13 AM, "Artem Saffron" notifications@github.com wrote:

Template.name.viewmodel({property:Meteor.userId()}) returns undefined. Even if I call this from console after template is fully rendered.

— Reply to this email directly or view it on GitHub https://github.com/ManuelDeLeon/viewmodel/issues/196.

avalanche1 commented 8 years ago

No, I have this template appear only after I click a button - my app is already fully loaded by that time. And how can I not user parentheses? Meteor.userId is a function, is it not?

ManuelDeLeon commented 8 years ago

Yes it's a function but you're invoking the function when Meteor loads the app, not when the template is instantiated. Replace Meteor userId with console log to see what I mean. On Mar 3, 2016 2:33 AM, "Artem Saffron" notifications@github.com wrote:

No, I have this template appear only after I click a button - my app is already fully loaded by that time. And how can I not user parentheses? Meteor.userId is a function, is it not?

— Reply to this email directly or view it on GitHub https://github.com/ManuelDeLeon/viewmodel/issues/196#issuecomment-191677983 .

avalanche1 commented 8 years ago

Ok, I've fixed it with

Template.name.viewmodel({
  load:{
    property: Meteor.userId()
  }
})

But is there a way to do it without load??

ManuelDeLeon commented 8 years ago

Use a function that returns the userId On Mar 3, 2016 6:37 AM, "Artem Saffron" notifications@github.com wrote:

Ok, I've fixed it with

Template.name.viewmodel({ load:{ property: Meteor.userId() } })

But is there a way to do it without load??

— Reply to this email directly or view it on GitHub https://github.com/ManuelDeLeon/viewmodel/issues/196#issuecomment-191763389 .

ManuelDeLeon commented 8 years ago

btw, if all you're doing is returning the userId then it's better to put it in a mixin so you can use it on other templates.