methusalem
methusalem

Reputation: 95

Sencha scope issue

I've completely rewritten my question to hopefully better reflect what I am trying to do here. Thank you guys so much for your help so far.

I have a file called en.js, which holds this code:

Ext.apply(Ext.locale || {}, {
variable:       'great success!'    
});

Here's my index.js setup code:

Ext.setup({
tabletStartupScreen: 'tablet_startup.png',
phoneStartupScreen: 'phone_startup.png',
icon: 'icon.png',
glossOnIcon: false,
onReady: function() {
    Ext.locale = {};
    var headID = document.getElementsByTagName("head")[0],
    newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = 'en.js';
    headID.appendChild(newScript);
    loginPanel = new login.Panel();
}
});

login.Panel is an extension of the Sencha panel class using Ext.extend.

The 'en.js' script is added to the header correctly. I don't have it in the index.html file because once this problem is solved there will be several files that could be loaded, depending on the output of a function. That's why I need to add the script to the header in the onReady function, and not in the index.html file itself.

Once the script has been added it loads "variable: 'great success'" into Ext.locale, Yet my problem currently lies within login.Panel(), which is an extension of the Sencha panel class using Ext.extend. Currently, there is a button in the panel.

When I put this in the button's handler:

console.log(Ext.locale.variable)

it returns the string "great success",

yet when I try to set the button's text like this:

text:Ext.locale.variable,

I get the error

Uncaught TypeError: Cannot read property 'variable' of undefined

I'm guessing I have a scope issue here, since console.log() and alert() can both access Ext.locale, but trying to use it to construct the form gives me the undefined error.

Any help would be greatly appreciated.

Thank you!

Upvotes: 1

Views: 959

Answers (2)

Chris Farmiloe
Chris Farmiloe

Reputation: 14175

it sounds like you are defining Ext.locale from your script early on... then later in onReady you are overwriting it as Ext.locale = {}

onReady will run after all your other scripts have been loaded.

Why not move your initialisation code for locale into onReady insted of your = {} line

Upvotes: 2

Old McStopher
Old McStopher

Reputation: 6349

This will add three properties and their values to the receiving object.

Ext.apply(receivingObject, {
        property1: 'value1',
        property2: 'value2',
        property3: 'value3'
});

Here also is the Sencha documentation on the Ext.apply method: http://dev.sencha.com/deploy/touch/docs/source/Ext.html#method-Ext-apply

As for accessing the isReady property, you could do something like if(someExtObj.isReady), but you may be more interested in using the onReady method...

Ext.setup({
    onReady: function() {
        // your setup code
    }
});

Upvotes: 0

Related Questions