1
votes

I've been working with Ember Data for a while now and I'm mostly happy with it. I like the way that it has forced me to structure my REST api in a sensible way.

I'm coming into a few problems tho, here's one of them: I was using a model with a flat structure before:

App.Pageview = DS.Model.extend({
  event: attr('string'),
  value: attr('string'),
  time: attr('date'),
  count: attr('number')
});

In this case value was a url all in one string. This model would work fine with JSON that would look like this:

{
  "event" : "pageview",
  "value" : "google.com/some/page",
  "time" : "2013-01-31T16:30:00.000Z",
  "count" : 14
}

I've changed the way the JSON is structured in the database to make it easier to query so now it looks like this:

{
  "event" : "pageview",
  "value" : {
    "url" : "google.com/some/page",
    "domain" : "google.com",
    "path" : "/some/page"
  },
  "time" : "2013-01-31T16:30:00.000Z",
  "count" : 14
}

But now i don't really know where to go with things. I tried doing things like this:

App.Pageview = DS.Model.extend({
  event: attr('string'),
  value: {
      domain: attr('string'),
      url: attr('string'),
      path: attr('string')
  },
  time: attr('date'),
  count: attr('number')
});

But that didn't seem to work, I tested it like this:

  console.log(item.get('value'));
  console.log(item.get('value.domain'));
  // console.log(item.get('value').get('domain')); // throws 
  console.log(item.get('value').domain);

And got the following results: http://d.pr/i/izDD

So I did some more digging and found that i should probably do something like this:

App.Pageview = DS.Model.extend({
  event: attr('string'),
  value: DS.belongsTo('App.SplitUrl', {embedded:true}),
  time: attr('date'),
  count: attr('number')
});

App.SplitUrl = DS.Model.extend({
  domain: attr('string'),
  url: attr('string'),
  path: attr('string')
});

But this doesn't work because of an error i get:

Uncaught TypeError: Cannot call method 'hasOwnProperty' of undefined 

This usually happens when I have sent an object down my REST api without an id. Here is the exact response from my REST api:

http://d.pr/n/UyW0

Note: i've overloaded the Adapter so that it expects the id to be in the _id field so that it can work better/easier with mongodb. This is how I have overloaded the Adapter:

App.Store = DS.Store.extend({
  revision: 11,
  adapter: DS.RESTAdapter.create({
    serializer: DS.JSONSerializer.extend({
      primaryKey: function(type) {
        return '_id';
      }
    }), 
    namespace: 'api'
  })
});

Can anyone help me figure this one out?

1
Related issue: github.com/emberjs/data/issues/53. Unfortunately, it's still open. I suggest not to use nested object currently and instead just use separate attributes domain_value, url_value, etc.Mik

1 Answers

1
votes

Assuming you're using ember-data revision 11, try to configure your value as embedded in the adapter instead.

DS.RESTAdapter.map('App.SplitUrl',{value:{ embedded:'always'}});