How do you update a controller from another controller in ember.js? -


in application have common header outlets main application layout. in header can select site. need site selection update template rendered in application layout. having struggled few days think correct way solve use shared service, have header controller observe site selection , set value in shared service, have index controller use ember.computed.alias on value in service controller. following example of code:

controllers/header.js

import ember 'ember';  export default ember.controller.extend({   sessionservice: ember.inject.service(),    currentsitechanged: ember.observer('session.current_site', function(){     var current_site = this.get('session.current_site');     console.log('currentsiteobserver', current_site);     this.get('sessionservice').set('currentsite', current_site);   }),  }); 

controllers/index.js

import ember 'ember';  export default ember.controller.extend({   sessionservice: ember.inject.service(),   currentsite: ember.computed.alias('sessionservice.currentsite'),   dashboarddata: function(){     var currentsite = this.get('currentsite');     console.log("in dashboarddata", currentsite);     //other code uses currentsite   }.property('currentsite'), }); 

services/session-service.js

import ember 'ember';  export default ember.service.extend({   currentsite: null,    setcurrentsite: function(){     var currentsite = this.get('session.current_site');     this.set('currentsite', currentsite );   }.on('init'), }); 

i think should allow select site in header , have dashboarddata property in index update use selection. when page loads header defaults first site , index renders has correct site value must have gotten session-service, if select site index not updated. via console.logs , debugging can see header observing change , setting value on session-service.

additionally have tried solving other ways (injecting header service index , observing property, injecting index in header , directly setting value, sending , listening events,etc) willing try or corrected isn't correct way solve problem.

i using ember 1.13.8 , moving 2 isn't option @ moment.

i don't think service appropriate solution problem.

you want application have restful url design (respect urls corner-stone of ember framework), try capture application state in url.

consider if user select site, , hit refresh lose selection unless stored somehow in cookie or localstorage.

i recommend using either routes or query parameters solve problem.

routes
using routes straightforward (http://whatever.com/sites/pet-hampsters).

query params
can use query params, http://whatever.com/?site=pet%20hampsters.

to write action bubbles application controller , sets value of 'site' queryparam. of sub-controllers on active route can read site value new ember.inject syntax. conventional way manage dependencies between controllers.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -