2012-02-28 23 views
2

在我看來,我有Ember.TextField。當應用程序中的某個屬性更改時,我希望該字段可以關注。我已經學會了如何將回調綁定到焦點事件,但是有什麼方法可以基於綁定來啓動焦點嗎?啓動模糊/專注於一些物業更改

回答

3

簡單的小提琴,我想你會問什麼。

http://jsfiddle.net/algesten/MGfAd/1/

代碼的完整性:

window.App = Ember.Application.create(); 

App.model = Ember.Object.create({ 
    someProp: 123, 
}); 

App.MyTextField = Ember.TextField.extend({ 

    value: 'foo', 

    autoFocus: function() { 
     if (App.model.someProp === "42") { 
      this.$().focus(); 
     } 
    }.observes('App.model.someProp') 

}); 
​ 

HTML:

<script type="text/x-handlebars"> 
    {{view App.MyTextField}} 
</script> 

<br/>Try changing this field to '42' and see that the focus moves. 

<script type="text/x-handlebars"> 
    {{view Ember.TextField valueBinding="App.model.someProp"}} 
</script> 
​ 
+0

完美。謝謝! Ember以幾種不同的方式接近綁定,我有時會發現我完全忘記了觀察者作爲一種選擇。 – Technocrat 2012-02-29 02:10:02