GlennJ
GlennJ

Reputation: 167

Sending additional parameters with Ember Data

I'm using Pusher with EmberJS and Ember Data. To avoid the push notifications coming back to the same client, I would like to always send the Pusher socketId with each API call.

I can get the socketId from within my controller's actions, but don't know how to send it back.

e.g.

        var socketId = this.pusher.get('socketId');

        var project = this.get("model");
        project.save();

The problem is, the socketId will be different for each controller. So I can't do it directly in the adapter.

Something like this would be ideal...

        var socketId = this.pusher.get('socketId');

        var project = this.get("model");
        project.ajaxParams = { socketId: socketId };
        project.save();

Upvotes: 2

Views: 431

Answers (1)

GJK
GJK

Reputation: 37369

I do something similar to this, and I think the best place to do it is in the adapter, so you only have to do it once. I'll give my example using the RESTAdapter because it's most common, but it'll likely work with any adapter.

App.ApplicationAdapter = DS.RESTAdapter.extend({
    ajaxOptions: function(url, type, hash) {
        var options = this._super(url, type, hash);
        options.headers = options.headers || {};
        // I don't know what `pusher` is, but you can always inject it into your adapter
        options.headers.socketId = this.pusher.get('socketId');
        return options;
    }
});

Now, every API request will include the socketId header.

EDIT: To inject the pusher:

Ember.Application.initializer({
    name: 'injectPusherIntoStore',
    after: 'pusherConnected',
    initialize: function(container, application) {
        application.inject('adapter', 'pusher', 'pusher:main');
    }
});

Upvotes: 1

Related Questions