简体   繁体   English

如何将这个小代码从Extjs3升级到Extjs4.2?

[英]How to upgrade this small code from Extjs3 to Extjs4.2?

I have been trying to find a solution to upgrading the code below from ExtJs3.4 to ExtJs 4.2 ? 我一直试图找到一个解决方案,将下面的代码从ExtJs3.4升级到ExtJs 4.2
I found some answers and looked into Sencha's docs, but still having hard time with it. 我找到了一些答案,并查看了Sencha的文档,但仍然很难用它。

If anyone knows how to rewrite this code in ExtJs4.2, please let me know. 如果有人知道如何在ExtJs4.2中重写此代码,请告诉我。 Thanks in advance. 提前致谢。

        var config = 
        {       
            store: new Ext.data.Store({
                autoLoad: true,
                proxy: new Ext.data.HttpProxy({ url: '/main/...' }),
                reader: new Ext.data.JsonReader
                ({
                    root: 'data',
                    totalProperty: 'totalCount',
                    id: 'id',
                    fields: 
                    [
                        'alert_id',
                        {name: 'duration', type: 'int'},
                        {name: 'start_date', type: 'date', dateFormat: 'timestamp'},
                        {name: 'end_date', type: 'date', dateFormat: 'timestamp'},
                        'monitor'
                    ]
                })
            }),
        }

        // more code here

This is what I know from the code above: 这是我从上面的代码中得知的:

  • Models are using the field not Stores anymore Models使用的field不再是Stores
  • reader should be inside the proxy reader应该在proxy内部

These are the warning 这些都是警告

[DEPRECATED][4.0][Ext.data.Store]: Passing a "fields" config via the store's reader config is no longer supported. Instead you should configure a model and pass it as the store's "model" config. Please see the header docs for Ext.data.Store for details on properly setting up your data components.
ext-all.js (line 21)
[DEPRECATED][4.0][Ext.data.Store] reader (config): The reader config should now be specified on the configured proxy rather than directly on the store.


Addendum 附录

I was doing it this way at first: 我一开始就是这样做的:

  Ext.define('User', { extend: 'Ext.data.Model', id: 'id', fields: [ 'alert_id', {name: 'duration', type: 'int'}, {name: 'start_date', type: 'date', dateFormat: 'timestamp'}, {name: 'end_date', type: 'date', dateFormat: 'timestamp'}, 'monitor' ] }); var config = { store: new Ext.data.Store({ model:'User', proxy: { url: '/main/...', reader: new Ext.data.JsonReader ({ root: 'data', totalProperty: 'totalCount', }) } }), // more code here } 

So I was not sure what to write instead of reader: new Ext.data.JsonReader . 所以我不确定写什么而不是reader: new Ext.data.JsonReader
Also whether to use the Model or not since Store was not using fields anymore. 此外,是否使用Model ,因为Store不再使用fields
I had no idea about Ext.data.JsonStore until I saw the answer. 在看到答案之前,我根本不知道Ext.data.JsonStore

How about this? 这个怎么样? The JsonStore is documented at http://docs-origin.sencha.com/extjs/4.2.0/#!/api/Ext.data.JsonStore . JsonStore记录在http://docs-origin.sencha.com/extjs/4.2.0/#!/api/Ext.data.JsonStore中

initComponent: function () {

    var config = {
        store: new Ext.data.JsonStore({
            autoLoad: true,
            fields: [ 'alert_id', 'duration', 'start_date', 'end_date' ],
            proxy: {
                type: 'ajax',
                url: '/main/...',
                reader: {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'totalCount'
                }
            }
        })
    }
}

Chris Farmer's answer is correct. Chris Farmer的回答是正确的。 However, here's a more thorough explanation. 但是,这是一个更彻底的解释。

Ext now encourages you to document the format of your data, so you should use an Ext.data.Model to define the field names. Ext现在鼓励您记录数据的格式,因此您应该使用Ext.data.Model来定义字段名称。 The suggested way to do it, is to define the proxy on the model itself so it can be loaded independent of a store 建议的方法是在模型本身上定义代理,以便可以独立于商店加载它

// File 1
Ext.define('my.User', {
     extend: 'Ext.data.Model',
     fields: [
         'alert_id',
         {name: 'duration', type: 'int'},
         {name: 'start_date', type: 'date', dateFormat: 'timestamp'},
         {name: 'end_date', type: 'date', dateFormat: 'timestamp'},
         'monitor'
     ],
     proxy: {
        type: 'ajax',
        url: '/main/...',
        // Reader is now on the proxy, as the message was explaining
        reader: {
            type: 'json',
            root: 'data',
            totalProperty: 'totalCount'
        }
     }
 });

 // File 2
 // Use the model with the store
 var config = {
     // Passing the model definition to the store as the message was explaining
     store: new Ext.data.JsonStore({model:  'my.User', autoLoad: true})
 };

Ext still allows you to use fields definitions instead of models when creating stores, but it's not recommended, an implicit model will be created. Ext仍允许您在创建商店时使用字段定义而不是模型,但不建议使用隐式模型。 Here's how to do it. 这是怎么做的。

 var config = {
     store: new Ext.data.JsonStore({
         fields: [
             'alert_id',
             {name: 'duration', type: 'int'},
             {name: 'start_date', type: 'date', dateFormat: 'timestamp'},
             {name: 'end_date', type: 'date', dateFormat: 'timestamp'},
             'monitor'
         ],
         proxy: {
             type: 'ajax',
             url: '/main/...',
             reader: {
                 type: 'json',
                 root: 'data',
                 totalProperty: 'totalCount'
             }
         }
     });
 };
initComponent: function () {

this.store= new Ext.data.JsonStore({
        autoLoad: true,
        fields: [ 'alert_id', 'duration', 'start_date', 'end_date' ],
        proxy: {
            type: 'ajax',
            url: '/main/...',
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'totalCount'
            }
        }
    });

 this.callParent(arguments);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM