Skip to content

Instantly share code, notes, and snippets.

@lcoq
Created December 21, 2015 17:39
Show Gist options
  • Save lcoq/e0292cc34f28a3593daa to your computer and use it in GitHub Desktop.
Save lcoq/e0292cc34f28a3593daa to your computer and use it in GitHub Desktop.
post.comments.@each.id observers are not called when a new post comment is saved
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle',
init() {
this._super(...arguments);
$.mockjax({
url: '/posts/1',
responseText: {
'data': {
'id': '1',
'type': 'post',
'attributes': {
'name': "1st post"
},
'relationships': {
'comments': {
'data': [],
'links': {
'related': "/posts/1/comments",
'self': "/posts/1/relationships/comments"
}
}
}
}
}
});
$.mockjax({
url: '/comments',
responseText: {
'data': {
'id': '1',
'type': 'comment'
},
'relationships': {
'post': {
'links': {
'related': "/comments/1/game",
'self': "/comments/1/relationships/game"
}
}
}
}
});
},
post: Ember.computed(function() {
return this.store.findRecord('post', '1');
}),
postCommentsIdChanged: Ember.observer('post.comments.@each.id', function() {
console.log('post.comments.@each.id changed');
}),
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('will save, post.comments.@each.id should change soon');
comment.save().then( () => {
let post = this.get('post');
console.log('did save, post.comments.@each.id observers should be called');
console.log('posts.comments.length: ' + post.get('comments.length'));
console.log('posts.comments ids: [' + post.get('comments').mapBy('id') + ']');
});
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
<h3>{{post.name}}</h3>
<ul>
{{#each post.comments as |comment|}}
<li></li>
{{/each}}
</ul>
<button {{action "buildComment"}}>Build comment</button>
<button {{action "saveComment"}}>Save comment</button>
import DS from 'ember-data';
export default DS.Model.extend({
content: DS.attr(),
post: DS.belongsTo('post')
});
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr(),
comments: DS.hasMany('comment')
});
{
"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