2013-10-26 2 views
62

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"}]

+0

당신은 단지 동적으로 의견을 업데이 트합니까? json 객체 전체를 반환하는 대신? – yardpenalty

답변

-1

나는 Ember와 함께 몇 가지 것을 알아 챘다. . 특히 Ember-Data.

그 중 하나는 연관을 처리 할 때 수동으로 연관을 다시 추가해야했습니다. 다시 저장해야하며 addObject를 메모리 내 연결에 사용하려면 여기를 조금만 사용하십시오. :)

대개 이것은 한 번에 두 개 이상의 새 개체를 업데이트 할 때만 발생합니다. 예를 들어 귀하의 게시물이 새로운 경우 귀하의 의견도 새로운 것입니다.

코드베이스에 다음 코드가 표시 될 필요가 없어서 조금 걱정됩니다. 당신은 당신의 연관 안에 null 또는 배열이 아닌 객체를 가질 수 없습니다. 난 당신이 어댑터와 함께 왜 그것이 필요하다고했던 해커 모르겠어요,하지만 난 그 이유 아니었다 희망 :

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); 
    }); 
} 

매우 간단합니다. 일반적으로 복잡하고 복잡한 작업을 수행하는 경우 문제가 발생할 수 있으며 시간이 지나면 기본 사항으로 돌아가서 한 번에 하나씩 추가하여 추가 작업을 철저히 테스트해야합니다. :)

행운을 빌어 요!

+2

도움이 되었습니까? 어때? –

1

LSSerializer를 확장 한 ApplicationSerializer를 사용하면 효과가있는 것 같습니다.

질문 이후로 수정되었을 수 있습니까?

관련 문제