Tuesday 15 February 2011

javascript - Keep focus on Ember.TextField -



javascript - Keep focus on Ember.TextField -

in ember.js application have search text field this:

{{view ember.textfield valuebinding="search" action="dosearch" autofocus="autofocus"}} <button {{action "dosearch"}} type="button">hledat</button>

and controller following:

app.bookscontroller = ember.arraycontroller.extend({ queryparams: ['keywords'], keywords: [], search: "", actions: { dosearch: function() { var tokens = this.get('search').split(' '); this.set('keywords', tokens); this.set('search', ''); } } });

what need text field keeps focus after button has been pushed or come in key pressed.

what's right way accomplish that? guess i'll need set focus input box in dosearch method somehow. how can done?

communication between controller , view layer can accomplish in clean way using ember.evented

here go

your template :

{{view ember.textfield valuebinding="search" class="search-field" action="dosearch" autofocus="autofocus"}} <button {{action "dosearch"}} type="button">hledat</button>

your controller :

app.bookscontroller = ember.arraycontroller.extend(ember.evented, { queryparams: ['keywords'], keywords: [], search: "", actions: { dosearch: function() { var tokens = this.get('search').split(' '); this.set('keywords', tokens); this.set('search', ''); this.trigger('search'); } } });

and view :

app.booksview = ember.view.extend({ didinsertelement: function() { this.get('controller').on('search', function() { $('.search-field').focus(); }); this._super(); } });

it improve access text field scoping view

didinsertelement: function() { this.get('controller').on('search', function() { this.$().find('.search-field').focus(); }.bind(this); this._super(); }

javascript ember.js focus

No comments:

Post a Comment