8

My Ember component looks like this:

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")
});

And the component is called like this:

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

Looks like nothing is printed nor can I use anything in the view. Is something wrong?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Ahmed Abbas
  • 952
  • 11
  • 25

4 Answers4

6

It works if you declare it as a property on your controller as in:

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

And then:

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

In the result above leftSubDetails does not work. It will result in undefined.

Working demo here

Thomas
  • 303
  • 3
  • 14
Kalman
  • 8,001
  • 1
  • 27
  • 45
5

Or you can check ember-composable-helpers

{{#view-users
  users=model
  primaryAction='follow'
  leftSubDetails=(w 'tweets' 'followers' 'following')
  rightSubDetails=(w 'follow' 'reply' 'addList')
}}
{{/view-users}}
mpugach
  • 591
  • 5
  • 15
2

Use addon ember-composable-helpers

ember-composable-helpers has an array defined (and lot's of others too!). Look at the documentation: https://github.com/DockYard/ember-composable-helpers#array

Custom helper

You can also define a generic helper for creating via .hbs files. Run command

$ ember g helper arr

And then the hbs call would be:

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

Reference: Define array inside template for handlebars/ember?

morhook
  • 685
  • 7
  • 19
0

You can also pass the component a JSON string and then use JSON.parse() in your logic. Sort of cheating but prevents adding additional logic

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