using extjs Store from Different JS file

1.7k views Asked by At

this is CreateUI.js

Ext.onReady(function() {
    Ext.QuickTips.init(); //tips box


    Ext.define('RouteModel', {
        extend: 'Ext.data.Model',
        fields: [{name: '_id', type: 'number'}, 'Route_Code','Route_Name','Name','AddBy_ID']
    });

    Ext.override(Ext.data.Connection, {
    timeout : 840000
    });

    var RouteNameStore = Ext.create('Ext.data.JsonStore', {
        model: 'RouteModel',
        autoLoad: true,
        proxy: {
            type: 'ajax',
            url: 'get-routename.php',
            api: {
                    create: 'insert-routename.php',
                    //read: 'http://visual04/ModuleGestion/php/Pays.php?action=read',
                    update: 'update-routename.php',
                    //destroy: 'http://visual04/ModuleGestion/php/Pays.php?action=destroy'
                },

            reader: {
                type: 'json',
                idProperty: '_id'
            },
            writer: {
                type: 'json',
                id: '_id'

             }
        }
    });

})

routename.js

RouteNameStore.add ({
          Route_Code: txtAddRouteCode,
          Route_Name: txtAddRouteName,
          AddBy_ID: getCookie('User_ID')
        }); 

and this is the index.html page to link this two js file

 <script type="text/javascript" src="CreateUI.js?"></script>
   <script type="text/javascript" src="routename.js?"></script>

fire bug error

ReferenceError: RouteNameStore is not defined
RouteNameStore.add ({

I m trying to use the JSonStore on different JavaScript file, but failed.
how to fix this? thanks

1

There are 1 answers

0
Akatum On BEST ANSWER

You define RouteNameStore as local varible inside Ext.onReady handler function.

Because varible scope is inside function, it is not accessible from other functions.

If you want more informations about variable scopes in JavaScript you can look here: What is the scope of variables in JavaScript?

For accessing stores in ExtJS, you can add to your store configuration unique storeId and then get your store in other object by Ext.data.StoreManager

// create store
Ext.create('Ext.data.Store', {
    ...
    storeId: 'myStore'
    ...
});

// get existing store instance
var store = Ext.data.StoreManager.lookup('myStore');