关于Ext4的mvc模式构建前端界面的问题!reader.read is not a function

来源:互联网 发布:少年包青天1抄袭 知乎 编辑:程序博客网 时间:2024/05/16 09:35

原帖发表在我的baidu个人空间.

http://hi.baidu.com/kvkens/blog/item/d40f84946d6a7204d21b7096.html

原创!

==================================================================

一直按官方API给的例子在看MVC,一直没有成功,总是出问题,本地内存数据可以加载,但是官方的远程JSON一直是错误的,写普通的EXT可以加载,在MVC下就是无法加载,显示“reader.read is not a function”,大概的意思是读取函数不存在模型是空的,也就是说,model是未赋值的,不存在才出的错误,可是model是赋值的,很奇怪,后来百度无结果,ext4在国内资源讨论还是很少的,最后我在google的时候,看到Sencha官方forum的一个帖子,我才恍然大悟 http://www.sencha.com/forum/showthread.php?136484-quot-reader.read-is-not-a-function-quot-problem-in-firefox/page2  (reader.read is not a function" problem in firefox),说的是MVC的加载顺序问题,

ok I resolve my problem...
and I understand I have to include my file in this order :
- app.js
- model.js
- store.js
- view.js
- controllers.js
otherwise I need to use Ext.require ...
 看到这个我明白了是加载顺序!

 

很显然,我们的EXT4 MVC的加载顺序是这样子的,app->controller->view->store->model

看到没,最后是model ,但是store是需要加载model的,所以导致store要去加载model的时候,model不存在,解决方法就是在store里面添加引用requires: [ 'My.model.User']

这样,store就可以在加载之前把model提前读取来,就可以访问到了。

 

 

附一下修改后的代码:

Ext.define('AM.store.Users', {
 extend : 'Ext.data.Store',
 requires : [ 'AM.model.User' ],
 model : 'AM.model.User',
 autoLoad : true,
 proxy : {
  type : 'ajax',
  api : {
   read : 'data/users.json',
   update : 'data/updateUsers.json'
  },
  reader : {
   type : 'json',
   root : 'users',
   successProperty : 'success'
  }
 }
});

Ext.define('AM.store.Users', { extend : 'Ext.data.Store', requires : [ 'AM.model.User' ],//添加引用 model : 'AM.model.User', autoLoad : true, proxy : {  type : 'ajax',  api : {   read : 'data/users.json',   update : 'data/updateUsers.json'  },  reader : {   type : 'json',   root : 'users',   successProperty : 'success'  } }});



原创粉丝点击