问题描述
我试图将Ember cli 0.1.2的项目迁移到0.1.15。目前的问题是下一步:我有帮忙。它可以在较旧的ember工作正常,但是在较新的版本中,所有的can-block都不存在。可以帮助使用模型中的计算属性,所以我尝试将调试器设置为计算功能 - 它工作在0.1.2,并且不工作在0.1.15
原始ember - 1.7.0-beta.1
尝试迁移到ember - 1.9.1
因为我发现boundIf帮助器的实现被改变了。有什么想法/经验有这样的麻烦吗?
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);
}
};
导出默认功能(permissionName,property,options){
var attrs,context,key,path,permission;
//属性是可选的,如果我们只有2个参数,那么该属性包含我们的选项
if(!options){
options = property;
property = null;
}
context =(options.contexts&& options.contexts [0])||这个;
attrs = {};
//如果我们有一个属性名称,获取其值并将其设置为权限的内容
//这将将post中的传递设置为内容,例如:
// {{#can editPost post}} ... {{/ can}}
if(property){
attrs.content = getProp(context,property,options);
}
//如果我们有任何选项,可以找到它们的值,例如:
// {{#can CreatePost =项目用户= 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&使用提供的属性创建权限
permission = this.get('container')。lookup('permissions:main')。get(permissionName,attrs);
//确保boundIf使用权限作为上下文,而不是view / controller
//否则在错误的地方查找'can'
options.contexts = null;
//将它们绑定在一起并启动观察者
返回Ember.Handlebars.helpers.boundIf.call(权限,可以,选项);
}
在模板中用作
{{#can read model =contact}}
< div class =panel>
{{#link-to'contacts'id =nav-to-contactsclass =main-menu-root-itemtitle =Contacts}}
联系人
{ {/ link-to}}
< / div>
{{/ can}}
可以是权限的计算属性
可以:function(){
var model = get(this,'model'),
field = get ,'field'),
permission = rules.findBy('name',model);
返回权限&& permission.read&&&
(field?permission.read.contains(field):permission.read.length);
} .property()
在旧版本中,我可以使用调试器语句停止,但是在更新之后 - 不能
这可能是观察者对模型的更改的错误,但是至少应该调用一次(在初始化渲染) - 我明白
获取财产的错误上下文的原因
请看这里
I try to migrate project form Ember cli 0.1.2 to 0.1.15. Current problem is next:
I have can-helper. It works fine at older ember but at newer version all can-blocks are absent. Can-helper use calculated property in model, so i try to set debugger into calculation function - it work on 0.1.2 and didn't work at 0.1.15
Original ember - 1.7.0-beta.1Try migrate to ember - 1.9.1
As i found implementation of boundIf helper was changed. Have anybody some ideas / experience with such trouble?
P.S. code of can-helper is below
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);
}
in template it used as
{{#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 is computed property at permission
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()
At older version i can stop inside it with debugger statement, but after updating - can'tIt's possible errors on observers changes of model, but it should be called at least once ( on initial rendering ) - as i understand
The reason in wrong context for getting propertyPlease look here https://github.com/emberjs/ember.js/issues/10692
这篇关于Ember.handlebars boundIf没有调用计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!