简体   繁体   English

如何解析DataStore Ext.define()中更深的JSON节点

[英]How to parse deeper JSON nodes inside of DataStore Ext.define()

I'm trying to implement a web-based Desktop application for administration purpose of my website. 我正在尝试实现基于Web的桌面应用程序,以管理我的网站。 When I tried to rewrite the BogusMenuModule and BogusModule which are examples of ExtJS, I was unable to get deeper nodes of JSON by using myDataStore.load({callback:function(){...}}) inside of Ext.define('MyDesktop.BasicWindowModule', {...}) . 当我试图重写BogusMenuModule和BogusModule这是ExtJS的例子,我无法使用得到JSON的更深层次的节点myDataStore.load({callback:function(){...}})Ext.define('MyDesktop.BasicWindowModule', {...}) I'm only able to get the first layer's ID. 我只能获取第一层的ID。

If myDataStore(...) is outside of Ext.define(...) it works, but the problem is that it's unable to set parameters to 'win' which is an inside variable of Ext.define(...) . 如果myDataStore(...)myDataStore(...)之外, Ext.define(...)可以工作,但是问题在于它无法将参数设置为'win',这是Ext.define(...)的内部变量。

Why I'd like to modify them is that I wanna implement a base-class module in order to pass taskbar ID to it and create a new taskbar instead of creating a new js file for my taskbar every time. 我想修改它们的原因是,我想实现一个基类模块,以便将任务栏ID传递给它,并创建一个新的任务栏,而不是每次都为我的任务栏创建一个新的js文件。

What I mean by deeper nodes is that if there's only one layer in JSON, it worked fine. 我所说的更深的节点是,如果JSON中只有一层,它就可以正常工作。 But it doesn't work if the JSON looks like: 但是,如果JSON看起来像这样,那将不起作用:

{
"BasicWindows": [
    {
        "id": 0,
        "window": {
            "id": 0,
            "name": "ControlPanel",
            "hasButton": false,
            "configs": [
                {
                    "id": 0,
                    "title": "ControlPanel",
                    "width": 640,
                    "height": 480
                }
            ]
        }
    },
    {
        "id": 1,
        "window": {
            "id": 1,
            "name": "Customers",
            "hasButton": true,
            "configs": [
                {
                    "id": 1,
                    "title": "Customers",
                    "width": 400,
                    "height": 300,
                    "button": [
                        {
                            "text": "Submit"
                        },
                        {
                            "text": "Cancel"
                        }
                    ]
                }
            ]
        }
    },
    {
        "id": 2,
        "window": {
            "id": 2,
            "name": "Reports",
            "hasButton": false,
            "configs": [
                {
                    "id": 2,
                    "title": "Reports",
                    "width": 600,
                    "height": 400
                }
            ]
        }
    }
]

} }

And my modified BogusModule looks like: 我修改过的BogusModule看起来像:

 Ext.require([
    'Ext.data.*',
    'Ext.form.*',
    'Ext.window.Window'
]);
Ext.define('BasicWindow',{
    extend: 'Ext.data.Model',
    fields: [
           {name: 'id', type:'int'}
       ],
    hasMany : {model : 'myWindow', name : 'window'}
});

Ext.define('myWindow',{
    extend: 'Ext.data.Model',
    fields: [
           {name: 'id', type:'int'},
           {name: 'name', type: 'string'},
           {name: 'hasButton', type: 'boolean'}
       ],
    hasMany : {model : 'myConfigs', name : 'configs'},   
    belongsTo: 'BasicWindow'
});

Ext.define('myConfigs', {
    extend: 'Ext.data.Model',
    fields: [
           {name: 'id', type:'int'},
           {name: 'title', type: 'string'},
           {name: 'width', type: 'int'},
           {name: 'height', type: 'int'}
       ],
    hasMany : {model : 'myButton', name : 'button'},
    belongsTo: 'myWindow'    
});

Ext.define('myButton',{
    extend: 'Ext.data.Model',
    fields: [
           {name: 'text', type:'string'}
       ],
    belongsTo: 'myConfigs'        
});

var myDataStore = Ext.create('Ext.data.Store', {
        model: 'BasicWindow',
        proxy: {
            type: 'ajax',
            url : 'js/extjs/src/desktop/json/BasicWinConfig.json',
            reader:{ 
                type:'json',
                root: 'BasicWindows'
            }
        }
});

var windowIndex = 0;
//function GetWindowName
Ext.define('MyDesktop.BasicWindowModule', {
    extend: 'Ext.ux.desktop.Module',

    init : function(){
        this.launcher = {
            //text: 'Auto Search',
            iconCls:'bogus',
            handler : this.createWindow,
            scope: this,
            windowId:windowIndex
        };
    },

    createWindow : function(src){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('BasicWindow');
        var form = new Ext.form.Panel({
                border: false,
                fieldDefaults: {
                    labelWidth: 60
                }
            });

        if(!win){
            win = desktop.createWindow({
                        autoShow: true,
                        id: 'BasicWindow',
                        //title: 'Auto Search',
                        //width: 240,
                        //height:200,
                        //minWidth: 240,
                        //minHeight: 200,
                        layout: 'fit',
                        plain: true,
                        items: form
                });

            myDataStore.load({callback:function(){
                alert('This is inside load callback');
                myDataStore.each(function(rec) {
                    var window = rec.get('window');
                    alert(window.getId());
                    rec.each(function(conf){
                        alert(conf.getId());
                        win.add({ 
                                title: config.get('title'),
                                width: config.get('width'),
                                height: config.get('height')
                        });
                    });

                });
            }});
        }
        win.show();
        return win;
    }

});

Any comment or answer would be appreciated. 任何意见或答案将不胜感激。

I figured it out! 我想到了! Add the line outside of Ext.define 将行添加到Ext.define之外

var me = this;

and then 接着

me.myDataStore.load(...);

Another question comes out. 另一个问题出来了。 How can I load myDataStore if I move Models and DataStore definition to another .js file? 如果将模型和数据存储定义移至另一个.js文件,如何加载myDataStore?

Any suggestion? 有什么建议吗?

Ext.data.StoreManager.lookup('myDataStore');

This function didn't work for ExtJS4 in my case. 在我的情况下,此功能不适用于ExtJS4。

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

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