gpt4 book ai didi

ember.js - 如何创建 Ember Handlebars helper ?

转载 作者:行者123 更新时间:2023-12-02 06:08:32 24 4
gpt4 key购买 nike

大家好

我尝试创建助手

Handlebars.registerHelper('testCan', function(permissionName, options){
var permission = Ember.Object.extend({
testCan: function(){
debugger;
return true;
}.property()
}).create();

// wipe out contexts so boundIf uses `this` (the permission) as the context
options.contexts = null;

Ember.Handlebars.helpers.boundIf.call(permission, "testCan", options)
});

并用作
{{#testCan read controller=controller}}
<h1>It's works</h1>
{{/testCan}}

我这样做是为了从这里 http://livsey.org/blog/2012/10/16/writing-a-helper-to-check-permissions-in-ember-dot-js/测试模式

但这不起作用((

怎么了? Ember 版本-1.9.1

P.P.S最初我使用现有代码(请参见 Ember.handlebars boundIf didn't call calculated property),但是我在这个示例中尝试重现/解决该问题

最佳答案

如果尝试检查权限/授权,则使用现有的加载项可能是最简单的。我建议使用ember-canember-sanctify(我相信sanctify仅支持1.10及更高版本)。

但是,您尝试执行的操作可能更容易推断组件内部。实际上,我建议创建助手的唯一原因是进行简单转换或能够传递任意数量的args。 Ember可以改进的一件事是帮助用户了解如何在帮助程序内部进行更复杂的操作。

示例组件:
app/templates/components/test-can.hbs:

{{#boundIf hasPermission}}
{{yield}}
{{/boundIf
app/components/test-can.js:
import Ember from 'ember';

export default Ember.Component.extend({
permission: null,
controller: null,

hasPermission: function() {
//implement logic here
}.property('permission', 'controller')
});

使用示例:
{{#test-can permission=read controller=controller}}
Your Content Here
{{/test-can}}

不确定您的示例中的 readcontroller是什么,因此,如果这些变量没有指向任何对象,则不会有太大作用。希望这可以帮助。

更新:

因此,使用 app/helpers/test-can.js创建一个如下所示的文件
export default function(permissionName, options){
var permission = Ember.Object.extend({
testCan: function(){
return true;
}.property()
}).create();

// wipe out contexts so boundIf uses `this` (the permission) as the context
options.contexts = null;

return Ember.Handlebars.helpers.boundIf.call(permission, "testCan", options);
}

测试时不起作用?当然,上面的示例将始终返回true,从而始终产生该块。 1.9.1 still basically uses the same code for the if helper

关于ember.js - 如何创建 Ember Handlebars helper ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29183823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com