没有ID的Ember数据建立关系 [英] Ember-data building relationships without ids

查看:47
本文介绍了没有ID的Ember数据建立关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MongoDB后端构建Ember应用程序。现在,Ember-data支持嵌入式对象,这使将对象直接从嵌套文档中拉出变得非常容易和出色。

I'm trying to build an Ember application with a MongoDB backend. Ember-data now supports embedded objects which makes it really easy and great to be able to pull the objects right out of the nested document.

我的问题是弄清楚如何将对象彼此关联。

My problem is in figuring out how to relate objects to each other.

让我们看一个示例教室,其中有学生和作业。

Lets look at an example of a class room with students and assignments.

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

我如何为学生这样我就可以撤回他们所有的任务

How would I build a relationship for the student so that I could pull back all of their assignments?

相关,我正在尝试找出如何关联彼此嵌套的对象。我创建了一个 jsbin ,试图在嵌套对象之间建立关系(沿着树而不是仅仅靠树) ),但我不确定该怎么做。

Related, I'm trying to figure out how to relate objects that are nested inside each other. I created a jsbin trying to build a relationship between nested objects (going up the tree rather than just down) but I'm not sure how to do it.

推荐答案

您可以使用下面的存储库来指导您如何做嵌入式关联。尽管他们没有使用mongodb,但重要的是在ember-data方面,他们正在进行嵌入式关联。

You can use the repo below to guide you on how to do embedded association. Though they are not using mongodb, the important thing is on the ember-data side, they are doing embedded association.

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

请注意,此处App.PhoneNumber嵌入在App.Contact中。但这应该会让您对如何解决自己的想法有所了解。

Note that here App.PhoneNumber is embedded in App.Contact. But it should give you an idea on how to go abut resolving yours.

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

});

这篇关于没有ID的Ember数据建立关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆