2013-01-23 22 views
0

在我們的應用程序中,我們依賴sessionStorage進行持久化。但是,在移動Safari中,sessionStorage在私人模式下瀏覽時不可用。如何在商店配置生效之前執行代碼?

但是,對於那些在私人模式下使用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> 
//... 

myStore.js

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:{ 
     type: isSessionStorageAvailable ? 'sessionstorage' : 'memory', 
     id:'ccCart' 
    } 
}, 
//... 

所以,煎茶得到之前的加載,我們首先檢查了的sessionStorage和設置全局變量。因此,當商店文件加載正確的配置時被拿起。

但是,我真的不喜歡這個解決方案,因爲我不得不改變index.html文件。在開發模式中,您可以在app.js文件的任何位置寫入此檢查,因爲之後會加載所有其他文件。但在生產中並非如此。所以我想知道如何在不改變index.html文件的情況下做到這一點?

UPDATE

我還試圖用這樣的施放:

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]); 
} 

然而,第一次了一些代碼調用此存儲同步()它提高了煎茶框架內的錯誤:

這是在這一行的商店類的同步方法(它是從縮小的來源,對不起):

d.getProxy().batch({operations: b,listeners: d.getBatchListeners()}) 

它會引發錯誤,因爲getProxy()返回undefined。因此,它看起來好像施放沒有工作:(

+0

您需要返回this.callParent調用。請注意我在代碼中是如何做到的 –

+0

謝謝!有用。當我昨天嘗試時,我非常親密。但我喜歡: '返回this.callParent(代理);' 但是,當然,這失敗了,因爲在父母這兩個參數是必要的。我現在覺得很愚蠢。謝謝你的幫助! – Christoph

回答

1

使用施放...

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]); 
    } 
}); 
+0

感謝您的努力來幫助我。但它失敗了:-(請參閱我的答案更新。 – Christoph

0

只需創建你的店,像這樣:

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' 
}); 

希望這有助於

+0

我試圖在商店的構造函數中設置代理,但不幸的是,我在應用程序的其他部分發現錯誤,然後依賴於商店。 – Christoph

+0

這不是最優的。最好立刻設置它,而不是一切。 –

0

你確定else..try括號不會導致問題(JSHint抱怨)?

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; 
    } 
} 
相關問題