Provides a consistent this
to all cosmos:blaze-profiles functions.
Added options for all functions so this functionality requires specifying this:true
in the options. Then, all existing uses of the standard functions are not affected.
meteor add cosmos:blaze-this
The this
always has:
template
- the template instance,Blaze.TemplateInstance
data
- the current non-reactive data objectgetData
- reactive access to current data and parent data contextsTemplate
- theBlaze.Template
object, for example:Template.theName
Conditionally has:
event
- when it's an event listenerargs
- when it's a helper, event listener, or afunctions
function receiving argumentshash
- when it's a helper and an object is provided to the helper. Thehash
property is extracted from theSpacebars.kw
argument and provided in thethis
; theSpacebars.kw
object is removed from the argssubscribe
- when it's a lifecycle listener (created and rendered)autorun
- when it's a lifecycle listener (created and rendered)- functions specified via the
template.functions()
function or an added profile
The this
will be as described above for all functions added via:
Template.profiles()
Template.helpers()
Template.events()
Template.onCreated()
Template.onRendered()
Template.onDestroyed()
Template.functions()
For example:
# instead of passing a function, pass an object which contains `this` and `fn`
Template.onCreated this:true, fn: ->
object = SomeCollection.findOne this.data.objectId
this.template.$myObject = object
# provide options with this:true as `$options`
Template.helpers
$options: this:true
name: -> this.template.$myObject.name
# provide options with this:true as `$options`
Template.events
$options: this:true
name: -> this.template.$myObject.name
# instead of passing a function, pass an object which contains `this` and `fn`
Template.onDestroyed this:true, fn: ->
delete this.template.$myObject
Traditionally the this
and other objects are done differently for different types. Describe:
-
Events:
- this = the current
Blaze.TemplateInstance
data context - event - the first function arg
- template - the second function arg
- data (non-reactive) =
this
- data (reactive) =
Template.currentData()
andTemplate.parentData()
- Template - template.view.template is the
Blaze.Template
- this = the current
-
Helpers:
- this = the current
Blaze.TemplateInstance
data context - template =
Template.instance()
- data (non-reactive) = the
this
- data (reactive) =
Template.currentData()
andTemplate.parentData()
- Template -
Template.instance().view.template
is theBlaze.Template
- this = the current
-
Lifecycle handlers:
- this = the current
Blaze.TemplateInstance
- template =
this
- data (non-reactive) =
this.data
- data (reactive) =
Template.currentData()
andTemplate.parentData()
- Template -
this.view.template
is theBlaze.Template
- this = the current
There is no additional API. It alters the inner workings of cosmos:blaze-profiles
to call functions with the this
described above.
Well, there's one function to explain which is available in the this
:
To reactively get data contexts we normally use Template.currentData()
and Template.parentData(number)
.
Those two functionalities are combined via getData(number)
. It calls Template.parentData
with a default number of zero.
So:
getData(0)
=Template.currentData()
getData(1)
=Template.parentData()
andTemplate.parentData(1)
getData(2)
=Template.parentData(2)
getData(3)
=Template.parentData(3)
- and so on...