ExtJs:初始化全局变量 [英] ExtJs:Initializing a global variable

查看:272
本文介绍了ExtJs:初始化全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个全局变量,需要在加载商店时进行初始化,并且需要在另一个商店中使用该值,如下所示:

I have a global variable which needs to be initialized when the store is loaded and needs to use that value in another store as follows

var cp = 0;

Ext.onReady(function() {
    Ext.define('Init', {
        singleton: true,
        cp: 0
    });

    Ext.define('loggedUserList', {
        extend: 'Ext.data.Model',
        fields: [
            'id',
            'name'
        ]
    });

    loggedUser = Ext.create('Ext.data.Store', {
        model: 'loggedUserList',
        autoLoad: true,
        proxy: {
        type: 'ajax',
        url: url+'/lochweb/loch/users/getLoggedUser',
        reader: {
            type: 'json',
            root: 'provider'
        },
        listeners: {
            load: function(loggedUser) {
                Init.cp = loggedUser.getAt(0).data.id;
            }
        }
    }); 
});

我正在另一个网址中使用cp的值,如下所示:url: url + '/lochweb/loch/vocabulary/getVocabularyByProvider?providerId=' + Init.cp,

I am using the value of cp in another url as follows: url: url + '/lochweb/loch/vocabulary/getVocabularyByProvider?providerId=' + Init.cp,

Ext.define('vocbList', {
    extend: 'Ext.data.Model',
    fields: [
        {
            name: 'id',
            mapping: 'id'
        },
        {
            name: 'code',
            mapping: 'code'
        }
    ]
});

var vocabulary = Ext.create('Ext.data.Store', {             
    model: 'vocbList',
    autoLoad: true,
    proxy: {
        type: 'ajax',                   
        url: url+'/lochweb/loch/vocabulary/getVocabularyByProvider?providerId='+Init.cp,                    
        reader: {
            type: 'json',
            root: 'Vocabulary'
        }
    }
});

,但其值仍为0.我尝试使用(cp,Init.cp).如何分配其值表单存储区以便可以重复使用?

but its value is still 0. I tried using(cp, Init.cp). How to assign its value form store so that it can be reused?

谢谢

推荐答案

存储异步加载数据,因此您无法确定在加载另一个存储之前,将使用新值初始化Init.cp. 试试这个:

Store loads data asynchronously, so you can't be sure that Init.cp will be initialized with a new value before the other store is been loaded. Try with this:

var cp=0;

Ext.onReady(function(){
Ext.define('Init', {
    singleton: true,
    cp: 0
});

Ext.define('vocbList', {
            extend: 'Ext.data.Model',
            fields: [
                { name: 'id', mapping: 'id' },
                { name: 'code', mapping: 'code' }

            ]
        });



    var vocabulary = Ext.create('Ext.data.Store', {             
        model: 'vocbList',
        proxy: {
            type: 'ajax',                                   
            reader:  {
                type: 'json',
                root: 'Vocabulary'
            }
        }

Ext.define('loggedUserList', {
                extend: 'Ext.data.Model',
                fields: ['id','name']
            });

            loggedUser = Ext.create('Ext.data.Store', {
                model: 'loggedUserList',
                autoLoad: true,
                proxy: {
                    type: 'ajax',
                    url : url+'/lochweb/loch/users/getLoggedUser',
                    reader: {
                     type: 'json',
                        root: 'provider'
                    }                   
                },
                listeners: {
                    load:function(loggedUser){
                        Init.cp = loggedUser.getAt(0).data.id;
                        vocabulary.getProxy().url = url+'/lochweb/loch/vocabulary/getVocabularyByProvider?providerId='+Init.cp;
                        vocabulary.load();
                    }
                }

            }); 
});

如您所见,必须在刚加载第一个商店时动态设置词汇表代理的URL,然后再加载商店.

As you can see, you have to set the url of the vocabulary proxy dynamically when the first store is just loaded and then load the store.

Cyaz

这篇关于ExtJs:初始化全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆