ember-data-sails-adapter icon indicating copy to clipboard operation
ember-data-sails-adapter copied to clipboard

Problem setting up SailsSocketAdapter and SailsRESTAdapter

Open moranje opened this issue 10 years ago • 4 comments

Hi I have been trying to to setup the either adapter but I am running into problem with either of the adapters. I am using a the rc7 of the sails.js 0.10 line and am using the latest SailsAdapter code available on Github. My frontend is a ember-cli project.

Both adapters seem, as far as I have figured out, seem to break on the data format that is not compliant with what ember expects (jsonapi). It breaks with the following error message:

No model was found for '<the first model property>'

If I format the sails API output so that the model data is wrapped in an object that has the model name as a key, all is well. But I would like to try to fix it on the client before I change the server output. Compare: Current output, with a model named 'sails'

{
   name: 'sails'
}

Expected

{
   sails:  {
      name: 'sails'
   }
}

I think I need to transform the data to sideload the associated models as ember expects. Can you give me a little nudge in the right direction here?

moranje avatar May 18 '14 20:05 moranje

Hi @moranje. I have been using the JSONSerializer with my project that runs on sails 0.9 but it seems like that is causing issues with sails 0.10. For now I'd suggest you check out @m19's sails serializer https://gist.github.com/m19/929475e8d26411abc091.

bmac avatar May 19 '14 13:05 bmac

Thanks, that solves things partially. The communication from the server to the app is now working, that leaves the communication from the client to the server. I will try to steal one more thought from you :), that is would this best be solved by adapting the RESTAdapter?

Is not much, but POST, PUT and DELETE request need to be unwrapped before sails can understand what ember is talking about. Like the example above, but the other way around.

Thanks again, Martien

moranje avatar May 19 '14 14:05 moranje

Hmm fair point. I can try to look into setting up a full test case tonight. But I suspect the easy work around would be to define a custom serializeIntoHash function on the RESTSerializer. Something like this should get you close to the format that sails is expecting.

serializeIntoHash: function(hash, type, record, options) {
  merge(hash, this.serialize(record, options));
},

bmac avatar May 19 '14 14:05 bmac

That works, thanks! For completeness sake here's the full function that does the trick (notice the Ember.merge instead of merge)

    /**
     * You can use this method to customize the root keys serialized into the JSON.
     * By default the REST Serializer sends camelized root keys.
     *
     * @date 2014-05-19
     * @param {Object} hash
     * @param {subclass of DS.Model} type
     * @param {DS.Model} record
     * @param {Object} options
     */
    serializeIntoHash: function(hash, type, record, options) {
        Ember.merge(hash, this.serialize(record, options));
    },

moranje avatar May 19 '14 14:05 moranje