Ember CLI + HTMLBars -- 将参数传递给助手
Ember CLI + HTMLBars -- Passing Parameters to a Helper
我正在尝试将参数从我的 HTMLBars 模板传递给助手。
根据文档,我创建了一个助手并明确注册了该助手:
export default Ember.HTMLBars.makeBoundHelper('is-foo', function(value, options) {
console.log("value: "+value);
});
但是我得到一个错误"Error: Assertion Failed: makeBoundHelper generated helpers do not support use with blocks"
所以我尝试按照建议使用 Ember.HTMLBars.helper 和 Ember.HTMLBars.registerHelper here 但我收到错误 "TypeError: Ember.default.HTMLBars.helper is not a function"
如果我没有明确注册助手:
export default function(value, options) {
console.log("value: "+value);
};
然后我可以传递一个参数,但它没有得到解析并注销我传递的内容的文字文本。
所以我尝试了概述的解决方案 here 但它似乎不适用于 CLI
我想要的结果是根据我发送给助手的参数值动态选择一个组件。我的 HTMLBars 代码如下所示:
{{#each foo in model}}
{{is-foo parameter}}
{{a-component}}
{{else}}
{{another-component}}
{{/is-foo}}
{{/each}}
我不确定下一步该做什么。感谢任何帮助。
在 Ember CLI 项目中做类似的事情,因为帮助程序文件名是帮助程序名称
export default Ember.HTMLBars.makeBoundHelper(function(value, options) {
而不是
export default Ember.HTMLBars.makeBoundHelper('is-foo', function(value, options) {
编辑
应@TheCompiler 的要求,这里提供建议作为答案。
像在 HTMLBars 中做一些事情
{{if is_parameter}}
{{a-component}}
{{else}}
{{another-component}}
{{/if}}
在控制器或组件中计算 parameter
属性
is_parameter: function () {
var pm = this.get('parameter');
return (your condition for `pm`)? true : false;
}.property('parameter')
您想要的行为已在 中实现。
如果您想知道 component
助手是如何实现的,您可以在此处查看其源代码:(当前为第 63 行)
我正在尝试将参数从我的 HTMLBars 模板传递给助手。
根据文档,我创建了一个助手并明确注册了该助手:
export default Ember.HTMLBars.makeBoundHelper('is-foo', function(value, options) {
console.log("value: "+value);
});
但是我得到一个错误"Error: Assertion Failed: makeBoundHelper generated helpers do not support use with blocks"
所以我尝试按照建议使用 Ember.HTMLBars.helper 和 Ember.HTMLBars.registerHelper here 但我收到错误 "TypeError: Ember.default.HTMLBars.helper is not a function"
如果我没有明确注册助手:
export default function(value, options) {
console.log("value: "+value);
};
然后我可以传递一个参数,但它没有得到解析并注销我传递的内容的文字文本。
所以我尝试了概述的解决方案 here 但它似乎不适用于 CLI
我想要的结果是根据我发送给助手的参数值动态选择一个组件。我的 HTMLBars 代码如下所示:
{{#each foo in model}}
{{is-foo parameter}}
{{a-component}}
{{else}}
{{another-component}}
{{/is-foo}}
{{/each}}
我不确定下一步该做什么。感谢任何帮助。
在 Ember CLI 项目中做类似的事情,因为帮助程序文件名是帮助程序名称
export default Ember.HTMLBars.makeBoundHelper(function(value, options) {
而不是
export default Ember.HTMLBars.makeBoundHelper('is-foo', function(value, options) {
编辑
应@TheCompiler 的要求,这里提供建议作为答案。
像在 HTMLBars 中做一些事情
{{if is_parameter}}
{{a-component}}
{{else}}
{{another-component}}
{{/if}}
在控制器或组件中计算 parameter
属性
is_parameter: function () {
var pm = this.get('parameter');
return (your condition for `pm`)? true : false;
}.property('parameter')
您想要的行为已在
如果您想知道 component
助手是如何实现的,您可以在此处查看其源代码: