使用Ember Data发送附加参数 [英] Sending additional parameters with Ember Data

查看:85
本文介绍了使用Ember Data发送附加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有EmberJS和Ember Data的Pusher。为了避免推送通知回到同一个客户端,我想始终发送每个API调用的Pusher socketId。



我可以从我的控制器的行动,但不知道如何发回。



例如

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

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

问题是,每个控制器的socketId会有所不同。所以我不能直接在适配器中。



这样的东西是理想的...

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

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


解决方案

我做了类似的事情,最好的地方是在适配器,所以你只需要做一次。我会使用 RESTAdapter 给出我的例子,因为它是最常见的,但它可能适用于任何适配器。

  App.ApplicationAdapter = DS.RESTAdapter.extend({
ajaxOptions:function(url,type,hash){
var options = this._super(url,类型,哈希);
options.headers = options.headers || {};
//我不知道`push'是什么,但你可以随时注入到你的适配器
options.headers.socketId = this.pusher.get('socketId');
return options;
}
});

现在,每个API请求都将包含 socketId 标题。



编辑:注入推杆:

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


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

解决方案

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

这篇关于使用Ember Data发送附加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆