Sails.js as the backend


#1

Hi everyone,

Any thoughts/tips/examples on using sails as the backend API for ember? Particularly, what is the best why to make the default sails rest format confirm to what ember expects? Is it maybe better to make ember accept sails format?

Thanks!


#2

I use Sails version 0.10@rc-7 for a realtime Ember app. I started with this adapter and this serializer and “hacked” them together. I’ve been thinking for a while now to make my versions open source so their quality can be improved a lot.

If there is more interest in this let me know.


#3

Thanks! So you do it with no change to the sails blueprint API?

I’m very interested in this. I’m surprised that sails doesn’t have an ember-adapter (does it?) I think sails could be an excellent backend, mostly because of its easy support for sockets.

It would have been really cool though if the integration were easier.


#4

Looking at https://github.com/bmac/ember-data-sails-adapter again, I can’t find how it deals with the problem of ember expecting a root-element in the json:

Ember: { posts: [{…},{…}] }

Sails: [{…},{…}]

Did I miss something?


#5

Yes, the blueprint api that Sails offers is untouched in my app.

You can find my version of the adapter here and my serializer here.

The serializer transforms the Sails payload to the expected Ember payload. This is the preferred way (imo) since your API might be used for other things too, editing your whole api makes no sense.

Please note that these versions are edited on the fly while developing my own app, so I wouldn’t recommend using it in production. But it should definitely help you start.


#6

Thanks a lot!!! I will check it out now and see if I can make it work.

Are you using the SailsSocketAdapter? How is that working for you?

Thanks again!


#7

Yes I use the socket adapter. Since I wanted real time functionality.

App.ApplicationAdapter = DS.SailsSocketAdapter.extend({ namespace: '/api', log: true });

I use the namespace /api in my sails config, so ignore that if you don’t.


#8

Great!

I hope I’ll be able to make it work today. Thanks!


#9

OK, plain REST now works :smile:

I’m very happy no change to the sails blueprint needed to be made. Only “pluralize: true” in the blueprint configuration, right?

Sockets is next :smile:

Thank you so much!


#10

I have pluralize: false, maybe both work. No idea if I’m honest.

For example I have this route:

App.GameRoute = Ember.Route.extend({
    model: function (params) {
        return this.store.find('game', params.game_id);
    }
});
```
It makes a request to `127.0.0.1:1337/api/game/1` when I go to `127.0.0.1:1337/#/game/1`.

Same for `Games` route

App.GamesRoute = Ember.Route.extend({ model: function () { return this.store.find(‘game’); } });


Makes a request to `127.0.0.1:1337/api/game/`, so singular, not plural.

These requests are made over sockets by default when using the `SailsSocketAdapter`.

#11

ok, using the SailsRESTAdapter and this.store.find(‘user’) I got a call to /users (plural).


#12

Hi @ofridagan. There is an unfortunate bug currently with the Ember Sails Adapter where the SailsSocketAdapter uses singular model names in its routes and the the SailsRESTAdapter uses plural model names in its routes. There is a pr that fixes that issue where I’ve been waiting on some feedback but I can merge that now and push out a new release.


#13

I’ve been using Ember Data’s built in JSONSerializer to work around Sails’s lack of a root-element in its response payload. This requires defining your relationships as ‘async’ which have some annoying edge cases in Ember Data at the moment. As a result I’d defiantly suggest go down the path of using @m19’s serializer.


#14

Thank you @bmac!

If I understand correctly the fix adds the option to use plural for SailsSocketAdapter. There is still no option to not use plural with SailsRESTAdapter, right?


#15

The fix makes it so the SailsRESTAdapter uses singular route names by default (SailsSocketAdapter was already working this way). This is in line with the default sails.js behavior.

If you would like to use plural model names in your route you will need to extend the SailsRESTAdapter and set pluralize: true" in the blueprint config.


#16

I am very interested in using Sails.js as the backend as well and will probably try it for one of the next projects. Good to see that there is some more interested. Even though I have seen the ember-sails-adapter my strategy would probably to create a blueprint to change it on the server-side (don’t like doing that kind of stuff on the client-side if I have control over the API).


#17

That’s interesting. I was also thinking about the best way to go.

I think I would also like to keep Ember as it is because it is more compatible with http://jsonapi.org/ It’s a shame jsonapi is not yet the standard.


#18

Just starting to work with Sails 0.10 and it is really simple to override the default JSON response using blueprints. Might push it to a repository so other people can save the grunt work.


#19

@GlobeCoder, really curious what you’ve made.


#20

@GlobeCoder Also curious about your Sails blueprints for Ember, since I’m more or less half way through writing/modifying my own blueprints and I also thought about releasing them open source. Maybe a good idea to do this together?