2013-03-15 5 views
0

MongoDB 백엔드로 Ember 애플리케이션을 빌드하려고합니다. Ember-data는 이제 포함 된 객체를 지원하므로 중첩 된 문서에서 객체를 바로 잡아낼 수 있습니다.id가없는 엠버 데이터 구축 관계

제 문제는 객체를 서로 관련시키는 방법을 찾는 것입니다.

학생들과 과제가있는 강의실의 예를 살펴 봅니다.

{ 
    students: [ 
    { name: 'Billy' }, 
    { name: 'Joe' } 
    ], 
    assignments: [ 
    { name: 'HW1', score: 70, student_name: 'Billy' }, 
    { name: 'HW2', score: 80, student_name: 'Billy' }, 
    { name: 'HW1', score: 60, student_name: 'Joe' }, 
    { name: 'HW2', score: 75, student_name: 'Joe' } 
    ] 
} 

어떻게 내가 다시 모든 assignments의를 당길 수 있도록 student에 대한 관계를 구축 할 ?

관련, 나는 서로 내부에 중첩 된 개체를 연결하는 방법을 알아 내려고하고 있습니다. 난 jsbin 중첩 된 객체 사이의 관계를 만들려고 노력했다. (트리가 아니라 아래로 올라간다.) 어떻게해야할지 모르겠다.

답변

0

아래의 repo를 사용하여 삽입 된 연결을 수행하는 방법을 안내 할 수 있습니다. 그들이 몽고 (mongodb)를 사용하지는 않지만, 중요한 것은 엠버 데이터 측면에 있으며, 그들은 임베디드 연합을하고 있습니다. 여기 App.PhoneNumber가 App.Contact에 포함

https://github.com/dgeb/ember_data_example/blob/master/app/assets/javascripts/controllers/contact_edit_controller.js

참고. 그러나 그것은 당신에게 당신의 문제를 해결하는 방법에 대한 아이디어를 줄 것입니다.

App.Contact = DS.Model.extend({ 
    firstName: DS.attr('string'), 
    lastName: DS.attr('string'), 
    email: DS.attr('string'), 
    notes: DS.attr('string'), 
    phoneNumbers: DS.hasMany('App.PhoneNumber'), 
}); 

App.PhoneNumber = DS.Model.extend({ 
    number: DS.attr('string'), 
    contact: DS.belongsTo('App.Contact') 
}); 

https://github.com/dgeb/ember_data_example/blob/master/app/assets/javascripts/store.js

App.Adapter = DS.RESTAdapter.extend({ 
    bulkCommit: false 
}); 

App.Adapter.map('App.Contact', { 
    phoneNumbers: {embedded: 'always'} 
}); 

App.Store = DS.Store.extend({ 
    revision: 12, 
    adapter: App.Adapter.create() 
}); 

https://github.com/dgeb/ember_data_example/blob/master/app/assets/javascripts/controllers/contact_edit_controller.js

App.ContactEditController = Em.ObjectController.extend({ 
needs: ['contact'], 

startEditing: function() { 
    // add the contact and its associated phone numbers to a local transaction 
    var contact = this.get('content'); 
    var transaction = contact.get('store').transaction(); 
    transaction.add(contact); 
    contact.get('phoneNumbers').forEach(function(phoneNumber) { 
    transaction.add(phoneNumber); 
}); 
    this.transaction = transaction; 
    }, 

    save: function() { 
    this.transaction.commit(); 
    }, 

    addPhoneNumber: function() { 
    this.get('content.phoneNumbers').createRecord(); 
    }, 

}); 
+0

내가 가까이 그것을 확인해야합니다,하지만 당신은 참조 할 수 있다면 당신은 PHONENUMBER을 일단 당신이 알고 않습니다 접촉? 그 뒤의 관계는 나에게 힘든 시간을주는 부분이다. – RyanJM

+0

App.Adapter.map 줄에 다음과 같이 표시됩니다. Uncaught TypeError : 정의되지 않은 'map'메서드를 호출 할 수 없습니다 ... 'ember1.0에서 변경된 것 같습니다. –

+0

현재 버전. 데이터가 많이 바뀌었고이 코드의 일부 내용이 더 이상 적용되지 않습니다. ** RestAdapter는 더 이상 자동으로 수행되므로 선언 할 필요가 없지만 jsfiddle에서 ** fixtureAdapter를 사용하는 경우 선언해야합니다. 또한 확장하려면 ** App.Store **를 정의하지 않아도됩니다. ** 퍼가기 **, 관계가 일시적으로 제거되지만 나중에 추가됩니다. ** rails ** 및 ** emberjs **에서는 ember-data와 함께 번들로 제공되는 ** activemodeladapter **를 사용하십시오. 정의 ** 관계 **가 변경되었습니다. 참조 : ** http : //bit.ly/14Pl9Av** – brg

관련 문제