1

我正在尝试绑定按钮是计算属性active还是disabled计算属性,但之后会收到此弃用警告和错误。

这是一个麻烦的按钮(此处为 Ember 1.11.1):

<button {{ action 'loadMore' }} {{if canLoadMore 'active' 'disabled'}}>Load More Posts...</button>

此警告和错误:

DEPRECATION: Returning a string of attributes from a helper inside an element is deprecated.
Uncaught TypeError: Cannot read property 'replace' of undefined

关于这个功能:

if (value) {
  Ember['default'].deprecate('Returning a string of attributes from a helper inside an element is deprecated.');

  var parts = value.toString().split(/\s+/);
  for (var i = 0, l = parts.length; i < l; i++) {
    var attrParts = parts[i].split('=');
    var attrName = attrParts[0];
    var attrValue = attrParts[1];

    attrValue = attrValue.replace(/^['"]/, '').replace(/['"]$/, '');

    env.dom.setAttribute(domElement, attrName, attrValue);
  }
4

3 回答 3

4

我假设您想根据canLoadMore属性在按钮元素上设置一个活动类。在这种情况下,请继续阅读。

您的按钮代码评估为类似于此 html 的内容:

<button data-ember-action="1234" 'active'>

当你可能想要这个时:

<button data-ember-action="1234" class='active'>

所以改变你的代码:

<button {{ action 'loadMore' }} {{if canLoadMore 'active' 'disabled'}}>Load More Posts...</button>

至:

<button {{ action 'loadMore' }} class="{{if canLoadMore 'active' 'disabled'}}">Load More Posts...</button>
于 2015-04-10T13:08:23.380 回答
1

尝试这个:

<button {{ action 'loadMore' }} 
    class="{{if canLoadMore 'active' 'disabled'}}">Load More Posts...</button>

无论如何, TypeError (这是因为您的字符串不包含 '=' 而触发的,因为<button {{ action 'loadMore' }} class={{if canLoadMore 'class="active"' 'class="disabled"'}}>Load More Posts...</button>不应发生(阅读:更有意义的错误消息将非常有帮助)。

但是,这是一个已弃用的功能,无论如何都会消失。

于 2015-04-10T13:05:43.453 回答
0

您可以使用{{bind-attr}}帮助程序:

{{bind-attr class=property:classIfTrue:classIfFalse :classThatDoesntCare}}

所以你的例子可能是这样的:

<button {{action "loadMore"}} {{bind-attr class="model.canLoadMore:active:disabled :btn :btn-default"}}>Load More Posts...</button>
于 2015-04-13T05:58:14.830 回答