原文:Creating an Online/Offline proxy in Sencha Touch

概述

在Sencha Touch中,一个常见的需求就是,当设备在没有连接互联网的时候,应用程序必须能够继续工作。Sencha Cmd为实现应用程序离线工作提供了一切所需的工具,如自动生成应用程序清单文件,不过,这其中最大问题是如何处理数据。有许多方式可以用来处理数据,而一个常用的技术就是在本地存储代理和AJAX代理之间实现切换。

在本文,ProWeb软件公司的Tom Cooksey将展示如何使用一个代理来实现类似的效果,而且该代理的存储配置对于使用它的程序员来说是完全透明的。

代理

在示例中,虽然是从AJAX代理扩展的,但你可以根据所好,从所喜欢的代理中进行扩展,不过使用AJAX是比较常见的需求,因而使用了这个。在代理中,除了要重写两个方法来处理数据流外,还需要创建几个配置项,这个后面将会进行说明。以下是将要创建的不包含任何逻辑的骨架类:

/**
* Offline Proxy
* @extend Ext.data.proxy.Ajax
*/
Ext.define('proxy.OfflineProxy', { extend: 'Ext.data.proxy.Ajax',
alias: 'proxy.offline', config: {
storageKey: null, storageFacility: null, online: true
}, originalCallback: null, /**
* Override doRequest so that we can intercept the request and
* catch a failed request to fall back to offline
* @param operation
* @param callback
* @param scope
* @returns {*}
*/
doRequest: function(operation, callback, scope) { }, /**
* Override processResponse so that if we are online we can store the response
* into the offline storage method provided and if a response fails,
* we can fall back.
* @param success
* @param operation
* @param request
* @param response
* @param callback
* @param scope
*/
processResponse: function(success, operation, request, response, callback, scope) { } });

方法doRequest的实际用途是用来执行服务器请求的。要重写该方法是因为在这里需要判断设备是离线,还是不能访问服务器,以及是否需要将伪响应返回存储。

方法processResponse是用来解释服务器响应的,重写改方法的主要原因是除了要保留所有原始功能外,还需要将成功获取的数据存储到存储设施。如果获取数据不成功,还需要告诉代理让它再做一次,不过不是使用上面所说的伪响应。

存储设施

代理需要传递一个存储设施供它使用,在这里,它只是一个带有getItem和setItem这两个方法的单例类。也可以使用其他的任何存储设施,不过必须与示例相同的方式来实现API,它才能工作:

/**
* A class that gives access into WebSQL storage
*/
Ext.define('storage.WebSQL', { singleton: true, config:{
/**
* The database capacity in bytes (can't be changed after construction). 50MB by default.
*/
capacity:50 * 1024 * 1024
}, /**
* @private
* The websql database object.
*/
storage:null, connected: false, constructor: function (config) {
this.callParent(config); this.storage = openDatabase('storage', '1.0', 'Offline resource storage', this.getCapacity()); this.storage.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS items (key, value)');
}, function (error) {
console.error('WebSQL: Connection Error');
}, function () {
console.log('WebSQL: Connected');
});
}, /**
* Get an item from the store.
* @param key The key to get.
* @param callbacks object of success and failure callbacks
*/
getItem:function (key, callbacks) { this.storage.transaction(function (tx) {
tx.executeSql('SELECT * FROM items WHERE key = ?', [key], function (tx, results) { var len = results.rows.length; if (len > 0) {
callbacks.success(results.rows.item(0).value)
} else {
callbacks.failure(); // no result
}
});
}, function (error) {
console.log('WebSQL: Error in getItem');
callbacks.failure(error);
});
}, /**
* Set an item in the store.
* @param key The key to set.
* @param value The string to store.
* @param callbacks object of success and failure callbacks
*/
setItem:function (key, value, callbacks) { this.storage.transaction(function (tx) {
//remove old version first
tx.executeSql('DELETE FROM items WHERE key = ?', [key]);
tx.executeSql('INSERT INTO items (key, value) VALUES (?, ?)', [key, value]);
}, function (error) {
console.log('WebSQL: Error in setItem:' + error.message);
callbacks.failure(error.message);
}, function () {
callbacks.success(); // no value.
});
}
});

在这里,没什么特别的东西,但要注意的是setItem和getItem方法都要在成功或失败的时候执行回调。另外,在构造函数中创建了SQL数据库,不过这对于一些诸如本地存储这样的简单存储就不需要了。

下面来深入探讨一下setItem方法是如何工作的:

setItem:function (key, value, callbacks) {

        this.storage.transaction(function (tx) {
//remove old version first
tx.executeSql('DELETE FROM items WHERE key = ?', [key]);
tx.executeSql('INSERT INTO items (key, value) VALUES (?, ?)', [key, value]);
}, function (error) {
console.log('WebSQL: Error in setItem:' + error.message);
callbacks.failure(error.message);
}, function () {
callbacks.success(); // no value.
});
}
});

在这里将会将要设置的key(来自于代理的存储键)、新的值(在当前示例是序列号的JSON对象)和一个包含了回调的对象作为参数接收。代码将根据键值删除旧的引用并插入新的值。

以下这几行:

tx.executeSql('DELETE FROM items WHERE key = ?', [key]);
tx.executeSql('INSERT INTO items (key, value) VALUES (?, ?)', [key, value]);

对应的使用本地存储的代码是:

localstorage.removeItem(key);
localstorage.setItem(key, value);

如果该事务执行成功,就要调用传递过来的success回调,否则则调用error回调。

方法getItem的工作方式与之类似:

getItem:function (key, callbacks) {

        this.storage.transaction(function (tx) {
tx.executeSql('SELECT * FROM items WHERE key = ?', [key], function (tx, results) { var len = results.rows.length; if (len > 0) {
callbacks.success(results.rows.item(0).value)
} else {
callbacks.failure(); // no result
}
});
}, function (error) {
console.log('WebSQL: Error in getItem');
callbacks.failure(error);
});
}

在这里,只有key和callbacks两个参数。参数key是用来检索数据的,如果找到数据就调用success回调并返回数据,否则,调用error回调。

最终的代理

现在,已经有了存储设施,可以来完成代理了。要实现这个,需要在定义proxy配置项的时候将存储设施传递给它。

doRequest: function(operation, callback, scope) {

        var that = this,
passCallback,
request,
fakedResponse = {}; this.originalCallback = callback; function failedRequest() {
fakedResponse.status = 500;
fakedResponse.responseText = 'Error';
fakedResponse.statusText = 'ERROR'; that.processResponse(false, operation, request, fakedResponse, passCallback, scope);
} if(this.getOnline()) {
console.log('PROXY: Loading from online resource');
return this.callParent(arguments);
}else{
console.log('PROXY: Loading from offline resource');
request = this.buildRequest(operation);
passCallback = this.createRequestCallback(request, operation, callback, scope); if(this.getStorageKey() && this.getStorageFacility()) { this.getStorageFacility().getItem(this.getStorageKey(), {
success: function(dataString) { fakedResponse.status = 200;
fakedResponse.responseText = dataString;
fakedResponse.statusText = 'OK'; that.processResponse(true, operation, request, fakedResponse, passCallback, scope); },
failure: failedRequest
});
}else{
console.error('No storage key or facility for proxy');
setTimeout(function() {
failedRequest();
}, 1); } } },

要重写的第一个方法是doRequest方法。在原来的AJAX类,该方法用来处理实际的服务器请求,而在这里,当设备是在线的时候,将使用callParent方法来调用父类的方法,而如果设备是离线状态,则生成伪响应,从离线存储设施来获取数据。要生成伪响应是因为processResponse方法会分析传递给它的数据以确保是合法响应。伪装的方法是设置正确的http状态代码(200),设置responseText为从存储设施返回的数据,以及设置statusText为OK。伪装后的对象会让processResponse方法认为这是正常的请求响应。这种抽象方法是Sencha框架非常擅长且用来解耦代码的好东西。

processResponse: function(success, operation, request, response, callback, scope) {

        var that = this;

        if(success) {

            console.log('PROXY: Request succeeded');

            this.callParent(arguments);

            if(this.getOnline()) {
if(this.getStorageKey() && this.getStorageFacility()) {
this.getStorageFacility().setItem(this.getStorageKey(), response.responseText, {
success: function() {
console.log('PROXY: Data stored to offline storage: ' + that.getStorageKey());
},
failure: function(error) {
console.log('PROXY: Error in storing data: ' + that.getStorageKey());
}
});
}else{
console.error('PROXY: No storage key or facility for proxy');
}
} }else{
if(this.getOnline()) {
//If the request failed and we were online, we need to try and fall back to offline
console.log('PROXY: Request failed, will try to fallback to offline');
this.setOnline(false); this.doRequest(operation, this.originalCallback, scope);
}else{
this.callParent(arguments);
}
} }

第二个要重写的方法是processResponse方法。同样,在正常情况下,当服务器请求成功后,将调用callParent方法,除此之外,还要将请求返回的数据保存打离线存储设施。

在该处理过程中有几个阶段。首先,如果请求的success标志为true(即是从服务器得到了一个有效响应),则要检查代理的配置项online。该值可以在代理初始化的时候传递给代理的。或者,代理可以默认设置该值为true,直到请求失败的时候,再将设备置于离线状态。如果标志为true且存储设施存在,则存储数据并返回。每当请求成功的时候,都需要这样做,这样,每当设备离线的时候,就可以在这个时候访问到最后的数据。

如果请求失败,则设置标志online为false并重新运行doRequest方法,这时候,online标志的值为false,就可以从存储设施返回数据了。

综合使用

当将proxy设置为上面定义的存储的时候,就可以将他们糅合在一起了:

proxy: {
type : 'offline',
url : '/test-api/test-resource.json',
storageKey : 'buttons',
storageFacility : storage.WebSQL, reader : {
type : 'json',
rootProperty : 'data'
}
}

正如所看到的,将type设置为offline意味着jangle代理的别名设置为proxy.offline。配置项storageKey就是将请求返回的数据存储到离线存储的键值。在当前示例中,由于存储被定义为buttons,因此存储的键值使用了相同的名字。存储设施(storageFacility)就是上面创建的类,而其他的配置与标准的代码配置没有任何区别。

结果

为了演示这些代码,我们开发了一个Sencha Touch的演示应用程序。此外,下面还有一个屏幕截图。该演示应用程序包含一个工具栏,而它的内容则由服务器端的JSON文件决定。

在第一张图可以看到按钮已经生成了,而咋控制台,可以观察到数据已经被存储到离线存储。

在第二张图,test-resource.json文件已经不能加载了。在这里,只是修改了一下文件名,因此返回了404错误(这意味着设备不能再访问互联网或服务器已经宕机等等)。从控制台日志可以看到,作为替代,从离线版本加载了数据,而按钮也成功加载了。

小结

Sencha类系统的灵活性意味着很容易去扩展和重新利用内置组件和内置功能。通过示例就已经证明了,潜在的困难可以通过挂入已明确定义的工作流和简单添加所需功能来轻易解决。其结果就是可以在保留原有代理的强大功能的同时,添加所需的离线功能,并让开发人员可以完全透明的去使用它。

作者:Tom Cooksey
Tom is the CTO of ProWeb Software, a UK-based Sencha Partner providing dedicated Sencha resources and development internationally. He has extensive experience building web and mobile apps, using Sencha frameworks, JavaScript and Node.js, that have a complex system architecture and compelling user interface.

【翻译】在Sencha Touch中创建离线/在线代理的更多相关文章

  1. 【翻译】在Ext JS和Sencha Touch中创建自己定义布局

    原文:Creating Custom Layouts in Ext JS and Sencha Touch 布局系统是Sencha框架中最强大和最独特的一部分.布局会处理应用程序中每个组件的大小和位置 ...

  2. 【翻译】在Ext JS和Sencha Touch中创建自定义布局

    原文:Creating Custom Layouts in Ext JS and Sencha Touch 布局系统是Sencha框架中最强大和最独特的一部分.布局会处理应用程序中每个组件的大小和位置 ...

  3. 【翻译】Sencha Touch 2入门:创建一个实用的天气应用程序之三

    原文:Getting Started with Sencha Touch 2: Build a Weather Utility App (Part 3) 作者:Lee BoonstraLee is a ...

  4. sencha touch 入门系列 (三)sencha touch 项目创建

    通过上一章节的学习,我们的开发环境已经配置好了,接下来我们开始创建第一个sencha touch的项目,网络上很多sencha touch的教程都是手动搭建项目的, 不过手动搭建的项目缺少一些senc ...

  5. 【翻译】Sencha Cmd中脚本压缩方法之比较

    概述 为什么要修改默认设置 YUI压缩 Google Closure编译器 UglifyJS 案例研究Ext JS 6示例应用程序 注意事项 自定义JS压缩 小结 概述 这么多年来,Web开发人员都被 ...

  6. Sencha touch中Ext.List的使用及高度自适应

    最近在做 Sencha 的一个项目,需要用到 Ext.List 来列出所需商品及相关信息,平时我们使用 Ext.List 都是使用  fullscreen:true  来设置 List 全屏显示, 但 ...

  7. Sencha Touch中 xclass和xtype区别

    1.xclass 就是 Ext.create(xclass) 和 xtype一样的性质,不一定非要是自己创建的. 2.xtype是xclass的简称. 3.使用xtype前,你要new的对象,先要re ...

  8. sencha touch中按钮的ui配置选项值及使用效果

  9. 关于sencha touch中给文本添加焦点无效的解决方案

    目前的解决方案是给你的执行代码加上一个timeout延迟100ms+ setTimeout(function(){ SoftKeyboard.isShowing(function(isShowing) ...

随机推荐

  1. env-cmd 从文件读取配置变量

    npm install --registry=https://registry.npm.taobao.org -D env-cmd So.. 这样你在npm run build的时候会发现输出文件里面 ...

  2. Useful command for Docker

    Copy file from Container to Host: docker cp <containerId>:/file/path/within/container /host/pa ...

  3. 关于ListView中包含EditText数据复用引起异常的解决方案

    概述 前几天测试提了一个bug,在ListView中添加留言信息,导致错乱的问题.实际上就是ListView需要添加一个EditText,复用导致错乱的问题,这个问题以前也遇到过.诸如,ListVie ...

  4. 联想G510F1F2..功能键和FN+功能键反过来

    进入BIOS, 将HotKey Mode 修改为Disabled,右边有详细说明:

  5. SpriteKit给游戏弹跳角色添加一个高度标示器

    这是一个类似于跳跃涂鸦的小游戏,主角不断吃能量球得到跳跃能量向更高的地方跳跃,如果图中碰到黑洞就挂了- 在游戏调试过程中如果能实时知道主角的高度就好了,这将有助于程序猿动态的判断游戏胜败逻辑. 你可以 ...

  6. 自制DbHelper实现自动化数据库交互

    之前一直对apache的DbUtils很好奇,也很佩服其中的设计上的智慧.于是就自己模拟实现了一个更加简便的小框架.我们只需要在配置文件中写上数据库层面的连接信息,就可以随心所欲的实现自己的需求了. ...

  7. UE4 读取本地图片

    参考链接:https://answers.unrealengine.com/questions/235086/texture-2d-shows-wrong-colors-from-jpeg-on-ht ...

  8. Android动态换肤(三、安装主题apk方式)

    相比之前免安装的方式,这种方法需要用户下载并安装皮肤apk,程序写起来比免安装的要简单很多,像很多系统主题就是通过这种方式实现的. 这种方式的思路是,从所有已安装的应用程序中遍历出皮肤程序(根据特定包 ...

  9. 12 SharedPreferences

    SharedPreferences 创建方式 SharedPreferences preferences = getPreferences(Context context ,int mode); 参数 ...

  10. sublime text3空格和tab的显示

    最近在使用sublime text3修改shell文件时,明明看着相同的文件,对比却说不一样.最后发现是空格和tab惹的祸. 1.显示空格和tab: 在Preferences→Key Bindings ...