Skip to content

Instantly share code, notes, and snippets.

@rinoldsimon
Last active March 21, 2019 06:54
Show Gist options
  • Save rinoldsimon/c4c29c7a758e97e74480801009bd3247 to your computer and use it in GitHub Desktop.
Save rinoldsimon/c4c29c7a758e97e74480801009bd3247 to your computer and use it in GitHub Desktop.
changing service property and accessing it globally
import Ember from 'ember';
export default Ember.Controller.extend({
name1: null,
name2: null,
init() {
this._super(...arguments);
// print initial value
this.set('name1', (this.get('serviceName').name)); // hello
// change value of name property in service
this.get('serviceName').changeName("hai");
// print changed value
this.set('name2', (this.get('serviceName').name)); // hai
}
});
export function initialize(application) {
application.inject('controller', 'serviceName', 'service:service-name');
// if needed
application.inject('route', 'serviceName', 'service:service-name');
application.inject('component', 'serviceName', 'service:service-name');
}
export default {
initialize
};
import Ember from 'ember';
export default Ember.Service.extend({
name: "hello",
changeName(name) {
this.set('name', name);
}
});
<h1>before change</h1> {{name1}}
<h1>after change</h1> {{name2}}
<br>
<br>
{{outlet}}
<br>
<br>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment