Skip to content

Instantly share code, notes, and snippets.

@lcoq
Created December 21, 2015 21:06
Show Gist options
  • Save lcoq/8f60fb47ad37195b6795 to your computer and use it in GitHub Desktop.
Save lcoq/8f60fb47ad37195b6795 to your computer and use it in GitHub Desktop.
post.comments.@each.id observers not called on Comment#save
import Ember from 'ember';
export default Ember.Controller.extend({
init() {
$.mockjax({
type: 'get',
url: '/posts/1',
responseText: {
'data': {
'id': '1',
'type': 'post',
'attributes': {
'name': "First post"
},
'relationships': {
'comments': {
'data': [],
'links': {
'related': '/posts/1/comments',
'self': '/posts/1/relationships/comments'
}
}
}
}
}
});
$.mockjax({
type: 'post',
url: '/comments',
responseText: {
'data': {
'id': '1',
'type': 'comment',
'relationships': {
'game': {
'links': {
'related': '/comments/1/game',
'self': '/comments/1/relationships/game'
}
}
}
}
}
});
},
post: Ember.computed(function() {
return this.store.findRecord('post', 1);
}),
postCommentIdsChanged: Ember.observer('post.comments.@each.id', function() {
console.log('post.comments.@each.id observer called');
}),
actions: {
buildComment() {
this.get('post').then((post) => {
let comment = this.store.createRecord('comment', { post: post });
this.set('newComment', comment);
});
},
saveComment() {
let comment = this.get('newComment');
console.log('comment will save, post.comments.@each.id should change soon');
console.log('[' + this.get('post.comments').mapBy('id') + ']');
comment.save().then(() => {
console.log('comment saved, post.comments.@each.id should have changed');
console.log('[' + this.get('post.comments').mapBy('id') + ']');
});
}
}
});
{{outlet}}
<h2>{{post.name}}</h2>
<button {{action "buildComment"}}>#buildComment</button>
<button {{action "saveComment"}}>#saveComment</button>
import DS from 'ember-data';
export default DS.Model.extend({
post: DS.belongsTo('post')
});
import DS from 'ember-data';
export default DS.Model.extend({
comments: DS.hasMany('comment'),
name: DS.attr()
});
{
"version": "0.4.17",
"EmberENV": {
"FEATURES": {}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.2.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js",
"jquery-mockjax": "https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment