2

I have a Post model with the following fields:

BlogApp.Post = DS.Model.extend({
    author: DS.attr('string'),
    title: DS.attr('string'),
    preamble: DS.attr('string'),
    content: DS.attr('string'),
    created: DS.attr('date'),
    comments: DS.hasMany('BlogApp.Comment'),
    lastUpdate: DS.attr('date')
});

After rendering, instead of the Post.content, the result is like:

<BlogApp.Post:ember272:1>

Other fields rendering are OK. I guess content is conflicting with some internal property. I have a few questions:

  1. is there any workaround when the REST API has names that conflict with Ember internals?
  2. is there other forbidden attribute names I should be aware?

[update]

The name clash is with the controller, not with the model. It is not a definitive list but watch out for other common database column names like:

  • model
  • transaction
  • is_new
  • is_deleted
  • store
  • errors

I guess Ember developers not as afraid of namespace-related bugs as this poor pythonist.

BTW, Angular.js guys got it right: they always prefix API attributes and methods with $ effectively preventing this kind of bug.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • I've also had clashes when using a 'container' attribute. This was trying to retrieve a hash using `get` within a controller, my 'container' attribute was always overwritten. – Kevin Ansfield May 13 '13 at 21:51

3 Answers3

4

I'm trying to answer your two questions:

1 you can define a mapping for all of you Keys, here an example for the content property

App.Adapter.map('App.Post', {
  myContent: {key: 'content'}
});

2 as far I know there is no such explicit list of reserved names in ember, but as a rule of thumb very generic names like content should be avoided (preventively)

Hope it helps

intuitivepixel
  • 23,302
  • 3
  • 57
  • 51
  • Some ORMs prefix internal/reserved attributes with an underscore to avoid clashes with user-defined properties, I would like to see this in ember-data. – Paulo Scardine May 13 '13 at 08:15
  • @PauloScardine : did you open an issue to propose this in ember-data's github ? – lauhub Sep 04 '13 at 11:58
  • 1
    @lauhub: In the mean time I found Angular.js and I'm pretty pleased with it (Angular prefixes internals with `$`). I was able to spot a bug in Angular's main loop and get a pull request accepted. It was overall a better experience than when I filled a bug regarding Ember data, which in much aspects is a superior product compared to Angular resources, but I have lost motivation to contribute to Ember. – Paulo Scardine Sep 04 '13 at 16:14
  • @PauloScardine Ok, thanks for the feedback. I may switch to Angular if Ember-Data deceives me, but I still have hope ;) – lauhub Sep 04 '13 at 16:25
  • @lauhub: Ember is nicer and is reaching production quality (has come a long way since I tried), I guess you won't be disappointed. – Paulo Scardine Sep 04 '13 at 17:54
  • I did this using model instead of content but it doesn't work. I get weird ember error - Uncaught TypeError: Object function ().... – Abhaya Mar 26 '14 at 07:56
1

I ran into the same issue, and the above solution didn't work for me. I'm using ember 1.5 and ember-data 1.0.0-beta.7+canary and I solved it by doing this:

//store.js

LS.SERIALIZATION_KEY_MAPPINGS = {
  'body': 'content'
};

LS.ApplicationSerializer = DS.ActiveModelSerializer.extend({
  keyForAttribute: function(attr) {
    if (LS.SERIALIZATION_KEY_MAPPINGS.hasOwnProperty(attr)) {
      return LS.SERIALIZATION_KEY_MAPPINGS[attr];
    } else {
      return this._super(attr);
    }
  }
});

LS.ApplicationAdapter = DS.ActiveModelAdapter.extend({
  namespace: 'api/v1'
});

//models/learning_object.js
LS.LearningObject = DS.Model.extend({
  name: DS.attr(),
  body: DS.attr(),
)};
Jurre
  • 129
  • 8
0

I had a similar issue with a model property called 'namespace' when trying to access it a handlebars each helper. I solved this in ember-cli 0.1.15 by setting the attrs property on the model's RestSerializer. So, for the above:

// file: app/serializers/post.js

export default DS.RESTSerializer.extend({
  attrs: {
    body: 'content'
  }
});

where 'body' is in your model and 'content' in the REST server payload.

For more, see http://emberjs.com/api/data/classes/DS.RESTSerializer.html#property_attrs

conormag
  • 11
  • 3