2013-05-06 34 views
0

我有一个users模板,这是一个用户表。在该表下是创建新用户的链接。为了更好的用户体验,我想通过使用class="disabled"来禁用它,或者在创建新用户时隐藏该链接。什么是最好的方式来做到这一点?隐藏用户期间的链接。新

的index.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> 

app.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') 
    } 
}); 

回答

2

我认为一个可能的解决办法是在usersController添加属性,像 'isCreating',这您可以在UsersNewRoute的激活挂钩中设置为true,并在deactivate中将其重置为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' }); 
    } 
}); 

显然你会在模板中使用这个属性,并绑定类来隐藏按钮。

+0

当'users.new'中定义模型时,如何在'users'模板中使用'isNew'检查? – wintermeyer 2013-05-06 21:36:28

+0

谢谢!我希望“少代码”版本,但它像一个魅力。 – wintermeyer 2013-05-06 21:58:10

+0

也许有一个更简洁的版本,但我没有记住它... – 2013-05-06 22:00:50