2013-01-18 61 views
5

http://jsfiddle.net/pF2cF/6/代碼2個問題: 1.單擊 「myButton的」 不走clickButton功能App.indexController 2.進入在文本字段將觸發MyButton第一次點擊(如果#1得到解決)ember.js <按鈕{{行動}}></button>不能正常工作

任何人都可以幫助解決它們?我確實有一個解決方法,但我不確定使用什麼問題。

謝謝!

的代碼片段如下,在2013年1月14日使用ember.js從它的主分支:

<script type="text/x-handlebars" data-template-name="myTemplate"> 
     <button {{action clickButton target="App.indexController"}} >MyButton1</button> 
     {{view App.MyView placeholder="Input something 1 and enter"}} 
    </script> 

App = Em.Application.create({ 
    ready: function() { 
    } 
}); 

App.Router.map(function() { 
    this.route("index", { path: "/" }); //master 01142013 syntax 
}); 
App.IndexRoute = Ember.Route.extend({ 
    renderTemplate: function() { 
    this.render('myTemplate', {controller: 'indexController'}); 
    } 
}); 

App.indexController = Ember.Controller.extend({ 
    clickButton: function() { 
    alert("clickButton"); 
    } 
}); 

App.MyView = Em.TextField.extend({ 
    insertNewline: function (evt) { 
    alert("enter pressed"); 
    } 
}); 

回答

18

你犯了一些小錯誤。我已經在JSBin上提供了一個工作版本。

未造成任何故障文體問題:

  • 你不需要聲明任何index路線。
  • 您不需要Application上的空ready方法。一般來說,將啓動邏輯放在ApplicationRoute#setupController中,在那裏你也可以訪問控制器。
  • 除非您嘗試跨多個路線共享單個模板,否則應該將模板命名爲與路線相同的名稱。在這種情況下,您應該只使用index而不是myTemplate

相關故障問題:

  • 控制器的名稱應該大寫:App.IndexControllerApp.indexController
  • 你不應該,如果目標是電流控制器指定目標。
  • 您的render調用指定{ controller: 'indexController' }。它應該是{ controller: 'index' }
+0

謝謝!這很好用! – xinqiu