[英]Ember.js delay in passing object properties to child component after setting them in parent
I'm working in Ember 3.1.4. 我正在使用Ember 3.1.4。
In keeping with the DDAU pattern, I have a set setProperty
action in my controller, which simply accepts the property name and the value to set it to, and then does so. 与DDAU模式保持一致,我在控制器中设置了
setProperty
操作,该操作仅接受属性名称和将其设置为的值,然后执行此操作。
I then pass the action into my component, and I can call it to set a property at the controller level, and then pass the property back down into the component. 然后,我将动作传递到组件中,然后可以调用它以在控制器级别设置属性,然后再将该属性传递回组件中。
When setting a property to a string, it works as expected and if I get the property immediately after calling the setProperty
action, it returns the new value (As when setting simpleProp
below). 将属性设置为字符串时,它可以按预期工作,并且如果我在调用
setProperty
操作后立即获得该属性,它将返回新值(与下面设置simpleProp
时simpleProp
)。
Strangely, when I use setProperty
to set a property to an empty object, or to set a child property on an object, the property returns undefined
if I get it immediately after calling the action, but returns the new value if I use a setTimeout of 1 millisecond. 奇怪的是,当我使用
setProperty
将属性设置为空对象或在对象上设置子属性时,如果我在调用操作后立即获得属性,则该属性返回undefined
,但是如果我使用setTimeout,则返回新值1毫秒。
What causes this, and is there a way I can prevent this from happening? 是什么原因造成的,有什么办法可以防止这种情况的发生?
Setting a property to an empty object 将属性设置为空对象
Controller 控制者
setProperty: function(property, value) { this.set(property, value); },
my-component.js my-component.js
didInsertElement() { this.setProperty('parentObject', {}); console.log(this.get('parentObject')); // undefined setTimeout(() => { console.log(this.get('parentObject')); // {} }, 1); this.set('simpleProp', 'foo'); console.log(this.get('simpleProp')); // 'foo' }
Template 模板
{{my-component simpleProp=simpleProp parentObject=parentObject setProperty=(action "setProperty")}}
Setting a child property on an existing object 在现有对象上设置子属性
Controller 控制者
init() { this._super(...arguments); this.parentObject = {}; } setProperty: function(property, value) { this.set(property, value); },
my-component.js my-component.js
didInsertElement() { this.setProperty('parentObject.childProp', 'bar'); console.log(this.get('childProp')); // undefined setTimeout(() => { console.log(this.get('childProp')); // 'bar' }, 1); this.set('simpleProp', 'foo'); console.log(this.get('simpleProp')); // 'foo' }
Template 模板
{{my-component simpleProp=simpleProp childProp=parentObject.childProp setProperty=(action "setProperty")}}
What you are trying to do is against Ember's design pattern because Controllers are considered singletons. 您试图做的是违反Ember的设计模式的,因为控制器被视为单例。 The guides state that:
指南指出:
Controllers are singletons so we should avoid keeping state that does not derive from either the Model or Query Parameters since these would persist in between activations such as when a user leaves the Route and then re-enters it.
控制器是单例的,因此我们应避免保持既不是从模型参数也不从查询参数派生的状态,因为这些状态将在激活之间保持不变,例如当用户离开路线然后重新输入路线时。
Because of Ember's bi-directional data flow, you should simply be able to this.set()
in the component and completely get rid of this.setProperty()
in the controller. 由于Ember的双向数据流的,你应该简单地能够
this.set()
的组件和完全摆脱this.setProperty()
控制器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.