2011-04-25 74 views
2

我對Sencha Touch很新穎。我非常喜歡這款產品,但是我很難解決這個問題。請幫忙。我已經在這3天了!如何在Sencha Touch的localstorage中重新使用存儲的數據?

我能夠將數據存儲在localstorage中以便重複使用。但是我無法在網址中的任何其他面板(或頁面)和參數中重複使用商店中的數據。我可以通過在事件處理程序中調用它來使用像app.UserStore.getAt(0).get('id')這樣的數據,但不能在url的參數或我指定的值中使用。

這是不是可以在其他地方使用app.UserStore.getAt(0).get('id')格式?如果是這樣,我有什麼選擇來解決這個問題?

- 在localStorage的存儲的數據在登錄後

Ext.regModel('User', { 
    fields: ['id', 'email'], 

    proxy: { type: 'localstorage', id: 'user-localstorage' } 
}); 

app.UserStore = new Ext.data.Store({ 
    model: 'User', 
    storeId: 'ConfigStore', 
    autoLoad: true 
}); 

- 源在localStorage的重用數據(請app.UserStore.getAt(0)獲得( 'ID')在源)

  • 我可以看到存儲的數據*

**我得到一個錯誤,由於這些代碼。 (該頁面不顯示)**

initComponent: function(){ 

    var toolbarBase = { 
     xtype: 'toolbar', 
     title: 'Group Chat' 
    }; 

     toolbarBase.items = [{ xtype: 'spacer', flex: 1 }, { 
      iconCls: 'action', 
      iconMask: true, 
      scope: this, 
      ui: 'plain', 
      handler: function(){ 

       Ext.Msg.alert('Login Sucess', *app.UserStore.getAt(0).get('id')*, Ext.emptyFn); 
      } 
     }]; 


    this.dockedItems = toolbarBase; 

    var searchModel = Ext.ModelMgr.getModel("Search"); 

    var search = new searchModel({ 
     query: **app.UserStore.getAt(0).get('id')** 
    }); 

    var store = search.tweets(); 

    var tweetList = { 
     cls: 'timeline', 
     emptyText : '<p class="no-searches">No Message Found</p>', 

     disableSelection: true, 

     store: store, 

     plugins: [ { 
      ptype: 'pullrefresh' 
     }], 

     itemCls: 'tweet', 
     itemTpl: new Ext.XTemplate(
      '<div class="avatar"<tpl if="profile_image_url"> style="background-image: url({profile_image_url})"</tpl>></div>', 
      '<div class="x-tweetanchor"></div>', 
      '<div class="tweet-bubble">', 
       '<div class="tweet-content">', 
        '<h2>{from_user}</h2>', 
        '<p>{text:this.linkify}</p><strong></strong>', 
        '<span class="posted">{created_at}</span>', 
       '</div>', 
      '</div>', 
      { 
       linkify: function(value) { 
        return value.replace(/(http:\/\/[^\s]*)/g, "<a target=\"_blank\" href=\"$1\">$1</a>"); 
       } 
      } 
     ) 
    }; 

    this.list = new Ext.List(Ext.apply(tweetList, { 
     fullscreen: true 
    })); 

    this.listpanel = new Ext.Panel({ 
     layout: 'fit', 
     items: this.list, 
     dockedItems: [{ 
      xtype: 'toolbar', 
      dock: 'top', 
      ui: 'gray', 
      items: [{ 
       xtype: 'textfield', 
       placeHolder: 'Share your thoughts with your Peers', 
       name: 'searchfield' 
      }] 
     }] 
    }); 

    this.items = this.listpanel; 
    this.list.store.load(); 

    app.views.ChatList.superclass.initComponent.call(this); 
    } 
+0

您是否收到JavaScript錯誤? – 2011-04-28 22:44:11

回答

1

您是如何看到這些數據的?您是否使用Chrome等調試控制檯?你如何保存數據?

您的商店可能有陳舊的數據。在未定義的對象上執行get('id')將導致您的JavaScript錯誤。在嘗試訪問商店中的數據之前,您應該發出app.UserStore.load()命令(在這種情況下,可能位於initComponent的頂部)。這將從磁盤加載數據並確保它可用。指定autoload僅適用於創建商店。

關於評論: 如果通過商店添加新數據而不是創建對象,那麼該數據將位於該商店中,因此無需刷新即可訪問。

var user = app.UserStore.add({email: '[email protected]'}); 
user.save() //OR app.UserStore.sync(); 

所以你不會有刷新,但你必須在模型上的商店進行同步()或保存(),以確保數據的持久化。

+0

非常感謝。我根據你的建議改變了它,它的工作原理!再次感謝您的幫助。 – 2011-05-07 00:24:00

+0

我還有一個問題... – 2011-05-07 01:10:41

+0

我還有一個問題...我可以重新使用所有其他面板的所有本地存儲數據。但是,當我清除或恢復本地存儲數據時,在其他面板中不起作用,除非我刷新整個事物。所以最終的結果是結果總是比行動落後一步。我做了更改後的store.sync()。我的問題是......如何在同一個面板中重新使用存儲的數據,以便我不必刷新每個面板,尤其是在卡內? – 2011-05-07 01:22:02

相關問題