I am using Ember.js with local-storage-adapter. I have a weird problem while updating records.
I have a post and ments model with hasMany relationships:
App.Post = DS.Model.extend({
title: DS.attr('string'),
ments: DS.hasMany('ment', {
async: true
})
});
App.Comment = DS.Model.extend({
message: DS.attr('string')
});
These are my post and ments controllers:
App.PostsController = Ember.ArrayController.extend({
newTitle: '',
actions: {
create: function() {
var title = this.get('newTitle');
var post = this.store.createRecord('post', {
title: title
});
this.set('newTitle', '');
post.save();
}
}
});
App.CommentsController = Ember.ArrayController.extend({
needs: "post",
post: Emberputed.alias("controllers.post.model"),
newMessage: '',
actions: {
create: function() {
var message = this.get('newMessage');
var ment = this.store.createRecord('ment', {
message: message
});
var post = this.get('post');
var ments = post.get('ments');
if (ments.get('content') == null) ments.set('content', []);
ments.pushObject(ment);
ment.save();
post.save();
}
}
});
While creating records hasMany relations updated correctly.
{
"App.Post": {
"records": {
"0v66j": {
"id": "0v66j",
"title": "post1",
"ments": ["p31al", "tgjtj"]
}
}
},
"App.Comment": {
"records": {
"p31al": {
"id": "p31al",
"message": "ment 1"
},
"tgjtj": {
"id": "tgjtj",
"message": "ment 2"
}
}
}
}
The problem occured while editing post. The relationships are gone after editing the post record. I did some searching and found this code:
DS.JSONSerializer.reopen({
serializeHasMany: function(record, json, relationship) {
var key = relationship.key;
var relationshipType = DS.RelationshipChange.determineRelationshipType(record.constructor, relationship);
// alert(relationshipType);
if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany' || relationshipType === 'manyToOne') {
json[key] = Ember.get(record, key).mapBy('id');
// TODO support for polymorphic manyToNone and manyToMany
// relationships
}
}
});
This did the trick and it worked fine. But now I have another problem. If I edit any other record, all the id references are replaced by whole object like this:
{"App.Post":{"records":{"0v66j":{"id":"0v66j","title":"post2","ments":[**{"message":"ment 1"},
{"message":"ment 2"}**]},"8nihs":{"id":"8nihs","title":"post3","ments":["b4v2b","dbki4"]}}},
"App.Comment":{"records":{"p31al":{"id":"p31al","message":"ment 1"},"tgjtj":{"id":"tgjtj","message":"ment 2"},
"b4v2b":{"id":"b4v2b","message":"ments3"},"dbki4":{"id":"dbki4",
"message":"ments4"}}}}
Comment refrences should be ments":["p31al","tgjtj"] like this. but the ids are replaced as "ments":[{"message":"ment 1"},{"message":"ment 2"}]
I am using Ember.js with local-storage-adapter. I have a weird problem while updating records.
I have a post and ments model with hasMany relationships:
App.Post = DS.Model.extend({
title: DS.attr('string'),
ments: DS.hasMany('ment', {
async: true
})
});
App.Comment = DS.Model.extend({
message: DS.attr('string')
});
These are my post and ments controllers:
App.PostsController = Ember.ArrayController.extend({
newTitle: '',
actions: {
create: function() {
var title = this.get('newTitle');
var post = this.store.createRecord('post', {
title: title
});
this.set('newTitle', '');
post.save();
}
}
});
App.CommentsController = Ember.ArrayController.extend({
needs: "post",
post: Ember.puted.alias("controllers.post.model"),
newMessage: '',
actions: {
create: function() {
var message = this.get('newMessage');
var ment = this.store.createRecord('ment', {
message: message
});
var post = this.get('post');
var ments = post.get('ments');
if (ments.get('content') == null) ments.set('content', []);
ments.pushObject(ment);
ment.save();
post.save();
}
}
});
While creating records hasMany relations updated correctly.
{
"App.Post": {
"records": {
"0v66j": {
"id": "0v66j",
"title": "post1",
"ments": ["p31al", "tgjtj"]
}
}
},
"App.Comment": {
"records": {
"p31al": {
"id": "p31al",
"message": "ment 1"
},
"tgjtj": {
"id": "tgjtj",
"message": "ment 2"
}
}
}
}
The problem occured while editing post. The relationships are gone after editing the post record. I did some searching and found this code:
DS.JSONSerializer.reopen({
serializeHasMany: function(record, json, relationship) {
var key = relationship.key;
var relationshipType = DS.RelationshipChange.determineRelationshipType(record.constructor, relationship);
// alert(relationshipType);
if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany' || relationshipType === 'manyToOne') {
json[key] = Ember.get(record, key).mapBy('id');
// TODO support for polymorphic manyToNone and manyToMany
// relationships
}
}
});
This did the trick and it worked fine. But now I have another problem. If I edit any other record, all the id references are replaced by whole object like this:
{"App.Post":{"records":{"0v66j":{"id":"0v66j","title":"post2","ments":[**{"message":"ment 1"},
{"message":"ment 2"}**]},"8nihs":{"id":"8nihs","title":"post3","ments":["b4v2b","dbki4"]}}},
"App.Comment":{"records":{"p31al":{"id":"p31al","message":"ment 1"},"tgjtj":{"id":"tgjtj","message":"ment 2"},
"b4v2b":{"id":"b4v2b","message":"ments3"},"dbki4":{"id":"dbki4",
"message":"ments4"}}}}
Comment refrences should be ments":["p31al","tgjtj"] like this. but the ids are replaced as "ments":[{"message":"ment 1"},{"message":"ment 2"}]
Share Improve this question edited Mar 15, 2017 at 19:50 Joshua Kleveter 1,76917 silver badges23 bronze badges asked Oct 26, 2013 at 7:40 vijiviji 1,6522 gold badges21 silver badges34 bronze badges 1- Cant you just dynamically update the ment only? Instead of returning the entire json object? – yardpenalty. Commented Apr 26, 2017 at 19:33
2 Answers
Reset to default 1When using ApplicationSerializer which extends LSSerializer, it seems to work.
Maybe it got fixed since asked?
I've noticed a few things in my path with Ember... and especially Ember-Data.
One of them is when dealing with associations I've had to manually re-add in the associations saving and having to re-save, and use addObject to in-memory associations as you're using a bit here. :)
Note that this usually only happens when I'm updating more than one new object at once. For example, if your post is new, and your ment is also new.
I'm a little worried to see the following code in your codebase, because it shouldn't need to be there. You shouldn't ever have null or non-array objects in your associations. I'm not sure what hackery you did with the Adapter and why it was necessary, but I hope that wasn't the reason:
if(ments.get('content') == null)
ments.set('content', []);
Anyway, the following code is how I would probably write your create action. It might help. I hope it does.
create: function() {
// get the post for association on the new ment
var post = this.get('post');
// get the message to store on the new ment
var message = this.get('newMessage');
var ment = this.store.createRecord('ment', {
message : message,
post : post
});
ment.save().then(function(savedComment) {
post.get('ments').addObject(savedComment);
});
}
Note that it's a lot simpler. Generally if you're doing tricky plicated things, something's amiss and it's time to go back to basics and add one thing at a time, testing thoroughly between additions. :)
Good luck!