1

我正在遍历数组控制器以显示项目-在此视图中,我尝试使用当前地址的上下文执行自定义 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);
});
4

1 回答 1

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);
}

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

于 2013-02-19T12:56:27.573 回答