2012-03-17 2 views
0

클릭 핸들러 내에서 Ember.Button의 텍스트 레이블을 가져 오는 "Ember"방법을 찾으려고합니다. 현재 나는이가 (그리고 그것은 작동)하지만 난 그것을 할 수있는 더 "올바른"방법이 의심 : Ember.Buttondeprecated 수 있는지 여부에 대한 논의가있다Click 메서드 내에서 Ember.Button의 텍스트를 가져 오는 중

App.RecentNameBtn = Em.Button.extend({ 
    click: function(e){ 
     App.tweetsArray.set('username', e.srcElement.innerText); 
     App.tweetsArray.loadTweets(); 
    } 
}); 

답변

3

. 그와 관계없이 username을 보유하고 {{action}} 도우미를 사용하는 App.userController을 작성합니다 (http://jsfiddle.net/bZ8fY/ 참조). 오버 헤드처럼 보일 수도 있지만 바인딩을 사용하면 훨씬 더 유연 해집니다. 또한 뷰에서 로직을 컨트롤러로 옮기는 것이 좋습니다.

핸들 바 :

<script type="text/x-handlebars" > 
    {{#view App.TweetView}} 
     <button {{action "loadTweets" target="App.userController"}}> 
      {{username}} 
     </button> 
    {{/view}} 
</script>​ 

자바 스크립트 :

App = Ember.Application.create({}); 

App.userController = Ember.Object.create({ 
    username: 'emberjs', 
    loadTweets: function(evt) { 
     console.log(this.get('username')); 
    } 
}); 

App.TweetView = Ember.View.extend({ 
    usernameBinding: 'App.userController.username' 
}); 
​ 

+0

내 애플 그냥 버튼과 매우 간단하지 않습니다. 더 큰 미리보기가 있습니다 : http://andymatthews.net/code/emberTweets/ 사용자를 추가 한 후 왼쪽에 버튼이 나타납니다. 단추를 클릭하면 단추의 텍스트 레이블을 가져 와서 텍스트 필드에 덤프하려고합니다. – commadelimited

관련 문제