如何在组件类中访问传递给 Ember 组件的属性? [英] how to access properties passed to Ember Component inside a component-class?

查看:18
本文介绍了如何在组件类中访问传递给 Ember 组件的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模板中声明了一个 emberjs 组件:

I have DECLARED an emberjs component in template as:

<script type="text/x-handlebars" id="components/custom-component">      
  <h4>The name of the object passed is : {{object.name}}</h4>
  {{#if show_details}}
    <p>The details of the object passed are : {{object.details}}</p>
  {{/if}}
</script>

现在我在我的 html 模板中使用这个组件:

Now I am USING this component in my html template as :

<script type="text/x-handlebars" data-template-name="index">
  <ul>
    {{#each object in model}}
      <li>{{custom-component object=object}}</li>
    {{/each}}
  </ul>
</script>

我的自定义组件的组件类如下所示:

My component class for custom-component is as shown below :

App.CustomComponentComponent = Ember.Component.extend({
  show_details: function() {
      // return true or false based on the OBJECT's property (e.g. "true" if object.type is 'AUTHORIZED')
  },
});

更新

我实现的方法如下:

App.CustomComponentComponent = Ember.Component.extend({
    show_details: function() {
      var object = this.get('object');
      if (object.type == "AUTHORIZED")
        return true;
      else
        return false;
    }
});

传递给组件类的参数可以使用它的 get 方法获得.

The parameters passed to the component class are available using it's get methods.

推荐答案

应该这样工作:

{{custom-component object_name=object}}

(您只是使用了错误的属性名称).

(you just used the wrong property name).

如果 object 是对象名称,这应该可以工作.如果 name 是 object 的属性,则使用 object.name.

This should work if object is the object name. If name is a property of object then use object.name.

更新

这应该很简单.show_details 应根据 object type 定义为计算属性:

This should be straightforward. show_details should be defined as computed property depending on the object type:

App.CustomComponentComponent = Ember.Component.extend({
    object: null,
    show_details: function() {
      var object = this.get('object');
      if (object.get('type') === "AUTHORIZED")
         return true;
      else
         return false;
    }.property('object.type') 
});

或更简单:

App.CustomComponentComponent = Ember.Component.extend({
    show_details: function() {
      return this.get('object').get('type') === "AUTHORIZED";
    }.property('object.type') 
});

在访问 Ember 对象的属性时不要忘记使用 get.

Don't forget to use get when accessing the properties of an Ember object.

这篇关于如何在组件类中访问传递给 Ember 组件的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆