0

我有一个users模板,它是一个用户表。该表下方是创建新用户的链接。为了获得更好的用户体验,我想class="disabled"在创建新用户期间使用或隐藏该链接来禁用它。最好的方法是什么?

索引.html

<script type="text/x-handlebars" data-template-name="users">
  <div class='row'>
    <div class='span7'>
      <table class='table table-striped'>
        <tbody>
          {{#each model itemController="user"}}
            <tr {{bindAttr class="isDirty:warning"}}>
              <td>{{lastName}}</td>
            </tr>
          {{/each}}
        </tbody>
      </table>
      <p>
        {{#linkTo 'users.new' classNames="btn btn-small"}}Create a new user{{/linkTo}}
      </p>
    </div>
    <div class='span5'>
      {{outlet}}
    </div>
  </div>
</script>

<script type="text/x-handlebars" data-template-name="users/new">
  <p><strong>Last name</strong><br>{{view Ember.TextField valueBinding=lastName}}</p>

  <p>
  {{#if isDirty}}
    <button {{action 'save' this}} class="btn btn-small">Save</button>
  {{else}}
    <button class="btn btn-small disabled">Save</button>
  {{/if}}
  </p>
</script> 

应用程序.js

App.UsersRoute = Ember.Route.extend({
  model: function() {
    return App.User.find();
  }
});

App.UsersNewRoute = Ember.Route.extend({
  model: function() {
    return App.User.createRecord();
  },

  renderTemplate: function() {
    this.render({ into: 'users' });
  }
});

App.UsersNewController = Ember.ObjectController.extend({
  save: function(model) {
    model.get('transaction').commit();
    App.Router.router.transitionTo('users.index')
  }  
});
4

1 回答 1

2

我认为一种可能的解决方案是在 usersController 中添加一个属性,例如“isCreating”,您可以在 UsersNewRoute 的激活挂钩中将其设置为 true,并在停用时重置为 false。这将是这样的:

App.UsersNewRoute = Ember.Route.extend({

  activate: function() {
    this.controllerFor('users').set('isCreating', true);
  },

  deactivate: function() {
    this.controllerFor('users').set('isCreating', false);
  },

  model: function() {
    return App.User.createRecord();
  },

  renderTemplate: function() {
    this.render({ into: 'users' });
  }
});

显然,您将在模板中使用此属性并绑定类以隐藏按钮。

于 2013-05-06T21:32:56.953 回答