Ember.js를 local-storage-adapter와 함께 사용하고 있습니다. 레코드를 업데이트하는 중 이상한 문제가 있습니다.Ember js - 다른 테이블을 업데이트 한 후 Hasmany 관계가 깨집니다.
App.Post = DS.Model.extend({
title: DS.attr('string'),
comments: DS.hasMany('comment', {
async: true
})
});
App.Comment = DS.Model.extend({
message: DS.attr('string')
});
이들은 내 게시물 및 댓글 컨트롤러 : 생성 기록 hasMany의 관계가 제대로 업데이트
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.computed.alias("controllers.post.model"),
newMessage: '',
actions: {
create: function() {
var message = this.get('newMessage');
var comment = this.store.createRecord('comment', {
message: message
});
var post = this.get('post');
var comments = post.get('comments');
if (comments.get('content') == null) comments.set('content', []);
comments.pushObject(comment);
comment.save();
post.save();
}
}
});
동안
나는 hasMany의 관계에 게시물 및 의견 모델을 가지고있다.
{
"App.Post": {
"records": {
"0v66j": {
"id": "0v66j",
"title": "post1",
"comments": ["p31al", "tgjtj"]
}
}
},
"App.Comment": {
"records": {
"p31al": {
"id": "p31al",
"message": "comment 1"
},
"tgjtj": {
"id": "tgjtj",
"message": "comment 2"
}
}
}
}
게시물을 편집하는 동안 문제가 발생했습니다. 게시물 레코드를 편집하면 관계가 사라집니다. 검색을 수행하고이 코드를 찾았습니다.
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
}
}
});
이것은 위력을 발휘했으며 정상적으로 작동했습니다. 하지만 지금은 또 다른 문제가 있습니다. 내가 다른 레코드를 편집하는 경우, 모든 ID 참조는 다음과 같이 전체 개체로 대체됩니다
{"App.Post":{"records":{"0v66j":{"id":"0v66j","title":"post2","comments":[**{"message":"comment 1"},
{"message":"comment 2"}**]},"8nihs":{"id":"8nihs","title":"post3","comments":["b4v2b","dbki4"]}}},
"App.Comment":{"records":{"p31al":{"id":"p31al","message":"comment 1"},"tgjtj":{"id":"tgjtj","message":"comment 2"},
"b4v2b":{"id":"b4v2b","message":"comments3"},"dbki4":{"id":"dbki4",
"message":"comments4"}}}}
코멘트 refrences이 코멘트를해야한다 "."p31al ","tgjtj "이 같은하지만 IDS {{ "message": "comment 1"}, { "message": "comment 2"}]
당신은 단지 동적으로 의견을 업데이 트합니까? json 객체 전체를 반환하는 대신? – yardpenalty