1
votes

I'm using Ember.js with ember-data to make a GUI for a self made API.

I've been following this tutorial to handle authentication but I want to use ember-data instead of custom jQuery requests.

One thing I have to do is to call the API to create a new session, by sending email and password, and the API sends me back an API Key object.

Here is my LoginController handling the loginUser action (it's CoffeeScript) :

App.LoginController = Ember.ObjectController.extend
  actions:
    loginUser: ->
      session = @store.createRecord 'session',
        email:    @get 'email'
        password: @get 'password'
      session.save()

Here is the result I get when creating a session:

{
    "users": [
        {
            "id": "525fa0286c696c0b14040000",
            "email": "[email protected]",
            "first_name": "John",
            "surname": "Doe"
        }
    ],
    "api_key": {
        "id": "526e464c6c696c07d2000000",
        "type": "session",
        "key": "6b824d6a-a065-4b6f-bb28-5c19389760f8",
        "expires_at": "2013-10-28T11:41:08+00:00",
        "user_id": "525fa0286c696c0b14040000"
    }
}

I have Session, ApiKey and User models. I can create the session, but the thing I don't understand is how to access the return value of the save() method.

I know that my ApiKey and User are loaded somewhere because I get an error after save() if their respective Ember model don't exist but I don't know how to access them.

I've tried to use save() callbacks like then() or didCreate event but there's a lack of documentation about arguments passed to these callbacks and how to use them.

  • Ember.js 1.1.2
  • Ember Data 1.0.0.beta.3

EDIT: I've tried to create an actuel Session model on my API, resulting in this JSON output:

{
    "api_keys": [
        {
            "id": "526f69526c696c07d2110000",
            "type": "session",
            "key": "4c26af37-2b21-49c2-aef5-5850a396da0b",
            "expires_at": "2013-10-29T08:22:50+00:00",
            "user_id": "525fa0286c696c0b14040000"
        }
    ],
    "users": [
        {
            "id": "525fa0286c696c0b14040000",
            "email": "[email protected]",
            "first_name": "John",
            "surname": "Doe"
        }
    ],
    "session": {
        "id": "526f6e666c696c18c0010000",
        "api_key_id": "526f69526c696c07d2110000"
    }
}

(note the root element is now session)

It doesn't work better because now my save action leads to the following error (not in the console but then points to error callback):

Object function () { [...] } has no method 'eachTransformedAttribute'

I get this error, the relation between Session and ApiKey being declared in Ember Data models or not...

1

1 Answers

0
votes

Your second example JSON looks better: since you are saving a Session, I would expect a session node in the response and other models to be side loaded. You can access the saved session after it's saved by using a promise callback:

session.save().then (savedSession) =>
  console.log savedSession.api_key.key

Since you have _id relationship keys in your JSON, I assume you are using the ActiveModel adapter and its default serializer:

App.ApplicationAdapter = DS.ActiveModelAdapter.extend()