Flexer
Flexer

Reputation: 253

Just created entity with ember-data doesn't have its state set

I use Ember + Ember Data. When I create an entity even after the corresponding server side call its state attributes (isLoaded, isNew, isValid, etc.) are all undefined. My expectation is that these should be properly set. For example, before the server side call I expect isNew to be 'true' and after the creation on the server side I expect isNew to be 'false'.

I am creating the enity this way:

myEntity = MyApp.MyEntity.createRecord({attr1: "value1"});
...
myEntity.transaction.commit();

What goes back and forth is:

Sent:
{"myEntity":{"attr1":"value1"}}
...
Received:
{"myEntity":{"id":2,"attr1":"value1","attr2":"value2"}}

The entity itself is defined like this:

MyApp.MyEntity = DS.Model.extend({
    attr1: DS.attr("string"),
    attr2: DS.attr("string")
});

Unfortunately before and after the remote call 'myEntity' has all state attributes as 'undefined'. Am I missing something?

Thanks!

Upvotes: 0

Views: 104

Answers (2)

Flexer
Flexer

Reputation: 253

My problem was that I was retrieving the state directly i.e. without using the getter.

WRONG: myEntity.isNew -> undefined

RIGHT: myEntity.get('isNew') -> true

Upvotes: 0

Myslik
Myslik

Reputation: 1158

I would guess that problem could be that you are not following naming conventions of Ember Data. Your MyApp.MyEntity is going to be posted to /my_entity in case you are using default DS.RESTAdapter and Ember Data will be looking for the model in the response under my_entity or my_entitys keys.

There is also possibility to customize this default behavior by using map function on App.Adapter.

Upvotes: 0

Related Questions