简体   繁体   中英

Ember opposite of positionalParams

In an Ember Component it is possible to define a positionalParams property so that parameters passed as positional params become available as properties. Eg:

let MyComponent = Ember.Component.extend;
MyComponent.reopenClass({
  positionalParams: ['name', 'age']
});

when it is invoked: {{my-component "John" 38}} the property name has the value John and de property age has the value 38 .

I would like to have the opposite behavior but I cannot find whether this is possible. The behavior I'm looking for is like following:

Instead of passing a number of positional params ( {{my-comp param1 param2}} ) I would like to pass an array property ( {{my-comp positionalArguments=myArray}} ) because it can be of a dynamic size.

I'm not only looking for this behavior at components, but also helpers: {{ concat firstName " " lastName }} should become {{ concat positionalArguments=myArray }} . The helper should get the same params-array as first argument in both cases.

UPDATE After my misunderstood is resolved:

You want to have a spread operator . You may look this discussions 1 , 2 . For now, you can get the array and do the assignment at init. Also have a look at this SO discussion .

OLD Answer

So don't use array brackets in your positional parameter definition:

let MyComponent = Ember.Component.extend;
MyComponent.reopenClass({
  positionalParams: 'myparams'
});

Use it like that:

{{my-component 2 'a' 'b'}}
{{my-component 2 'a' 'b' 4 'c' 7}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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