Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sencha touch 如何在存储配置生效之前执行代码?_Sencha Touch_Sencha Touch 2 - Fatal编程技术网

Sencha touch 如何在存储配置生效之前执行代码?

Sencha touch 如何在存储配置生效之前执行代码?,sencha-touch,sencha-touch-2,Sencha Touch,Sencha Touch 2,在我们的应用程序中,我们依赖会话存储来实现持久性。但是,在mobile Safari上,以私人模式浏览时,会话存储不可用 然而,对于那些在私有模式下使用Safari的客户来说,可以切换到内存中的方法。我们的应用程序在一定程度上仍然可用(刷新浏览器时会丢失数据,但由于它是一个单页应用程序,因此无论如何都不需要刷新) 在开发模式下,修复非常容易。然而,在运行生产构建之后,我面临一个巨大的问题 所以,代码胜过千言万语。以下是我们目前使用的: index.html //... <script ty

在我们的应用程序中,我们依赖会话存储来实现持久性。但是,在mobile Safari上,以私人模式浏览时,会话存储不可用

然而,对于那些在私有模式下使用Safari的客户来说,可以切换到内存中的方法。我们的应用程序在一定程度上仍然可用(刷新浏览器时会丢失数据,但由于它是一个单页应用程序,因此无论如何都不需要刷新)

在开发模式下,修复非常容易。然而,在运行生产构建之后,我面临一个巨大的问题

所以,代码胜过千言万语。以下是我们目前使用的:

index.html

//...
<script type="text/javascript">
    var isSessionStorageAvailable = true;
    var storage =  window.sessionStorage;
    if (typeof window.sessionStorage == 'undefined' || window.sessionStorage === null) {
        isSessionStorageAvailable = false;
    } else try {
        storage.setItem('__ccTest', '__ccTest');
    } catch (err) {
        isSessionStorageAvailable = false;
    }

</script>

<script id="microloader" type="text/javascript" src="../sencha/microloader/development.js"></script>
//...
因此,在加载sencha之前,我们首先检查sessionStorage并设置一个全局变量。因此,在存储文件加载时,会选择正确的配置

然而,我真的不喜欢这种解决方案,因为我不得不修改index.html文件。在开发模式下,您可以在app.js文件中的任何位置写入此检查,因为所有其他文件都是在之后加载的。但在生产中,情况并非如此。所以我想知道,在不修改index.html文件的情况下,您如何正确地执行该操作

更新

我也试着像这样使用applier:

applyProxy: function(proxy, oldProxy){

    proxy.type = 'sessionstorage';
    var storage =  window.sessionStorage;
    if (typeof window.sessionStorage == 'undefined' || window.sessionStorage === null) {
        proxy.type = 'memory';
    } else try {
        storage.setItem('__ccTest', '__ccTest');
    } catch (err) {
        proxy.type = 'memory';
    }

    this.callParent([proxy, oldProxy]);
}
但是,当一些代码第一次在此存储上调用sync()时,它在sencha框架内引发了一个错误:

它在这一行的store类的sync方法中(对不起,它来自缩小的源):


它会引发错误,因为getProxy()返回undefined。因此,似乎应用程序不起作用:(.

只需像这样创建您的应用程序商店:

Ext.define('App.store.Quote', {
extend: 'Ext.data.Store',
requires: ['App.model.QuoteItem','Ext.data.proxy.SessionStorage'],

config :{
    model: 'App.model.QuoteItem',
    autoLoad: true,
    autoSync: true,
    identifer: 'uuid'
}
然后,每当您检查sessionStorage是否可用时,只需执行以下操作

myStore.setProxy({
  type: isSessionStorageAvailable ? 'sessionstorage' : 'memory',
  id:'ccCart'
});
希望这有助于使用applier

Ext.define('App.store.Quote', {
    extend   : 'Ext.data.Store',
    requires : ['App.model.QuoteItem', 'Ext.data.proxy.SessionStorage'],

    config : {
        model     : 'App.model.QuoteItem',
        autoLoad  : true,
        autoSync  : true,
        identifer : 'uuid',
        proxy     : {
            id : 'ccCart'
        }
    },

    applyProxy : function (config, oldProxy) {
        config.type = isSessionStorageAvailable ? 'sessionstorage' : 'memory';

        return this.callParent([config, oldProxy]);
    }
});

你确定else..try括号没有引起问题(JSHint在抱怨)吗


我试图在应用商店的构造函数中设置代理,但不幸的是,我在依赖应用商店的应用程序的其他部分中遇到错误。这不是最佳选择。最好立即设置代理,而不是在一切完成后设置代理。感谢您的帮助。但失败了:-(请查看我对答案的更新。你需要返回this.callParent调用。注意我在代码中是如何做到的谢谢!它工作正常。我昨天尝试时非常接近它。但我遇到的情况是:
返回this.callParent(代理)
但是当然,这失败了,因为在家长中,这两个参数都是必需的。伙计,我现在觉得自己太笨了。谢谢你的帮助!
myStore.setProxy({
  type: isSessionStorageAvailable ? 'sessionstorage' : 'memory',
  id:'ccCart'
});
Ext.define('App.store.Quote', {
    extend   : 'Ext.data.Store',
    requires : ['App.model.QuoteItem', 'Ext.data.proxy.SessionStorage'],

    config : {
        model     : 'App.model.QuoteItem',
        autoLoad  : true,
        autoSync  : true,
        identifer : 'uuid',
        proxy     : {
            id : 'ccCart'
        }
    },

    applyProxy : function (config, oldProxy) {
        config.type = isSessionStorageAvailable ? 'sessionstorage' : 'memory';

        return this.callParent([config, oldProxy]);
    }
});
var isSessionStorageAvailable = true;
var storage =  window.sessionStorage;
if (typeof window.sessionStorage == 'undefined' || window.sessionStorage === null) {
    isSessionStorageAvailable = false;
} else {
  try {
    storage.setItem('__ccTest', '__ccTest');
  } catch (err) {
    isSessionStorageAvailable = false;
  }
}