在bindAttr中应用函数

use*_*109 1 ember.js

我需要将一个函数应用于属性值.我使用以下代码将其与输入框绑定.

{{bindAttr value="ticket.t_date"}}
Run Code Online (Sandbox Code Playgroud)

我有一个帮手功能如下

Ember.Handlebars.helper('date', function(unixDate){ return unixToDate(unixDate); });
Run Code Online (Sandbox Code Playgroud)

我需要在bindAttr中使用这个函数.

int*_*xel 5

在内部调用帮助程序bind-attr是不可能的,但是你可以用另一种方式执行它,在模型类上定义一个计算属性,它使用日期格式化辅助函数作为普通函数调用:

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

然后使用formattedDate你的bind-attr:

{{#each ticket in model}}
  <input type="text" {{bindAttr value="ticket.formattedDate"}}></input>
{{/each}}
Run Code Online (Sandbox Code Playgroud)

请参阅此处了解我的意思:http://jsbin.com/UfiSote/5/edit

更新以回应您的评论

是的,有可能,你需要绑定到你的ticket财产:

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

希望能帮助到你.