2011-04-25 31 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

相关问题