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()
ontransition
object passedmodel/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
Post a Comment