user1795109
user1795109

Reputation: 716

Apply function inside bindAttr

I need to apply a function to the property value. I use following code to bind it with an inputbox.

{{bindAttr value="ticket.t_date"}}

I have a helper function as follow

Ember.Handlebars.helper('date', function(unixDate){ return unixToDate(unixDate); });

I need to use this function inside bindAttr.

Upvotes: 1

Views: 1876

Answers (1)

intuitivepixel
intuitivepixel

Reputation: 23322

Calling a helper inside bind-attr is not possible, but you could do it in another way, define a computed property on your model class that makes use of your date formatting helper function as a normal function call:

App.Ticket = DS.Model.extend({
  t_date: DS.attr('string'),
  formattedDate: function () {
    return unixToDate(this.get('t_date'));
  }.property('t_date')
});

and then use the formattedDate in your bind-attr:

{{#each ticket in model}}
  <input type="text" {{bindAttr value="ticket.formattedDate"}}></input>
{{/each}}

See here for a demo of what I mean: http://jsbin.com/UfiSote/5/edit

Update in response to your comment

Yes it's possible, you then rather need to bind to your ticket property:

App.MyController = Ember.ObjectController.extend({
  ticket: null,
  formattedDate: function () {
    return unixToDate(this.get('ticket.t_date'));
  }.property('ticket')
});

Hope it helps.

Upvotes: 5

Related Questions