简体   繁体   English

将数据传递给 Ember Octane 组件时不需要的双向数据绑定

[英]Unwanted two-way data binding when passing data to Ember Octane component

I'm following the Full Stack Ember with Rails course on Embercasts .我正在关注Embercasts上的 Full Stack Ember with Rails 课程。 As they don't use Octane yet, I change the code where necessary to work with Ember 3.22.由于他们尚未使用 Octane,因此我在必要时更改了代码以使用 Ember 3.22。 I'm stuck in video 22 where I need to pass data to a component.我被困在视频 22 中,我需要将数据传递给组件。 This data should only be passed to the component, but when updating a value inside it, I don't want to see the change in other places.这个数据应该只传递给组件,但是在更新其中的值时,我不想看到其他地方的变化。

In the video, this is taken care of in a didReceiveAttrs() handler, but this is not available in Octane.在视频中,这是在didReceiveAttrs()处理程序中处理的,但这在 Octane 中不可用。 Instead, the Ember docs describe one way data flow as what happens automatically.相反,Ember 文档将一种数据流描述为自动发生的事情。 In my case, it doesn't:就我而言,它不会:

{{! author.edit.hbs !}}

<h3>Editing: {{model.last}},  {{model.first}}</h3>
<AuthorForm @author={{model}} />

{{! author-form.hbs !}}

<div class="field">
  <label for="first">First name</label>
  <Input @id="first" type="text" placeholder="First name" @value={{this.author.first}}/>
</div>  

<div class="field">
  <label for="last">Last name</label>
  <Input @id="last" type="text" placeholder="Last name" @value={{this.author.last}}/>
</div>

The h3 updates whenever I change the value in one of the component's inputs.每当我更改组件输入之一中的值时, h3就会更新。 What's wrong here?这里有什么问题?

The <Input> component shipped with Ember.js uses two-way data binding for the value. Ember.js 附带的<Input>组件对值使用双向数据绑定。

Arguments passed to a component are immutable.传递给组件的参数是不可变的。 You can not change this.args nor a value passed with @ .您不能更改this.args或通过@传递的值。 But object passed in as values of arguments are not frozen.但是作为参数值传入的对象不会被冻结。

Taking this template as an example:以这个模板为例:

<Input @value={{@post.title}}/>

It will not mutate @post but the title property of the object passed in as @post argument.它不会改变@post而是改变作为@post参数传入的对象的title属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM