2016-08-01 69 views
0

因此,當我點擊模板上的按鈕時,應該更改一個值並在屏幕上顯示記錄列表。這沒有發生。我究竟做錯了什麼?以下是我的控制器,模型,模板和路線的代碼。如何使用車把(EmberJS)

控制器:

import Ember from 'ember'; 

export default Ember.Controller.extend({ 

    usernameIn: '', 
    passwordIn: '', 
    found: '', 
    display: false, 

    actions:{ 

     renderNotes(){ 
      this.display = true; 
     } 
    } 

}); 

路線:

import Ember from 'ember'; 

export default Ember.Route.extend({ 

model() 
{ 
    return this.store.findAll('note'); 
} 

}); 

模板:

<h1>Login:</h1> 

<h3>Username:</h3>{{input type="text" value = keyInput size="50"}} 
<h3>Password:</h3>{{input type="text" value = keyInput size="50"}} 
<br> 
<br> 
<button{{action 'renderNotes'}}>Submit</button> 

{{#if display}} 
    <thead> 
     <tr> 
      <th>Username</th> 
      <th>Note</th> 
     </tr> 
    </thead> 
    <tbody> 
    {{#each model as |found|}} 

     <tr> 
      <th>{{found.username}}</th> 
      <td>{{found.note}}</td> 
     </tr> 

    {{/each}} 
    </tbody> 
{{/if}} 

{{outlet}} 

型號:

import DS from 'ember-data'; 

export default DS.Model.extend({ 

    note: DS.attr('string'), 
    username: DS.attr('string'), 
    password: DS.attr('string') 
}); 

回答

0

您必須使用「設置」設置燼財產

renderNotes(){ 
    this.set('display', true); 
} 
0

的非常簡單的用例,而這創造了component.js的動作代碼,你可以很容易地做到這一點在模板。如果您使用Ember 2+,請致電。方法如下:

<button onclick={{action (mut display) true)}}>Submit</button>

+0

我們怎樣才能讓這個按鈕切換顯示?這是可能的,沒有寫作的動作?我要求這個只是爲了確認 – kumkanillam

+0

是的,這是可能的,沒有寫作的動作。只需將'action'助手與'mut'助手結合起來就像我上面所示。你可以閱讀更多關於'mut' helper [here](http://emberjs.com/api/classes/Ember.Templates.helpers.html#method_mut)和'action' helper [here](http:// emberjs.com/api/classes/Ember.Templates.helpers.html#method_action)。 – rmmmp

+0

在您的代碼中,true是硬編碼的。我沒有找到切換顯示屬性的方法。可能是我應該問這個新的問題,但! – kumkanillam