【发布时间】:2012-01-15 04:43:59
【问题描述】:
我有一个我一直在开发的 MVC 应用程序,但我似乎缺少一些关于命名空间和正确访问对象的基础知识。我不明白在调用 index 方法时如何加载我的商店。在我的模型中,自动加载是错误的,我使用的代码与在其他应用程序(非 MVC)中使用的代码相同,但无济于事。有什么想法吗?
这是我的 app.js:
Ext.regApplication({
name: 'MVC',
defaultUrl: 'Home/index',
launch: function(){
console.log("Launching");
this.viewport = new MVC.views.Viewport();
}
});
这是我的控制器:
Ext.regController('Update', {
// index action
index: function(){
if(networkStatus()==true){
console.log("Checking for Updated Config: " + CONFIG_URL);
MVC.stores.Updates.load();//Uncaught TypeError: Cannot read property 'Updates' of undefined
}
if ( ! this.indexView){
this.indexView = this.render({
xtype: 'UpdateIndex',
});
}
this.application.viewport.setActiveItem(this.indexView);
},
});
这是我的模特:
Ext.regModel('UpdateModel', {
fields: [
{name: 'id'},
{name: 'version'},
{name: 'date'},
{name: 'md5'},
{name: 'manifest-doc'},
{name: 'manifest-image'}
]
});
这是我的商店:
Ext.regStore('Updates', {
model: 'UpdateModel',
storeId: 'Updates',
autoLoad: false,
proxy: {
type: 'ajax',
url: 'app/data/config.min.json',
reader: new Ext.data.JsonReader({
root: 'config',
}),
timeout: 6000,
listeners: {
exception:function () {
console.log("Update Failed");
}
}
}
});
【问题讨论】:
-
商店应该用
Ext.regStore('MVC.stores.Updates' ...创建吗?我很确定regStore的第一个字符串是您正在定义的商店的命名空间。 -
在 ext.js 4 中,如果您在模型中通过 'stores : ["Storename"]' 定义了商店,则可以通过 "this.getStorenameStore()" 访问商店
-
你也可以试试 var store = Ext.StoreManager.get("Storename");
-
马特,你说得对,命名空间应该是第一个字符串。我见过人们像上面那样定义命名空间,但在我遵循的 MVC 教程的范围内,我没有看到这种约定。我查看了官方的 Sencha Touch MVC with PhoneGap 教程,他们以类似的方式定义了它,但并不完全。我会试一试...
标签: sencha-touch