ember.js - How do I set an Ember query-parameter on the first transition? -


this follow-on #35192211

i have query-param, 'locale'. when user first loads application, if haven't specified ?locale=..., i'd detect 1 , set query-parameter. (this way, if share url, others see they're seeing. if think bad idea locales, imagine contextual query parameter account.)

so, if user navigates directly /foo/bar?locale=es, stay there. if navigate /foo/bar, url switches /foo/bar?locale=en , renders foo page.

attempt 1

beforemodel(transition) {   if (transition.queryparams.locale == null) {     return this.transitionto({ queryparams: { locale: 'en' } });   } } 

this nothing. @ end of transition, url has no ?locale=en.

attempt 2

beforemodel(transition) {   if (transition.queryparams.locale == null) {     this.send('switchlocale', 'en');   } }, actions: {   switchlocale(locale) {     this.controllerfor('application').set('locale', locale);     this.refresh();   } } 

this throws following exception:

error while processing route: foo.bar can't trigger action 'switchlocale' because app hasn't finished transitioning first route. trigger action on destination routes during transition, can call .send() on transition object passed model/beforemodel/aftermodel hooks.

attempt 3

same (2), using transition.send('switchlocale', 'en').

this prevents error, we're accomplishing nothing.

ok, seems have met both requirements:

so, if user navigates directly /foo/bar?locale=es, stay there. if navigate /foo/bar, url switches /foo/bar?locale=en , renders foo page.

basically i'm using ember.run.next in beforemodel:

beforemodel(transition) {   if (transition.queryparams.locale == null) {     ember.run.next(() => transition.send('switchlocale', 'en'));   } }, 

check working demo.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -