2014-11-02 2 views
1

Ember Data (Beta 11)의 다형성 관계가 어떻게 변하는지를 이해하는 데 어려움을 겪고 있으며이를 설정하는 방법과 JSON 페이로드에서 예상되는 내용에 대한 업데이트 정보를 찾을 수 없습니다. 피드에 다른 유형의 항목이있는 항목 피드를 만들려고합니다. (페이스 북 피드를 생각해보십시오.) 내 모델링은 다음과 유사합니다. 나는 /activities에서 가져 않는 경우HasMany Polymorphic Relationship in Ember 데이터

App.Feedable = DS.Model.extend({ 
activities: DS.hasMany('activity') 
}); 

App.Activity = DS.Model.extend({ 
feedable: DS.belongsTo('feedable', { polymorphic: true, async: false }) 
}); 

App.MemberLikeShare = DS.Model.extend({ 
status: DS.attr('string') 
}); 

App.PhotoShare = DS.Model.extend({ 
status: DS.attr('string'), 
photo: DS.attr('string') 
}); 

나는 보이는 JSON을 다시 보내 다음

{ 
    activities: [ 
    { 
     id: 1, 
     feedable: { id: 1, type: 'memberLikeShare' } 
    }, 
    { 
    id: 4, 
    feedable: { id: 4, type: 'memberLikeShare' } 
    }, 
    { 
     id: 5, 
     feedable: { id: 5, type: 'photoShare' } 
    } 
    ], 

    member_like_shares: [ 
    { 
     id: 1, 
     status: 'Foo' 
    }, 
    { 
    id: 4, 
    status: 'Bar' 
    } 
    ], 
    photo_shares: [ 
    {id: 5, photo: 'example.jpg'} 
    ] 
} 

를이 내가 같은 오류 얻을 실행하면 :

You can only add a 'feedable' record to this relationship Error: Assertion Failed: You can only add a 'feedable' record to this relationship 

난을 내 관계가 잘못되었다고 가정하거나 잘못된 JSON을 보냈습니까?

답변

0

다형성 관계는 기본 유형을 확장해야합니다.

App.Feedable = DS.Model.extend({ 
activities: DS.hasMany('activity') 
}); 

App.MemberLikeShare = App.Feedable.extend({ 
status: DS.attr('string') 
}); 

App.PhotoShare = App.Feedable.extend({ 
status: DS.attr('string'), 
photo: DS.attr('string') 
}); 

나는 또한 그들에게 활동을 정의 할 것을 기대합니다.

member_like_shares: [ 
    { 
     id: 1, 
     status: 'Foo', 
     activites: [1,2,3,4] 
    }, 
    { 
    id: 4, 
    status: 'Bar', 
     activites: [1,2,3,4] 
    } 
    ], 
    photo_shares: [ 
    { 
     id: 5, 
     photo: 'example.jpg', 
     activites: [1,2,3,4] 
    } 
    ] 
+0

ActiveModelAdapter를 사용해야이 작업을 수행 할 수 있습니까, 아니면 RESTAdapter를 사용할 수 있습니까? – Chad

+0

신경 쓰지 마라. 누구든지 실제 예제를 찾고 있다면 여기에 JSBin이있다. http://emberjs.jsbin.com/jipomodugo/1/edit – Chad

관련 문제