把手帮手 - 对象不工作

时间:2013-02-19 12:48:06

标签: ember.js handlebars.js

我正在遍历一个数组控制器来显示项目 - 在这个视图中我试图用当前地址的上下文执行一个自定义Handlebar助手 - 但是在我的帮助器中,该对象是未定义的。

查看:

{{#each temporaryUser.addresses}}
    {{#view App.AddressView addressBinding="this"}}
        {{addressActions view.address}}
        {{{view.address.display}}}
        <a {{action deleteAddress target="view"}} class="delete">Delete</a>
    {{/view}}
 {{/each}}

Handlebars.registerHelper('addressActions', function (address) {
    var display = '<div class="actions">';

    if (address.current && address.permanent) {
        display += '<p>This is my current residential, and permanent address:</p>';
    }
    else if (address.current && !address.permanent) {
        display += '<p>This is my current residential address:</p>';
        display += '<a href="#">Make permanent residential address</a>';
    }
    else if (!address.current && address.permanent) {
        display += '<p>This is my permanent address:</p>';
        display += '<a href="#">Make current residential address</a>';
    }
    else {
        display += '<a href="#">Make current residential address</a><br/>';
        display += '<a href="#">Make permanent residential address</a>';
    }
    display += '</div>';

    return new Handlebars.SafeString(display);
});

1 个答案:

答案 0 :(得分:0)

这不是助手的工作方式。您需要自己从选项对象中获取上下文:

Ember.Handlebars.registerHelper('addressActions', function(path, options) {
  // passed context objects are stored in options.contexts
  // as you are only passing one context, it is the first one in the array
  var address = options.contexts.objectAt(0);
}

它以这种方式工作,因为您可能会将多个上下文传递给您的助手。