简体   繁体   English

在ember.js中,如何在3个组件之间绑定属性?

[英]In ember.js, how can I bind an attribute across 3 components?

My JS file for my-textfield looks like this: 我的my-textfield JS文件如下所示:

import layout from '../templates/components/my-textfield';

export default Ember.component.extend({
  id: '',
  layout,
  tagName: ''
});

My handlebars file for my-textfield looks like this: my-textfield车把文件如下所示:

<div>
  {{my-textfield/input id=id}}
  {{my-textfield/label for=id}}
</div>

My JS file for my my-textfield/input looks like this: my-textfield/input JS文件如下所示:

export default Ember.TextField.extend({});

If I use my component with an ID {{my-textfield id="rawr"}} , I get the following output: 如果我使用ID为{{my-textfield id="rawr"}}组件,则会得到以下输出:

<div>
  <input id="rawr" class="ember-text-field ember-view">
  <span class="ember-view" for="rawr"></span>
</div>

If I don't pass an ID into my component {{my-textfield}} , Ember.TextField will generate its own ID. 如果我没有将ID传递到组件{{my-textfield}} ,则Ember.TextField将生成自己的ID。

<div>
  <input id="ember3972" class="ember-text-field ember-view">
  <span class="ember-view" for></span>
</div>

I'm trying to make it so when the ID gets generated, that the for attribute in the span will be updated with the same value. 我正在尝试使其生成ID时,将使用相同的值更新范围中的for属性。

I expected it to happen automatically since they all point to the same property on my component. 我希望它会自动发生,因为它们都指向我组件上的相同属性。 But since it didn't, I've been trying to figure out how bind them all together for a while. 但是由于没有,我一直试图找出如何将它们绑定在一起。 I found out about the mut helper and tried using that like so: 我发现了有关mut助手的信息,并尝试像这样使用它:

<div>
  {{my-textfield/input id=(mut id)}}
  {{my-textfield/label for=id}}
</div>

but that didn't work. 但这没用。 That just made all of my IDs in every component be the same. 这只是使我在每个组件中的所有ID都相同。

What do I need to do here to make this work? 我需要在这里做什么才能完成这项工作?

It is a very interesting question. 这是一个非常有趣的问题。 I think I can solve your problem but I'm not sure how does your my-textfield component works but here is how I would solve this. 我想我可以解决您的问题,但是我不确定您的my-textfield component如何工作,但这是我将如何解决此问题。 I would have a component my-textfield with following .hbs file: 我将使用以下.hbs文件创建组件my-textfield

<div>
  <input id={{id}} />
  <label for={{id}}></label>
</div>

And just pass id to this component while calling it. 并在调用它时将id传递给此组件。

{{my-textfield id="someId"}}

Now, lets say for some reason you are not able to take above mentioned approach and you want to proceed like: 现在,让我们说由于某种原因,您无法采用上述方法,而您想要像这样继续:

<div>
  {{my-textfield/input id=id}}
  {{my-textfield/label for=id}}
</div>

You can either use jQuery to get ID of the sibling element, I would assume you know how. 您可以使用jQuery获取同级元素的ID ,我假设您知道如何使用。 Or you can access the id for the component using this.elementId in the component.js or as {{this.elementId}} in component.hbs , you should then be able to pass around this value and use it as required. 或者您也可以访问id使用组件this.elementIdcomponent.js{{this.elementId}}component.hbs ,您应该然后能够通过围绕该值,并把它作为要求。

Your my-textField does not have a tagName ; 您的my-textField没有tagName which means ember will not generate an id for it; 这意味着ember将不会为其生成ID; hence when you do not pass an id attribute to it the following does not make any sense: 因此,当您不向其传递id属性时,以下内容将毫无意义:

  {{my-textfield/input id=id}}
  {{my-textfield/label for=id}}

id is not defined; id未定义; what are you passing to the children components? 您要传递给子组件什么? Simply an undefined value. 只是一个未定义的值。

Please check what I did in the following twiddle . 请检查什么,我在下面做了玩弄 I defined my-textfield to have a div tag and let ember generate an id for it. 我将my-textfield为具有div标记,并让ember为此生成一个ID。 Afterwards, I declared a computed property ( idToPass ) within my-textfield.js file and pass that as id of my-textfield/my-input component and as for of my-textfield/my-label component within my-textfield.hbs file. 后来,我宣布一个计算属性( idToPass内) my-textfield.js文件和传递,作为idmy-textfield/my-input组件和formy-textfield/my-label组件内my-textfield.hbs文件。 By this way; 这样 I made sure a controlled id is assigned to my-input via Ember's auto generated id for my-textfield and the same value is passed to my-label as for attribute. 我确信一个控制id分配给my-input通过Ember的自动生成的ID为my-textfield和相同的值传递给my-label作为for属性。

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

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