지금 사용하고 있는 엠베리제스 로컬 스토리지보다 어댑터입니다. 나는 이상한 문제 낼수있지않나 업데이팅이 레코드는유지합니다.
I have a 로그하고 의견 모델과 이스마니 관계:
App.Post = DS.Model.extend({
title: DS.attr('string'),
comments: DS.hasMany('comment', {
async: true
})
});
App.Comment = DS.Model.extend({
message: DS.attr('string')
});
내 그것은꿈의 로그하고 의견 컨트롤러에서는:
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();
}
}
});
만들 때 제대로 업데이트되도록 레코드는유지합니다 이스마니 관계.
{
"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"
}
}
}
}
문제가 발생했습니다.) 이 편집한 후 관계를 없어질 게시물로의 편집하든 기록. I did 일부 검색 및 결과 이 코드:
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"}}}}
참고로 이 때, 보통 한 번에 여러 개의 객체에는 I& # 39 만 m 업데이팅이 발생합니다. 예를 들어, post 는 새로운 및 의견 역시 새로운.
if(comments.get('content') == null)
comments.set('content', []);
아무튼, 난 아마도 다음 코드를 작성하는 방법입니다 만들기 작업을. 것이 도움이 됩니다. 않니다 됐으면 좋겠다 "고 말했다.
create: function() {
// get the post for association on the new comment
var post = this.get('post');
// get the message to store on the new comment
var message = this.get('newMessage');
var comment = this.store.createRecord('comment', {
message : message,
post : post
});
comment.save().then(function(savedComment) {
post.get('comments').addObject(savedComment);
});
}
참고로 it& # 39 의 많이유 간단해진다는. 일반적으로 복잡한 것을 하고, re you& # 39 의 경우 까다로운 something& # 39 어긋나서 it& # 39, s time to go back to basics 및 추가 및 한 번에 한 가지 분명한 것은 철저히 테스트 사이에 추가. )
굿 럭!