2013-05-06 105 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