煎茶触摸覆盖Ext.Ajax的 [英] sencha touch override ext.ajax
问题描述
我在写使用Sencha架构师煎茶触摸应用程序。因为我的应用程序做大量的Ajax请求,其中大部分是需要发送令牌的请求头进行身份验证。因此,我认为创建Ext.Ajax的子类的基础,它总是在请求头中有'令牌'的。然后,我可以使用这个子类无牵挂的头。
I'm writing a sencha touch app using sencha architect. Because my app do lot of ajax request, most of it need to send 'token' in request header for authentication. So I think of create child class base on Ext.Ajax which always has 'token' in request header. Then I can use this child class without care of the header.
MyApp.override.Ajax.request({ ... })
我试着在app /覆盖/ Ajax.js定义这个
I try define this in app/override/Ajax.js
Ext.define('Myapp.override.Ajax', {
override: 'Ext.Ajax',
headers: {
'token': 'test'
}
});
我还设置这个为'需要'的应用。但得到的错误,当试图调用
I also set this as 'requires' in Application. But get error when try to call
Myapp.override.Ajax.request({ ... })
好像MYAPP无法找到.override包(MyApp.override是undifined)
Seem Myapp can not locate .override package (MyApp.override is undifined)
如何让知道的MyApp覆盖包或什么是做到这一点的正确的/最好的方法。
How to let MyApp know override package or what is the correct/best way to do this.
一个简单的例子是非常AP preciated。非常感谢你。
A quick example is very appreciated. Thank you very much.
更新信息:
覆盖文件位置:应用程序\覆盖\ Ajax.js
override file location: app\override\Ajax.js
HTML文件:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script>
var Ext = Ext || {};
Ext.theme = {
name: "Default"
};
</script>
<script src="sencha-touch-all-debug.js"></script>
<link rel="stylesheet" href="resources/css/sencha-touch.css">
<script src="app/override/Ajax.js"></script>
<script type="text/javascript" src="app.js"></script>
</head>
<body></body>
</html>
app.js文件
app.js file
Ext.Loader.setConfig({
});
Ext.application({
requires: [
'MyApp.override.Ajax'
],
views: [
'ContactDetailView'
],
name: 'MyApp'
...
应用程序可以启动没有错误,但是当呼叫MyApp.override.Ajax.request:无法读取属性阿贾克斯的定义,是指MyApp.override未定义
App can start without error, but when call MyApp.override.Ajax.request : Cannot read property 'Ajax' of undefined , mean MyApp.override is undefined
更新
下面一些消息,这更好,但还没有成型。
Here something news, it better but not working yet.
Ext.define('MyApp.Ajax', {
extend: 'Ext.data.Connection',
singleton: true,
request: function( options ) {
this.constructor(options, options.url);
console.log(options);
options.header = {'Token':'mytoken'};
this.callParent( options );
}
});
和错误,当尝试MyApp.Ajax.request()。我敢肯定,options.url被检查存在的选项日志
and error when try MyApp.Ajax.request() . I'm sure that options.url is exist in options by check the log
[ERROR][Ext.data.Connection#request] No URL specified
我添加的构造函数延长
I add extend from constructor function
constructor : function (config, url)
{
config = config || {};
//config.url = 'google.com';
this.initConfig(config);
this.callParent(config);
},
错误刚刚消失的时候,我从config.url ='google.com'删除评论;但它的config.url有Ajax请求的URL,但本地文件的URL ???我从镀铬的控制台和网络看到了什么?
Error just disappear when I remove comment from config.url = 'google.com'; but it comes that the config.url there is ajax request url but local file url ??? I see from chrome console and network ?
GET file:///C:/MyApp/assets/www/google.com?_dc=1370855149720
请帮忙。谢谢。
推荐答案
最后,这是我的工作。
Ext.define('MyApp.Ajax', {
extend: 'Ext.data.Connection',
singleton: true,
request: function( options ) {
options.headers = {
Token: 'mytoken',
};
this.callParent( [options] );
}
});
和这个简单的做我想做太多,太好了。
And this simple do what i want too, great.
Ext.Ajax.on('beforerequest', (function(conn, options, eOpts) {
options.headers = {
Token: 'mytoken',
};
}), this);
这篇关于煎茶触摸覆盖Ext.Ajax的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!