如何将对象从块辅助程序传递到块状流星中? [英] How to pass an object from to a block helper back to the block in meteor blaze?
问题描述
在流星0.8引起轰动之前,我能够将对象传递给自定义块帮助器内容,如下所示:
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:
我使用了一个blockhelper
I use a blockhelper
{{#blockhelper argument="something"}}
{{somePartOfTheObject}}
{{/blockhelper}}
然后在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现在不允许将对象传递到此模板,因此可以在块内容中使用它.
Now blaze does not allow to pass an object to this template, so it can be used within the block contents.
我该如何解决?
我知道流星将是在控制器中获取对象.但是,由于我想将其用于原型框架,所以我希望能够直接从blockhelper创建一个或多个上述对象,以便有人可以传递将被转换为对象的精子.
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.
推荐答案
该解决方案非常简单.
如果我有助手:
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>
这篇关于如何将对象从块辅助程序传递到块状流星中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!