Ember.handlebars boundIf 没有调用计算 属性
Ember.handlebars boundIf didn't call calculated property
我尝试将项目从 Ember cli 0.1.2 迁移到 0.1.15。当前问题是下一个:
我有帮手。它在较旧的 ember 上运行良好,但在较新的版本中,所有 can-blocks 都不存在。 Can-helper 在模型中使用计算的 属性,所以我尝试将调试器设置为计算函数 - 它在 0.1.2 上工作,在 0.1.15
上不工作
原始 ember - 1.7.0-beta.1
尝试迁移到 ember - 1.9.1
我发现 boundIf 助手的实现已更改。有没有人对这种麻烦有一些想法/经验?
P.S。 can-helper 代码如下
var get = Ember.get, isGlobalPath = Ember.isGlobalPath, normalizePath = Ember.Handlebars.normalizePath,
IS_BINDING = Ember.IS_BINDING;
var getProp = function(context, property, options) {
if (isGlobalPath(property)) {
return get(property);
} else {
var path = normalizePath(context, property, options.data);
return get(path.root, path.path);
}
};
export default function(permissionName, property, options) {
var attrs, context, key, path, permission;
// property is optional, if we've only got 2 arguments then the property contains our options
if (!options) {
options = property;
property = null;
}
context = (options.contexts && options.contexts[0]) || this;
attrs = {};
// if we've got a property name, get its value and set it to the permission's content
// this will set the passed in `post` to the content eg:
// {{#can editPost post}} ... {{/can}}
if (property) {
attrs.content = getProp(context, property, options);
}
// if we've got any options, find their values eg:
// {{#can createPost project=project user=App.currentUser}} ... {{/can}}
for (key in options.hash) {
if (!options.hash.hasOwnProperty(key)) {
continue;
}
path = options.hash[key];
if (options.hashTypes[key] === 'STRING') {
if (IS_BINDING.test(key)) {
attrs[key.slice(0, -7)] = getProp(context, path, options);
}
else {
attrs[key] = path;
}
}
else {
attrs[key] = getProp(context, path, options);
}
}
// find & create the permission with the supplied attributes
permission = this.get('container').lookup('permissions:main').get(permissionName, attrs);
// ensure boundIf uses permission as context and not the view/controller
// otherwise it looks for 'can' in the wrong place
options.contexts = null;
// bind it all together and kickoff the observers
return Ember.Handlebars.helpers.boundIf.call(permission, "can", options);
}
在模板中用作
{{#can read model="contact"}}
<div class="panel">
{{#link-to 'contacts' id="nav-to-contacts" class="main-menu-root-item" title="Contacts"}}
Contacts
{{/link-to}}
</div>
{{/can}}
可以计算 属性 获得许可
can: function() {
var model = get(this, 'model'),
field = get(this, 'field'),
permission = rules.findBy('name', model);
return permission && permission.read &&
(field ? permission.read.contains(field) : permission.read.length);
}.property()
在旧版本中,我可以使用调试器语句在其中停止,但更新后 - 不能
观察者更改模型可能会出错,但至少应调用一次(在初始渲染时)——据我所知
获取 属性 的上下文错误的原因
请看这里https://github.com/emberjs/ember.js/issues/10692
我尝试将项目从 Ember cli 0.1.2 迁移到 0.1.15。当前问题是下一个:
我有帮手。它在较旧的 ember 上运行良好,但在较新的版本中,所有 can-blocks 都不存在。 Can-helper 在模型中使用计算的 属性,所以我尝试将调试器设置为计算函数 - 它在 0.1.2 上工作,在 0.1.15
上不工作原始 ember - 1.7.0-beta.1 尝试迁移到 ember - 1.9.1
我发现 boundIf 助手的实现已更改。有没有人对这种麻烦有一些想法/经验?
P.S。 can-helper 代码如下
var get = Ember.get, isGlobalPath = Ember.isGlobalPath, normalizePath = Ember.Handlebars.normalizePath,
IS_BINDING = Ember.IS_BINDING;
var getProp = function(context, property, options) {
if (isGlobalPath(property)) {
return get(property);
} else {
var path = normalizePath(context, property, options.data);
return get(path.root, path.path);
}
};
export default function(permissionName, property, options) {
var attrs, context, key, path, permission;
// property is optional, if we've only got 2 arguments then the property contains our options
if (!options) {
options = property;
property = null;
}
context = (options.contexts && options.contexts[0]) || this;
attrs = {};
// if we've got a property name, get its value and set it to the permission's content
// this will set the passed in `post` to the content eg:
// {{#can editPost post}} ... {{/can}}
if (property) {
attrs.content = getProp(context, property, options);
}
// if we've got any options, find their values eg:
// {{#can createPost project=project user=App.currentUser}} ... {{/can}}
for (key in options.hash) {
if (!options.hash.hasOwnProperty(key)) {
continue;
}
path = options.hash[key];
if (options.hashTypes[key] === 'STRING') {
if (IS_BINDING.test(key)) {
attrs[key.slice(0, -7)] = getProp(context, path, options);
}
else {
attrs[key] = path;
}
}
else {
attrs[key] = getProp(context, path, options);
}
}
// find & create the permission with the supplied attributes
permission = this.get('container').lookup('permissions:main').get(permissionName, attrs);
// ensure boundIf uses permission as context and not the view/controller
// otherwise it looks for 'can' in the wrong place
options.contexts = null;
// bind it all together and kickoff the observers
return Ember.Handlebars.helpers.boundIf.call(permission, "can", options);
}
在模板中用作
{{#can read model="contact"}}
<div class="panel">
{{#link-to 'contacts' id="nav-to-contacts" class="main-menu-root-item" title="Contacts"}}
Contacts
{{/link-to}}
</div>
{{/can}}
可以计算 属性 获得许可
can: function() {
var model = get(this, 'model'),
field = get(this, 'field'),
permission = rules.findBy('name', model);
return permission && permission.read &&
(field ? permission.read.contains(field) : permission.read.length);
}.property()
在旧版本中,我可以使用调试器语句在其中停止,但更新后 - 不能 观察者更改模型可能会出错,但至少应调用一次(在初始渲染时)——据我所知
获取 属性 的上下文错误的原因 请看这里https://github.com/emberjs/ember.js/issues/10692