2013-07-29 35 views
0

林現就讀煎茶觸摸2,做對Ext.data.LocalStorage一些研究可以在離線模式下使用。Ext.data.LocalStorage不工作的離線模式

我試圖按照這種turorial Sencha Touch 2 Local StorageGithub - RobK/SenchaTouch2-LocalStorageExampleriyaadmiller/LocalStorage剛剛更新的代碼和修改用自己的WCF休息 但我不能讓localStorage的離線mode.I工作的商店網址有沒有問題上運行的應用程序線上。我也嘗試在Chrome開發人員工具上進行調試,但LocalStorage總是獲得0數據。我沒有使用Chrome/Safari瀏覽器,並由於Android採用的PhoneGap構建和仍然沒有工作也建立應用程序。

我錯過了什麼嗎? 沒有人能提供的細節來處理這個問題。

下面是我的代碼:

商店:

Ext.define('Local.store.News', { 
    extend:'Ext.data.Store', 


    config:{ 
     model: 'Local.model.Online', 
    proxy: 
     { 
      type: 'ajax', 
      extraParams: { //set your parameters here 
       LookupType: "Phone", 
       LookupName: "" 
      }, 
      url: 'MY WCF REST URL', 
      headers: { 
       'Content-Type': 'application/json; charset=utf-8' 
      }, 
      reader: 
      { 
       type: 'json' 
       , totalProperty: "total" 
      }, 
      writer: { //Use to pass your parameters to WCF 
       encodeRequest: true, 
       type: 'json' 
      } 
     }, 
    autoLoad: true 
    } 
}); 

離線模式:

Ext.define('Local.model.Offline', { 
    extend: 'Ext.data.Model', 
    config: { 
     idProperty: "ID", //erm, primary key 
     fields: [ 
      { name: "ID", type: "integer" }, //need an id field else model.phantom won't work correctly 
      { name: "LookupName", type: "string" }, 
      { name: "LookupDescription", type: "string" } 
     ], 
    identifier:'uuid', // IMPORTANT, needed to avoid console warnings! 
    proxy: { 
     type: 'localstorage', 
     id : 'news' 
    } 
    } 
}); 

在線型號:

Ext.define('Local.model.Online', { 
    extend: 'Ext.data.Model', 
    config: { 
     idProperty: "ID", //erm, primary key 
     fields: [ 
      { name: "ID", type: "integer" }, //need an id field else model.phantom won't work correctly 
      { name: "Name", type: "string" }, 
      { name: "Description", type: "string" } 
     ] 
    } 
}); 

控制器:

Ext.define('Local.controller.Core', { 
    extend : 'Ext.app.Controller', 

    config : { 
    refs : { 
     newsList : '#newsList' 
    } 
    }, 

    /** 
    * Sencha Touch always calls this function as part of the bootstrap process 
    */ 
    init : function() { 
    var onlineStore = Ext.getStore('News'), 
     localStore = Ext.create('Ext.data.Store', { storeid: "LocalNews", 
     model: "Local.model.Offline" 
     }), 
     me = this; 

    localStore.load(); 

    /* 
    * When app is online, store all the records to HTML5 local storage. 
    * This will be used as a fallback if app is offline more 
    */ 
    onlineStore.on('refresh', function (store, records) { 

     // Get rid of old records, so store can be repopulated with latest details 
     localStore.getProxy().clear(); 

     store.each(function(record) { 

     var rec = { 
      name : record.data.name + ' (from localStorage)' // in a real app you would not update a real field like this! 
     }; 

     localStore.add(rec); 
     localStore.sync(); 
     }); 

    }); 

    /* 
    * If app is offline a Proxy exception will be thrown. If that happens then use 
    * the fallback/local stoage store instead 
    */ 
    onlineStore.getProxy().on('exception', function() { 
     me.getNewsList().setStore(localStore); //rebind the view to the local store 
     localStore.load(); // This causes the "loading" mask to disappear 
     Ext.Msg.alert('Notice', 'You are in offline mode', Ext.emptyFn); //alert the user that they are in offline mode 
    }); 

    } 
}); 

查看:

Ext.define('Local.view.Main', { 
    extend : 'Ext.List', 

    config : { 
    id    : 'newsList', 
    store   : 'News', 
    disableSelection : false, 
    itemTpl   : Ext.create('Ext.XTemplate', 
     '{Name}-{Description}' 
    ), 
    items   : { 
     docked : 'top', 
     xtype : 'titlebar', 
     title : 'Local Storage List' 
    } 
    } 
}); 

感謝和問候

+0

很難幫助沒有發佈模型和存儲代碼.. – Viswa

+0

好吧,我會更新我的代碼後由於 – BizApps

+0

預先感謝@Viswa我試了谷歌,但大量的示例代碼沒有一個工程。希望您能夠幫助我。 – BizApps

回答

1

1)當您創建記錄,並加入到店,首先,記錄的字段應該與專賣店的示範田。

在這裏,您與現場name創造的紀錄,但Local.model.Offline沒有name

var rec = { 
    name : record.data.name + ' (from localStorage)' 
}; 

這是你需要刷新

localStore.getProxy().clear(); 

// Also remove all existing records from store before adding 
localStore.removeAll(); 

store.each(function(record) { 
    console.log(record); 
    var rec = { 
     ID : record.data.ID, 
     LookupName : record.data.Name + ' (from localStorage)', 
     LookupDescription : record.data.Description 
    }; 

    localStore.add(rec); 
}); 

// Don't sync every time you add record, sync when you finished adding records 
localStore.sync(); 

2內做什麼),如果指定idProperty在使用localStorage的模型中,記錄不會被添加到localStorage中。

型號

Ext.define('Local.model.Offline', { 
    extend: 'Ext.data.Model', 
    config: { 
     // idProperty removed 
     fields: [ 
      { name: "ID", type: "integer" }, //need an id field else model.phantom won't work correctly 
      { name: "LookupName", type: "string" }, 
      { name: "LookupDescription", type: "string" } 
     ], 
    identifier:'uuid', // IMPORTANT, needed to avoid console warnings! 
    proxy: { 
     type: 'localstorage', 
     id : 'news' 
    } 
    } 
});