How to pass an object from to a block helper back to the block in meteor blaze?

让人想犯罪 __ 提交于 2019-12-11 07:17:44

问题


Before Meteor 0.8, that brought blaze, I was able to pass objects to a custom block helper content like this:

return options.fn(object)

Now that with blaze, block helpers require to return a template something like this.

return Template.someTemplate

How can I achieve the old behaviour. What I want to to is the following:

I use a blockhelper

{{#blockhelper argument="something"}}
   {{somePartOfTheObject}}
{{/blockhelper}}

Then in the the blockhelper definition, I want to do some stuff based on the argument, that will return an object.

UI.registerhelper "blockhelper", () ->
   object = getStuffFrom(this.argument)
   return Template.someTemplate(object)

As needed by blaze I created a minimal template

<template name="someTemplate">
    {{> UI.contentBlock}}
</template>

Now blaze does not allow to pass an object to this template, so it can be used within the block contents.

How can I get around this?

I know the meteor-way would be to get the object in the controller. But as I want to use this for a prototyping framework, I want to be able to create said object or objects directly from the blockhelper, so someone can pass argmuents that will get converted to objects.


回答1:


The solution turns out to really simple.

If I have a helper:

UI.registerhelper "blockhelper", () ->
   object = getStuffFrom(this.argument)
   return Template.someTemplate

The variables and object of the helper are actually available in the template. So you simply do:

<template name="someTemplate">
    {{> UI.contentBlock object}}
</template>


来源:https://stackoverflow.com/questions/23854851/how-to-pass-an-object-from-to-a-block-helper-back-to-the-block-in-meteor-blaze

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!