在模型中加载数据时实现自定义ajax调用 [英] Implementing custom ajax calls when loading data in model

查看:83
本文介绍了在模型中加载数据时实现自定义ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Emberjs应用程序中,我有一个Employee模型,我应该通过一个REST Get API调用加载,我必须首先验证一个令牌,然后开始加载数据,我知道如何使用JQuery轻松地这样做不知道我如何在EmberJS中实现这一点,所以如果有人可以指示我如何做,我将非常感激。



下面是JQuery代码认证,提取员工数据以及我的EmberJS型号代码



感谢



认证:


  $ .ajax 
({
type:POST,
url:http://portal.domainname.com/auth,
dataType:'json',
async:false,
data:JSON.stringify({
登录:logmein@email.com,
密码:test
}),
success:function(data){
console.log(data); //这里我得到进一步的电话需要的令牌...
},
错误:f unction(xhr,error){
console.debug(xhr); console.debug(误差);
}
});


调用加载员工数据:


  $。ajax({
type:GET,
url:http://
dataType:'json',
async:false,
beforeSend:function(xhr){
xhr.setRequestHeader(Token 0000000-0000-0000-0000-00000000);
},
success:function(data){
console.log(data);
},
error:function(xhr,error){
console.debug(xhr); console.debug(error);
}});


EmberJS模型

  App.Store = DS.Store.extend({
revision:11
});

App.Employee = DS.Model.extend({
employeeID:DS.attr('string'),
employeeName:DS.attr('string')
});

App.Store.reopen({
adapter:'DS.RESTAdapter'
});


解决方案

您可以向所有Ember AJAX请求添加标题:

  App.Store = DS.Store.extend({
revision:13,
adapter: DS.RESTAdapter.extend({
ajax:function(url,type,hash){
if(!hash){
hash = {};
}
hash.beforeSend = function(xhr){
xhr.setRequestHeader(Authorization,Token+ window.sessionToken);
};
返回this._super(url,type,hash );
}
})
});

我在生产中使用这段代码。


In my Emberjs application I have an Employee model which I should load through a REST Get API call, where I have to authenticate the API first for a token then start loading the data, I know how to do this easily using JQuery but not sure how I can implement this in EmberJS, so I will appreciate it so much if anyone can instruct me how to do so.

Below is the JQuery code I use for authentication, extracting the employees data, as well as my EmberJS model code

Thanks

Authentication:

 $.ajax
  ({
    type: "POST",
    url: "http://portal.domainname.com/auth",
    dataType: 'json',
    async: false,
    data: JSON.stringify({ 
        Login: "logmein@email.com", 
        Password : "test"
    }),
    success: function(data) {
        console.log(data); //Here I get the token needed for further calls...
    },
    error: function(xhr, error){
        console.debug(xhr); console.debug(error);
    } 
});

Calls to load employees data:

$.ajax   ({
    type: "GET",
    url: "http://portal.domainname.com/employees",
    dataType: 'json',
    async: false,
    beforeSend: function (xhr) {
        xhr.setRequestHeader ("Token", "0000000-0000-0000-0000-00000000");
    },
    success: function(data) {
        console.log(data);
    },
    error: function(xhr, error){
        console.debug(xhr); console.debug(error);
    }  });

EmberJS Model

App.Store = DS.Store.extend({
  revision: 11
});

App.Employee = DS.Model.extend({
  employeeID:             DS.attr('string'),
  employeeName:        DS.attr('string')
});

App.Store.reopen({
 adapter: 'DS.RESTAdapter'
});

解决方案

You can add headers to all Ember AJAX requests like this:

App.Store = DS.Store.extend({
  revision: 13,
  adapter: DS.RESTAdapter.extend({
    ajax: function(url, type, hash) {
      if (!hash) {
        hash = {};
      }
      hash.beforeSend = function(xhr) {
        xhr.setRequestHeader("Authorization", "Token " + window.sessionToken);
      };
      return this._super(url, type, hash);
    }
  })
});

I use this code in production.

这篇关于在模型中加载数据时实现自定义ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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