Sending additional parameters with Ember Data

北城余情 提交于 2019-12-11 01:10:38

问题


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();

回答1:


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');
    }
});


来源:https://stackoverflow.com/questions/24992802/sending-additional-parameters-with-ember-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!