1. #1
    Sencha User
    Join Date
    Jun 2009
    Posts
    84
    Vote Rating
    0
    nofx is on a distinguished road

      0  

    Default global variables

    global variables


    I'm using Designer 2 to create my sencha 2 app. But now i'm in the need of using a global variable(s).
    Normally i would just create another .js file called globals.js in which i put some global variables, like a url to a wsdl and include that into my index.hmtl.

    But i was wondering if Designer 2 offers some kind of abillity to add global variables which can be used throughout my application.

  2. #2
    Sencha - Architect Dev Team aconran's Avatar
    Join Date
    Mar 2007
    Posts
    9,306
    Vote Rating
    125
    aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold

      0  

    Default


    I would recommend against using truly global variables but I would recommend using application specific variables.

    One way you could achieve this would be to go into your Application's launch method and create a global property which can be accessed throughout all of your source.

    For example:
    Code:
    MyApp.globals = {
       key: 123,
       key2: 'abc'
    };
    The touch20/yql.xda example does something similar. You can download the sample projects from this link:
    http://www.sencha.com/forum/showthre...Download-links
    Aaron Conran
    @aconran
    Sencha Architect Development Team

  3. #3
    Sencha User
    Join Date
    Jun 2009
    Posts
    84
    Vote Rating
    0
    nofx is on a distinguished road

      0  

    Default


    Quote Originally Posted by aconran View Post
    I would recommend against using truly global variables but I would recommend using application specific variables.

    One way you could achieve this would be to go into your Application's launch method and create a global property which can be accessed throughout all of your source.

    For example:
    Code:
    MyApp.globals = {
       key: 123,
       key2: 'abc'
    };
    The touch20/yql.xda example does something similar. You can download the sample projects from this link:
    http://www.sencha.com/forum/showthre...Download-links
    Thanks, thats working fine.

    I do have another problem though. I also need a few 'global' functions. I used to just add functions to my controller which i could just call like:

    myApp.controller.Main.backButton();

    But in the Designer tool i can only add functions based on events, like; keyUp, onItemTap etc. I can't even approach my controller anymore through "MyApp.controller.Main.backButton()" (that's how it is Ext.defined).

    But sometimes i just want to call a custom function. Any idea how i'm able to do that in the Designer tool? Couldn't find the answer for this in the examples.

  4. #4
    Sencha Premium Member
    Join Date
    May 2010
    Location
    Guatemala, Central America
    Posts
    1,273
    Vote Rating
    81
    ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough

      0  

    Default


    You can add "Basic Function" to Application, Controllers and containers.
    UI: Sencha Architect 3.x / ExtJS 4 & 5
    Server side: JEE / EJB 3.x / CDI / JPA 2.x/ JAX-RS / JasperReports
    Application Server: Glassfish / WildFly
    Databases: Oracle / DB2 / MySQL / Firebird

    If you like my answer please vote!

  5. #5
    Sencha User
    Join Date
    Jun 2009
    Posts
    84
    Vote Rating
    0
    nofx is on a distinguished road

      0  

    Default


    I just added a basic function, but now i have a new problem. How do i call it?

    I created a controller, gave it an "userAlias" and the same "userClassName". Then i added a Basic Function to the controller with a function name.

    Now i also have a list of DIVs which i load inside a panel. These DIVs have an "onclick" event which i want to use to call my function.

    I tried to call the function like this:

    MyApp.controller.TestController.showTest();

    And even with the component query:
    Ext.ComponentQuery.query('TestController')[0].showTest();

    But both didn't work.. How should this be done from an "onclick" event??

  6. #6
    Sencha - Architect Dev Team aconran's Avatar
    Join Date
    Mar 2007
    Posts
    9,306
    Vote Rating
    125
    aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold

      0  

    Default


    If you are within the same class, the syntax is this.showTest()
    Aaron Conran
    @aconran
    Sencha Architect Development Team

  7. #7
    Sencha User
    Join Date
    Jun 2009
    Posts
    84
    Vote Rating
    0
    nofx is on a distinguished road

      0  

    Default


    Quote Originally Posted by aconran View Post
    If you are within the same class, the syntax is this.showTest()
    No the problem is that i'm not in the same class. I created a "Basic Function" which i want to call.

    I have a Panel on which i have some html DIV elements. I gave those DIV elements an onclick event like this:

    <div onclick="call.my.basic.func()">Test<.div>

    From that onclick event i want to call my "Basic Function" (which is inside a controller ofcourse). But i have no idea how to do that, and i really need this...

    Any idea how to do that??

  8. #8
    Sencha - Architect Dev Team aconran's Avatar
    Join Date
    Mar 2007
    Posts
    9,306
    Vote Rating
    125
    aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold

      0  

    Default


    Bind an event listener to the div you'd like via JavaScript..

    eg
    Code:
    Ext.get('myDiv').on('click', function() {
       basic.showTest();
    });
    Clearly you have to be able to reference both the div and then the instance of whatever you've associated with the basic function.
    Aaron Conran
    @aconran
    Sencha Architect Development Team

  9. #9
    Sencha User
    Join Date
    Jun 2009
    Posts
    84
    Vote Rating
    0
    nofx is on a distinguished road

      0  

    Default


    Sorry i can't get your example to work. I also don't think that it fits my problem.

    In short this is what's happening. I have a DataView which is connected to JSON Store. This JSON store gets dynamic data which the DataView processes. I do that by using the "itemTpl" property.

    In there i have this:

    PHP Code:
    <tpl for=".">
    <
    div class="block">
        <
    div onclick="myController.testFunc({UserGuid}, {Param1}, {Param2});">{Username}</div>
    </
    div>
    </
    tpl
    So as you can see i need to call my controller function from my DIV with certain parameters. The best way to do this is to directly call the function of a Controller. Is there any way to do that...??

    On your example i have no idea where to define the 'event listener'. How to make it work when the amount of DIVs are dynamic. How to pass the needed params from the event listener to the Controller.
    And in 'basic.showTest()', i have no idea where 'basic' comes from. I tried to replace that with the name of my controller, but that's "undefined".

    Anyway, i'd rather call my Controller function directly. I really hope that this is possible..

  10. #10
    Sencha - Architect Dev Team aconran's Avatar
    Join Date
    Mar 2007
    Posts
    9,306
    Vote Rating
    125
    aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold

      0  

    Default


    The pseudo code which you have posted above will have severe memory leaks.

    You do not want to attach event handlers via DOM level 0 event handling "onclick" attributes. Instead you want to subscribe to the DataView's click event which uses dom event delegation. It will determine which record you clicked on and you can read the guid and additional parameters out of it.
    Aaron Conran
    @aconran
    Sencha Architect Development Team