Skip to content

Instantly share code, notes, and snippets.

@green-arrow
Created April 15, 2016 20:04
Show Gist options
  • Save green-arrow/9feca6b4eab268eaa845e513774db58a to your computer and use it in GitHub Desktop.
Save green-arrow/9feca6b4eab268eaa845e513774db58a to your computer and use it in GitHub Desktop.
Observer Bug
import Ember from 'ember';
let Person = Ember.Object.extend({
notificationService: Ember.inject.service(),
firstName: null,
lastName: null,
fullName: Ember.computed('firstName', 'lastName', function() {
return `${this.get('firstName')} ${this.get('lastName')}`;
})
});
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
firstName: 'John',
lastName: 'Doe',
person: null,
notificationService: Ember.inject.service(),
init() {
this._super(...arguments);
let person = new Person({
firstName: this.get('firstName'),
lastName: this.get('lastName')
});
this.set('person', person);
this.get('notificationService')
.sendNotification('All set up');
},
partOfNameChanged: Ember.observer('person.firstName', 'person.lastName', function() {
this.get('notificationService')
.sendNotification(
`Person's name changed to
${this.get('person.fullName')}`
);
}),
actions: {
updateName() {
let { firstName, lastName } = this.getProperties('firstName', 'lastName');
let personService = this.get('personService');
this.set('person.firstName', firstName);
this.set('person.lastName', lastName);
}
}
});
import Ember from 'ember';
export default Ember.Service.extend({
messages: null,
init() {
this.set('messages', []);
},
sendNotification(message) {
this.get('messages').pushObject(message);
}
});
<h1>Welcome to {{appName}}</h1>
Person full name: {{person.fullName}}
<br>
<br>
<ul>
{{#each notificationService.messages as |message|}}
<li>{{message}}</li>
{{/each}}
</ul>
{{input type="text" value=firstName}}
{{input type="text" value=lastName}}
<br>
<button type="button" {{action "updateName"}}>
Update Name
</button>
{
"version": "0.7.2",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.4/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.4.3/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.4/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment