如何在 Sencha Touch 中重用 localstorage 中存储的数据?
我对 Sencha Touch 很陌生。我真的很喜欢这个产品,但我很难解决这个问题。请帮忙。我已经这样了3天了!!!
我能够将数据存储在本地存储中以供重复使用。但我无法在任何其他面板(或页面)和 url 中的参数中重用商店中的数据。我可以通过在事件处理程序中调用 app.UserStore.getAt(0).get('id') 等数据来使用它,但不能在 url 的参数或我分配的值中调用它。
这是否根本不可能在其他地方使用 app.UserStore.getAt(0).get('id') 格式?如果是这样,我有什么选择来解决这个问题?
-- 登录后将数据存储在本地存储中
Ext.regModel('User', {
fields: ['id', 'email'],
proxy: { type: 'localstorage', id: 'user-localstorage' }
});
app.UserStore = new Ext.data.Store({
model: 'User',
storeId: 'ConfigStore',
autoLoad: true
});
-- 重用本地存储中的数据的源(请检查源中的 app.UserStore.getAt(0).get('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);
}
I am very new to Sencha Touch. I really like the product, but I'm having trouble getting through this problem. Please help. I've been at this for 3 days!!!
I was able to store the data in localstorage to reuse. But I am not able to reuse the data from the store in any other panels (or pages) and parameters in url. I can use the data like app.UserStore.getAt(0).get('id') by calling it in the event handler but not in the url's param or the value I assign.
Is this simply NOT possible to use app.UserStore.getAt(0).get('id') format anywhere else? If so, what are my options to get around this problem?
-- stored data in localstorage after login
Ext.regModel('User', {
fields: ['id', 'email'],
proxy: { type: 'localstorage', id: 'user-localstorage' }
});
app.UserStore = new Ext.data.Store({
model: 'User',
storeId: 'ConfigStore',
autoLoad: true
});
-- source for reusing data in localstorage (please check app.UserStore.getAt(0).get('id') in the source)
- I can see the stored data*
** I get an error due to this code. (the page doesn't display) **
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你怎么看数据?您是否在 Chrome 之类的系统中使用调试控制台?你如何保存数据?
您的商店中可能有过时的数据。对未定义的对象执行
get('id')
将导致 JavaScript 错误。您应该在尝试访问存储中的数据之前发出app.UserStore.load()
命令(在本例中,可能位于initComponent
的顶部)。这将从磁盘加载数据并确保其可供使用。指定autoload
仅在创建商店时适用。关于评论:
如果您通过存储添加新数据,而不是创建对象,则该数据将位于该存储中,因此无需刷新即可访问。
因此,您不必刷新,但必须在存储上执行sync()或在模型上执行save()以确保数据被持久化。
How are you seeing the data? Are you using the debug console in something like Chrome? How are you saving the data?
You probably have stale data in your store. Performing a
get('id')
on an undefined object will result in your JavaScript error. You should issue aapp.UserStore.load()
command before trying to access data in the store (in this case, probably at the top ofinitComponent
). This will load the data off the disk and ensure it is available to use. Havingautoload
specified only applies when the store is created.Regarding the comment:
If you add the new data through the store, instead of creating an object, that data will be in that store and therefore accessible without having to refresh.
So you won't have to refresh but you will have to perform a sync() on the store or save() on the model to make sure that the data is persisted.