我在sencha touch中定义了商店,如下所示:
store.js
Ext.define('bluebutton.store.BlueButton.Testing', {
extend: "Ext.data.Store",
requires: [
'bluebutton.model.BlueButton.Testing'
],
config: {
storeId: 'testingstore',
model: 'bluebutton.model.BlueButton.Testing'
}
});
Model.js
Ext.define('bluebutton.model.BlueButton.Testing', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.proxy.LocalStorage'
],
config: {
fields: [
{ name: 'first', type: 'string' },
{ name: 'second', type: 'string' }
],
proxy: {
type: 'localstorage',
id: '_codeAnalyzerLocalStorage'
}
}
});
我需要调用getStore()
Ext.define('bluebutton.view.BlueButton.testing', {
extend: 'Ext.form.Panel',
xtype: 'testing',
requires: [
'bluebutton.view.BlueButton.TransactionList',
'bluebutton.view.BlueButton.MemberPopUp',
'bluebutton.view.BlueButton.MemberDetail',
'bluebutton.store.BlueButton.MemberList',
'bluebutton.model.BlueButton.Testing',
'bluebutton.store.BlueButton.Testing'
],
config: {
id:'register',
items :[
{
xtype: 'textfield',
name: 'name',
label: 'Name'
},
{
xtype: 'emailfield',
name: 'email',
label: 'Email'
},
{
xtype: 'button',
text: 'Test Local',
handler: function(button) {
var test = Ext.getCmp('testingstore').getStore();
alert(test);
}
},
],
}
});
但是我收到了这个错误
getStore is undefined
请帮忙。感谢
答案 0 :(得分:5)
我尝试使用以下代码,它没有问题!
存储
Ext.define('bluebutton.store.BlueButton.Testing', {
extend : "Ext.data.Store",
requires : [ 'bluebutton.model.BlueButton.Testing' ],
config : {
storeId : 'testingstore',
model : 'bluebutton.model.BlueButton.Testing'
}
});
模型:
Ext.define('bluebutton.model.BlueButton.Testing', {
extend : 'Ext.data.Model',
requires : ['Ext.data.proxy.LocalStorage'],
config : {
fields : [{
name : 'first',
type : 'string'
}, {
name : 'second',
type : 'string'
}],
proxy : {
type : 'localstorage',
id : '_codeAnalyzerLocalStorage'
}
}
});
视图:
Ext.define('bluebutton.view.BlueButton.Testing', {
extend : 'Ext.form.Panel',
xtype : 'testing',
config : {
fullscreen : true,
id : 'register',
items : [{
xtype : 'textfield',
name : 'name',
label : 'Name'
}, {
xtype : 'emailfield',
name : 'email',
label : 'Email'
}, {
xtype : 'button',
text : 'Test Local',
handler : function(button) {
var test = Ext.getStore('testingstore');
console.log(test);
}
}]
}
});
app.js:
Ext.Loader.setConfig({
enabled : true,
});
Ext.application({
name : 'bluebutton',
views : [ 'BlueButton.Testing', ],
stores : [ 'BlueButton.Testing', ],
models : [ 'BlueButton.Testing', ],
launch : function() {
Ext.create('bluebutton.view.BlueButton.Testing');
}
});
按下按钮后,控制台会将商店记录下来。
但是当我把按钮动作放在控制器中而不是视图时,我认为这将是一个更好的代码!
button config:
{
xtype : 'button',
text : 'Test Local',
action : 'buttonTest'
}
控制器:
Ext.define('bluebutton.controller.BlueButton.Testing', {
extend : 'Ext.app.Controller',
config : {
control : {
'button[action="buttonTest"]' : {
tap : 'testButtonTap'
}
}
},
testButtonTap : function() {
var test = Ext.getStore('testingstore');
console.log(test);
}
});
答案 1 :(得分:1)
你可以通过
获得商店var test = Ext.getStore('testingstore');
Sencha Touch API:http://docs.sencha.com/touch/2-1/#!/api/Ext-method-getStore
答案 2 :(得分:0)
您必须在app.js中添加商店
你必须把商店放在商店数组中,因为storeId引用的sencha错误(据我所知,js加载应用脚本的顺序),所以如果你想使用 storeId,把你的商店放在app.js。
Ext.application({
name : 'bluebutton',
views : [ 'BlueButton.Testing', ],
stores : [ 'BlueButton.Testing', ], //this is the key
models : [ 'BlueButton.Testing', ],
launch : function() {
Ext.create('bluebutton.view.BlueButton.Testing');
}
});