Initiating blur/focus on some property change
-
14-11-2019 - |
Pergunta
I've got an Ember.TextField
in my view. I would like the field to focus when a certain property in the app changes. I have learned how to bind callbacks to the focus event but is there any way to initiate the focus of a field based on a binding?
Solução
Simple fiddle that I think does what you're asking.
http://jsfiddle.net/algesten/MGfAd/1/
Code for completeness:
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>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow