tomitrescak
tomitrescak

Reputation: 1110

How can I assign data context in onCreated

How can I assign data context in onCreated (replace the whole context)? Following does not work:

Template.mine.onCreated(function() {
  this.data = function() { return "MyData"; }
})

While following does:

Template.mine.onCreated(function() {
  this.data.myData = function() { return "MyData"; }
})

I would like to replace the whole context. Is this possible?

Upvotes: 0

Views: 356

Answers (2)

Kuba Wyrobek
Kuba Wyrobek

Reputation: 5273

Cannot you use : Template.dynamic? Docs

{{> Template.dynamic template='TEMPLATE_NAME' data=CONTEXT }}

Upvotes: 1

Peter Sutton
Peter Sutton

Reputation: 1213

this.data is immutable. To replace the data context either wrap your mine template and pass the correct data. E.g.;

<template name="mineWrap">
  {{> mine mydata}}
</template>

Or, store your data directly on the template instance. E.g.;

Template.mine.onCreated(function () {
  this._myData = 'data';
});

Template.mine.helpers({
  myData: function () {
    return Template.instance()._myData;
  }
});

Upvotes: 1

Related Questions