如何将数组属性传递给Ember组件?

时间:2015-01-23 15:07:21

标签: ember.js ember-data ember-cli

我的Ember组件如下所示:

import Ember from 'ember';

export default Ember.Component.extend({
  users: undefined,
  primaryAction: 'follow',
  leftSubDetails:  [],
  rightSubDetails:  [],

  onInitialization: function(){
    console.log('this', this);
    console.log('right',this.rightSubDetails);
    console.log('rightdetail', this.get('rightSubDetails'));
  }.on("init")
});

组件的调用如下:

{{#view-users
  users=model
  primaryAction='follow'
  leftSubDetails=['tweets', 'followers', 'following']
  rightSubDetails=['follow', 'reply', 'addList']
}}
{{/view-users}}

看起来什么也没有打印,也不能在视图中使用任何东西。有什么不对吗?

4 个答案:

答案 0 :(得分:4)

如果您将其声明为控制器上的属性,则可以使用它:

App.IndexController = Ember.ArrayController.extend({
  details: ['follow', 'reply', 'addList']
});

然后:

{{#view-users
    users=model
    primaryAction='follow'
    leftSubDetails=['tweets', 'followers', 'following']
    rightSubDetails=details
}}
{{/view-users}}

在上面的结果leftSubDetails 不起作用。这将导致undefined

工作演示here

答案 1 :(得分:4)

或者您可以查看ember-composable-helpers

{{#view-users
  users=model
  primaryAction='follow'
  leftSubDetails=(w 'tweets' 'followers' 'following')
  rightSubDetails=(w 'follow' 'reply' 'addList')
}}
{{/view-users}}

答案 2 :(得分:2)

使用addon ember-composable-helpers

ember-composable-helpers 定义了数组(还有很多其他人!)。查看文档:{​​{3}}

自定义助手

您还可以定义用于创建via .hbs文件的通用帮助程序。 运行命令

$ ember g helper arr

然后hbs调用将是:

{{#view-users
  users=model
  primaryAction='follow'
  leftSubDetails=(arr 'tweets' 'followers' 'following')
  rightSubDetails=(arr 'follow' 'reply' 'addList')
}}
{{/view-users}}

参考:https://github.com/DockYard/ember-composable-helpers#array

答案 3 :(得分:0)

您还可以将组件传递给JSON字符串,然后在逻辑中使用JSON.parse()。作弊的排序,但阻止添加额外的逻辑

{{#view-users
     users=model
     primaryAction='follow'
     leftSubDetails='["tweets", "followers", "following"]'
     rightSubDetails='["follow", "reply", "addList"]'
}}
{{/view-users}}