简体   繁体   中英

How to pass object to handlebars helper

Using handlebars-1.1.2 and old emberjs 1.5.1. I've code:

{{#each queue in appController.queues_services_array}}
    <label for="{{GetObjectId queue}}">{{queue.title}}</label></div>
{{/each}}

and helper:

Handlebars.registerHelper('GetObjectId', function(value) {
  return value.id;
});

The problem is that data given to helper is just string "queue", not an object. I wonder how can I pass object "queue" to helper.

You can use the bind-attr helper to bind your value to a specific attribute:

<label {{bind-attr for=queue.id}}>{{queue.title}}</label>

Or alternatively if you do not expect the value to change it is possible to use the unbound helper, which yields some improved performance:

<label for="{{unbound queue.id}}">{{queue.title}}</label>

The best thing you could do would be to update to a newer version of Ember so you could make use of the newer syntax which allows you to use regular Handlebars:

<label for="{{queue.id}}">{{queue.title}}</label>

Assuming you have the time and budget.

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