首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >成员组件绑定-attr className不会更改

成员组件绑定-attr className不会更改
EN

Stack Overflow用户
提问于 2014-02-26 22:00:27
回答 2查看 1.6K关注 0票数 3

我试图在这里做一些非常简单的事情,但到目前为止,我无法做到这一点。可能是我在做傻事。

你点击圆圈打开或关闭一个帖子。您可以看到警报消息正在正常运行,参数正在更新,但是我无法更改className。

这是JS:http://jsbin.com/tusimozo/1/edit

代码语言:javascript
复制
<script type="text/x-handlebars" data-template-name="index">
  {{ group-list posts=model }}
</script>

<script type="text/x-handlebars" id="components/group-list">
  <div class="row">
    <table width="100%">
      <thead>
        <tr>
          <th width="90">Status</th>
          <th align="left">Categories</th>
        </tr>
      </thead>
      <tbody>
        {{# each item in posts}}
          {{list-item published=item.published title=item.title pubDate=item.pub_date}}
        {{/each}}
      </tbody>
    </table>
  </div>
</script>

<script type="text/x-handlebars" id="components/list-item">
  <tr>
    <td>
      <a href="#" {{bind-attr class=":post-status published:on:off"}} {{action "publish"}}></a>
    </td>
    <td align="left">{{title}}</td>
  </tr>
</script>

app.js

代码语言:javascript
复制
App = Ember.Application.create();

posts = [{
  title: "Items",
  published: true
}, {
  title: "Boards",
  published: false
}];

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return posts; 
  }
});

App.ListItemComponent = Ember.Component.extend({
  actions: {
    publish: function() {
      var publishStatus = this.get('published') ? false : true;
      this.set('published', publishStatus);
      alert(this.get('published') ? 'publish' : 'unpublish');
    }
  }
});

我在这里错过了什么?

干杯!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-26 23:18:50

因此,基本上您应该在组件上使用类绑定。

代码语言:javascript
复制
App.ListItemComponent = Ember.Component.extend({
    tagName:'tr',
    classNameBindings: ['isPublished:on:off'],
    isPublished: function() {
        return this.get('published');
    }.property('published'),
    actions: {
         publish: function() {
            this.toggleProperty('published');
         }
    }
});

JSBIN:

http://jsbin.com/mixeniheze/1/edit

票数 2
EN

Stack Overflow用户

发布于 2017-06-14 12:35:45

您也可以使用classNames添加多个类。

代码语言:javascript
复制
classNames: ['class-name-1'], 
classNameBindings: ['isSomethingTrue:class-name-2:class-name-3']

在此进一步参考:https://guides.emberjs.com/v1.10.0/components/customizing-a-components-element/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22054323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档