Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch embedded objects in one request with ember-data and couchdb-adapter

I am working on an application using ember.js and a couch DB backend. So far, i used ember-resource as database driver, but I am considering switching to ember-data, since this seems to be more sustainable.

Since I am working with couch DB, I am using the Couch DB-Adapter.

The documents in my database contain complete object structures, so I have to specify embedded objects in the database driver.

But although I am specifying my sub-objects as embedded, ember-data seems to fetch these objects with separate requests, instead of just getting them out of the main json.

My object definitions are as follows:

App.UserProfile = DS.Model.extend({
    type:              DS.attr('string'),
    fullname:          DS.attr('string'),
    email:             DS.attr('string'),
    pictureUrl:        DS.attr('string'),
    social:            DS.hasMany('App.SocialWebAccount', { embedded: true }),
    .....
});

App.SocialWebAccount = DS.Model.extend({
    profile: DS.belongsTo('CaiMan.UserProfile'),
    site:    DS.attr('string'),
    account: DS.attr('string'),
    .....
});

and the server data ist something like this:

{
  "_id": "thoherr",
  "_rev": "55-d4abcb745b42fe61f1a2f3b31c461cce",
  "type": "UserProfile",
  "fullname": "Thomas Herrmann",
  "email": "[email protected]",
  "pictureUrl": "",
  "social": [
      {
          "site": "socialFacebook",
          "account": "thoherr"
      },
      {
          "site": "socialXing",
          "account": "Thomas_Herrmann7"
      },
      {
          "site": "socialEmail",
          "account": "[email protected]"
      }
   ]
}

After loading, the UserProfile does contain an ArrayProxy for my social data, which is populated by three entries, but they are all undefined instead of instances of SocialWebAccount!

If i try to access this array, ember-data seems to do a separate database access to fetch the data, which then leads to an error because the couch DB-adapter accesses an _id field, which is not available in undefined....

What am i missing?

I thought the "embedded" flag signals that the data is already in the json and the objects can be instantiated from the json?

Why does ember-data try to fetch the embedded data?

Any hint?

like image 979
Thomas Herrmann Avatar asked Dec 01 '25 18:12

Thomas Herrmann


1 Answers

It seems that the embedded option has changed recently. I found some information in the test files on the ember-data github.

In these test files, the embedded content is defined like this

Comment = App.Comment = DS.Model.extend({
  title: attr('string'),
  user: DS.belongsTo(User)
});

Post = App.Post = DS.Model.extend({
  title: attr('string'),
  comments: DS.hasMany(Comment)
});
Adapter = DS.RESTAdapter.extend();
Adapter.map(Comment, {
  user: { embedded: 'always' }
});

or

Adapter = DS.RESTAdapter.extend();
Adapter.map(Comment, {
  user: { embedded: 'load' }
});

'always' seems to be used for embedded data without ids (your case),eg

id: 1,
title: "Why not use a more lightweight solution?",
user: {
  name: "mongodb_expert"
}

'load' seems to be used for embedded data with ids

id: 1,
user: {
  id: 2,
  name: "Yehuda Katz"
}

Hoping it will help in your particular case. I've had a lot of trouble with hasMany relationships recently (I had to modify my adapter to get it work)

like image 150
Twark Avatar answered Dec 03 '25 17:12

Twark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!