0

MongoDBバックエンドを使用してEmberアプリケーションを構築しようとしています。Ember-dataは埋め込みオブジェクトをサポートするようになりました。これにより、ネストされたドキュメントからオブジェクトを簡単に引き出すことができます。

私の問題は、オブジェクトを相互に関連付ける方法を理解することです。

生徒と宿題がある教室の例を見てみましょう。

{
  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' }
  ]
}

student私が彼らのすべてを引き戻すことができるように、私はどのように関係を築くでしょうassignmentsか?

関連して、私はお互いの中にネストされているオブジェクトを関連付ける方法を理解しようとしています。ネストされたオブジェクト間の関係を構築しようとしてjsbinを作成しました(ツリーを下に移動するのではなく上に移動します)が、その方法がわかりません。

4

1 に答える 1

0

以下のレポを使用して、埋め込み関連付けを行う方法をガイドできます。彼らはmongodbを使用していませんが、重要なことはember-data側にあり、組み込みの関連付けを行っています。

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

ここで App.PhoneNumber は App.Contact に埋め込まれていることに注意してください。しかし、それはあなたの問題を解決する方法についてのアイデアを与えるはずです。

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

});
于 2013-03-17T18:02:46.103 に答える