Skip to content

Instantly share code, notes, and snippets.

@ykaragol
Last active April 28, 2019 21:51
Show Gist options
  • Save ykaragol/a0039573de71b341f33d81f6d6abc47f to your computer and use it in GitHub Desktop.
Save ykaragol/a0039573de71b341f33d81f6d6abc47f to your computer and use it in GitHub Desktop.
service injection bug
import Component from '@ember/component'
import { inject as service } from '@ember/service'
import { computed } from '@ember/object'
import { alias } from '@ember/object/computed'
export default Ember.Component.extend({
myService: service(),
init() {
this._super(...arguments)
console.log(this.myService)
},
y: computed('myService.x', function() {
// if (Math.floor(Math.random()*2) > 0) {
// console.log(this.get('myService.x'))
// }
return Math.floor(10*Math.random())
}),
z: alias('myService.x'),
t: computed('z', function() {
return Math.floor(10*Math.random())
})
});
import Component from '@ember/component'
import { inject as service } from '@ember/service'
import { computed } from '@ember/object'
import { alias } from '@ember/object/computed'
export default Component.extend({
myService: service(),
init() {
this._super(...arguments)
// console.log(this.myService)
},
y: computed('myService.x', function() {
// if (Math.floor(Math.random()*2) > 0) {
// console.log(this.get('myService.x'))
// }
return Math.floor(10*Math.random())
}),
z: alias('myService.x'),
t: computed('z', function() {
return Math.floor(10*Math.random())
})
});
import Controller from '@ember/controller';
export default Controller.extend({
});
import Service from '@ember/service'
import { later } from '@ember/runloop'
export default Service.extend({
init() {
this.set('x', 4);
this.next();
},
increment() {
this.incrementProperty('x');
console.log(this.get('x'))
},
next() {
later(this, this.timer, 3000);
},
timer() {
this.increment();
this.next();
}
});
<h1>Welcome</h1>
<br>
<br>
{{my-component1}}
<br>
{{my-component2}}
<br>
<br>
My Component 1
<br>
{{y}}
<br>
{{t}}
My Component 2
<br>
{{y}}
<br>
{{t}}
{
"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