简体   繁体   English

Ember.RSVP.Promise与AJAX通话

[英]Ember.RSVP.Promise with AJAX call

Hi I'm trying to understand the way promises work along with Ajax calls. 嗨,我正在尝试理解Promise与Ajax调用一起工作的方式。 I want to send a simple Ajax call to a certain end point but I'm really confused with the code syntax. 我想将一个简单的Ajax调用发送到某个端点,但是我真的对代码语法感到困惑。 Here is my code: 这是我的代码:

The purpose of the code is to invalidate the session of the user. 该代码的目的是使用户会话无效。 First, the client should send an ajax call to the logout endpoint providing the user's token and then if the server responds successfully, we call the invalidate() method to clear the user's data from the client side. 首先,客户端应向提供用户令牌的注销端点发送ajax调用,然后,如果服务器成功响应,我们将调用invalidate()方法以从客户端清除用户的数据。

import Ember from 'ember';

export default Ember.Controller.extend({
    session: Ember.inject.service('session'),

    actions: { 
       invalidateSession() {
        const url = "http://namespace/logout/";
        let logoutRequest = new Ember.RSVP.Promise(function(r,e){
            r(this.ajax(url, "PUT", {auth_token});)
        },
        if (logoutRequest) {
            this.get('session').invalidate();
        }
      }
    }
});

I Know that my code doesn't working but I can't put it all together... 我知道我的代码无法正常工作,但无法将所有内容放在一起...

Using ember-ajax this should work: 使用ember-ajax应该可以工作:

ajax: Ember.inject.service(),
....
actions: {
 invalidateSession(){
   return this.get('ajax').request('url_goes_here', {
      method: 'PUT',
      data: {
        some: "additional data"
       },
     }).then(function(success) {
        // inform about successful logout, redirect...
     }).catch(function(error) {
        // handle or throw error 
     });
    }
}

Update : If you want to wrap it around Promises, try this: ... 更新 如果您想将其包装在Promises中,请尝试以下操作: ...

As @locks mentioned, it is recommended to use the ember-ajax library instead of using raw Ember.$.ajax() . 如@locks所述,建议使用ember-ajax库,而不要使用原始Ember.$.ajax() Since this.get('ajax').request() returns a promise, there is no need to wrap it around promises anymore. 由于this.get('ajax').request()返回了诺言,因此不再需要将其包装在诺言中。

However, if all you want to do is logout, I encourage you to take a look at the ember-simple-auth library (addon) which has already solved many features you may need in future (login, logout, invalidation, redirection to login route...). 但是,如果您只想注销,那么我建议您看一下ember-simple-auth库(插件),该库已经解决了将来可能需要的许多功能(登录,注销,失效,重定向到登录)路线...)。

You can't use this.ajax from a callback like that. 您不能从这样的回调中使用this.ajax But it's unnecessary anyway, as you don't need a new Promise when ajax(…) already returns one. 但是无论如何,这都是不必要的,因为当ajax(…)已经返回一个new Promise时,您不需要。 So do just 就是这样

invalidateSession() {
    const url = "http://namespace/logout/";
    let logoutRequestPromise = this.ajax(url, "PUT", {auth_token});
    return logoutRequestPromise.then((logoutRequestResult) => {
        if (…) {
            this.get('session').invalidate();
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM