Emberjs - 如果后者不在模板上,如何从计算属性设置另一个属性? [英] Emberjs - how to set another property from computed property when the latter is not on the template?
问题描述
这是从我的以前的问题,认为它应该有不同的职位。
我将数据子集设置为不同的属性,并在模板上提供(和更新)。所以我以为我要创建一个空的属性,只需要设置
其值。
更新的模板: p>
{{#each test12}}
{{businessname}}
{{/ each}}
控制器:
test12:[],
test11:function(){
var bms = this.get('businessmatches')。filterBy('type','hardware');
this.set('test12',bms);
return bms;
} .property('businessmatches。@ each。[type]'),
问题是这不行。有趣的是,如果我将模板更改为
{{#each test12}}
{{businessname} }
{{/ each}}
< hr />
{{#each test11}}
{{businessname}}
{{/ each}}
然后它的工作原理! :o
我可以隐藏第二部分在显示:none;
div或我可以直接使用逻辑在 test12
,但行为是一个完全惊喜(我认为集
起初没有工作)。我错过了什么吗?是否有正确的方法?
一般来说,计算属性中的副作用(在您的情况下,设置 test12
)最好避免。他们使程序难以推理。
在你的情况下,问题是只是指 test12
不计算任何东西。 Ember无法知道 test12
正在 test11
中设置,并且可能需要运行它。当您从模板中引用 test11
时,它将被计算,其副作用是设置 test12
,该更新模板(这样快,你看不到它;为了测试这个,你可以在 test12 $之间添加一个
{{debugger}}
c $ c>和 test11
模板的部分,当您在调试器中停止时,您会注意到 test12
部分仍然是空的)
根据你想要完成的任务,你可以像 test12这样做:function(){return this获得( 'TEST11'); } .property('test11')
或 test12:Ember.computed.alias('test11')
,这是一样的。 / p>
This is a follow-up from my previous question, thought it deserves a different post.
I what to set a data subset to a different property and have it available (and update-able) on the template. So I thought I 'd create an empty property and simply set
its value.
The updated template:
{{#each test12}}
{{businessname}}
{{/each}}
The controller:
test12: [],
test11: function(){
var bms = this.get('businessmatches').filterBy('type', 'hardware');
this.set('test12', bms);
return bms;
}.property('businessmatches.@each.[type]'),
The problem is that this does not work. The interesting thing is that if I change the template to
{{#each test12}}
{{businessname}}
{{/each}}
<hr/>
{{#each test11}}
{{businessname}}
{{/each}}
then it works!!! :o
I could hide the second part on a display:none;
div or I could have the logic directly on test12
but the behavior was a complete surprise (I thought set
didn't work at first). Am I missing something? Is there a proper way of doing it?
In general, side effects in computed properties (in your case, setting test12
) are best avoided. They make the program hard to reason about.
In your case, the problem is that merely referring to test12
does not compute anything. Ember has no way to know that test12
is getting set inside test11
and that it might need to run that. When you then refer to test11
from the template, it is computed, with the side effect of setting test12
, which updates the template (so fast you can't see it; to test this, you could add a {{debugger}}
between the test12
and test11
parts of the template, and when you stop in the debugger, you will notice that the test12
part is still empty).
Depending on what you are trying to accomplish, you could do something like test12: function() { return this.get('test11'); }.property('test11')
, or test12: Ember.computed.alias('test11')
, which is the same thing.
这篇关于Emberjs - 如果后者不在模板上,如何从计算属性设置另一个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!