Results 1 to 9 of 9

Thread: Call a class as function to keep files management

  1. #1
    Sencha Premium Member
    Join Date
    Nov 2011
    Location
    Compiegne FRANCE
    Posts
    48

    Default Call a class as function to keep files management

    Hello all,

    I would like to create a class with a direct function for the migration of code from Ext 3 to Ext 5:

    Currently the helpers of the applications are in several files in the same directory and declared like this:

    Code:
    Ext.namespace('MyApp');
    
    (function(){ // start of local scope
    
        MyApp.helpers = MyApp.helpers || {}; // sub name space
    
        // constructor
        MyApp.helpers.foo = function( a,b ) 
        {
            return a + b;
        };
    
    })(); // end of local scop
    To call the helper, I do:

    Code:
    ...
            var bar = MyApp.helpers.foo(1, 2);
    ...
    I would like to keep the files and directories like today and not change the syntax to call my plugins.
    I hoped to do this like this:

    Code:
    Ext.define('MyApp.helpers.foo',
    {
        singleton    :    true 
        ,direct        :    function (a,b)
        {
            return a + b;
        }
    });
    But If I call the helper, I get (logically) an error because it's not a function.

    Do you know a trick to create the class and call it as function with it's own name ?
    At least, I know that I can change the call by MyApp.helpers.foo.direct(a,b), but it's not really the idea.

    Thank you for your help.

  2. #2
    Sencha Premium User jvandemerwe's Avatar
    Join Date
    Apr 2009
    Location
    Raalte, Netherlands
    Posts
    249
    Answers
    7

    Default

    Code:
    Ext.define('MyApp.helpers',
    {
        singleton    :    true 
        ,foo        :    function (a,b)
        {
            return a + b;
        }
    });

  3. #3
    Sencha Premium Member
    Join Date
    Nov 2011
    Location
    Compiegne FRANCE
    Posts
    48

    Default

    Hello jvandemerwe,
    Thank you for your answer.

    You really shure I can define for all my plugins with the same "identifier" ?

    Code:
    Ext.define('MyApp.helpers',
    {
        singleton    :    true 
        ,foo        :    function (a,b)
        {
            return a + b;
        }
    });
    
    Ext.define('MyApp.helpers',
    {
        singleton    :    true 
        ,bar        :    function (a,b)
        {
            return a - b;
        }
    });
    
    Ext.define('MyApp.helpers',
    {
        singleton    :    true 
        ,boo        :    function (a,b)
        {
            return a * b;
        }
    });
    
    Ext.define('MyApp.helpers',
    {
        singleton    :    true 
        ,far        :    function (a,b)
        {
            return a / b;
        }
    });
    I will try, but I don't know that it was possible.

  4. #4
    Sencha Premium User jvandemerwe's Avatar
    Join Date
    Apr 2009
    Location
    Raalte, Netherlands
    Posts
    249
    Answers
    7

    Default

    NO! Not like that, but like this:

    Code:
    Ext.define('MyApp.helpers',
    {
        singleton    :    true 
        , foo        :    function (a,b)
        {
            return a + b;
        }
        , bar        :    function (a,b)
        {
            return a - b;
        }
        , boo        :    function (a,b)
        {
            return a * b;
        }
        , far        :    function (a,b)
        {
            return a / b;
        }
    });

    Quote Originally Posted by leduc View Post
    Hello jvandemerwe,
    Thank you for your answer.

    You really shure I can define for all my plugins with the same "identifier" ?

  5. #5
    Sencha Premium Member
    Join Date
    Nov 2011
    Location
    Compiegne FRANCE
    Posts
    48

    Default

    Hello,

    Ok, but in fact this will not resolve my problem, because I want to keep a different file (like for class) foreach helper as it's done today.

    Thank you for your help.

  6. #6
    Sencha Premium Member
    Join Date
    Nov 2011
    Location
    Compiegne FRANCE
    Posts
    48

    Default

    Hello, Somebody have another idea ? Does that mean that there is no solution to my problem? Thank you for your help.

  7. #7
    Sencha Premium User jvandemerwe's Avatar
    Join Date
    Apr 2009
    Location
    Raalte, Netherlands
    Posts
    249
    Answers
    7

    Default

    Quote Originally Posted by leduc View Post
    Hello, Somebody have another idea ? Does that mean that there is no solution to my problem? Thank you for your help.
    Sometimes the offered idea is the best idea. How many helpers are we talking about? > 10 or > 100?

  8. #8
    Sencha Premium User jvandemerwe's Avatar
    Join Date
    Apr 2009
    Location
    Raalte, Netherlands
    Posts
    249
    Answers
    7

    Default

    What you call 'identifier' is in fact the namespace. That it the folder path where Ext JS is looking for when it searches the source for the class. And that namespace has to be unique. So you can't have more than one class definition with the same namespace (identifier). To make it happen as you have in mind it should be at least be something like: 'MyApp' > folder: 'helpers' > files: 'foo.js', 'bar.js' etc...

    In foo.js you do an:
    PHP Code:
     Ext.define('MyApp.helper.foo', {
        
    singletontrue,
        
    alternateClassName'FooHelper',

        
    exec: function(ab) {
            return 
    b;
        }
    }); 
    Then you would call: MyApp.helpers.foo.exec(1, 2);

    or:

    FooHelper.exec(1,2);

    but: then the alternateClassName has to be unique throughout your app.

    Quote Originally Posted by leduc View Post
    Hello jvandemerwe,
    Thank you for your answer.

    You really shure I can define for all my plugins with the same "identifier" ?

    Code:
    Ext.define('MyApp.helpers',
    {
        singleton    :    true 
        ,foo        :    function (a,b)
        {
            return a + b;
        }
    });
    
    Ext.define('MyApp.helpers',
    {
        singleton    :    true 
        ,bar        :    function (a,b)
        {
            return a - b;
        }
    });
    
    Ext.define('MyApp.helpers',
    {
        singleton    :    true 
        ,boo        :    function (a,b)
        {
            return a * b;
        }
    });
    
    Ext.define('MyApp.helpers',
    {
        singleton    :    true 
        ,far        :    function (a,b)
        {
            return a / b;
        }
    });
    I will try, but I don't know that it was possible.

  9. #9
    Sencha Premium Member
    Join Date
    Nov 2011
    Location
    Compiegne FRANCE
    Posts
    48

    Default

    Hello jvandemerwe,

    Thank you again for your support and your time.
    I order to others people to not continue to disturb you with this problem.

    In fact, to summarize what I want:
    - Keep folder structure and files as today
    - Keep the calling methode use every where in the code.
    - (Well understand that I write. )

    With your last proposition I need to change the call to add the 'exec' command, and this correspond to my first post at:
    Quote Originally Posted by leduc;
    "At least, I know that I can change the call by MyApp.helpers.foo.direct(a,b), but it's not really the idea".
    I have post this problem because I'm not very aware with the namespaces and class declaration, and so I want to be sure that there is no clean solution.
    And this post, so yours anwsers, permit me to better know the class management: mission accomplished.

    Unfortunately, now I think there is no way to get that I want. I did hope that there is a "direct" methode automatically call or a trick into the declation, but it seems not.

    Thank you very much for your support.
    Read you.

Similar Threads

  1. Call function on class
    By blackangelnt in forum Sencha Touch 2.x: Q&A
    Replies: 2
    Last Post: 13 Nov 2012, 8:33 AM
  2. Replies: 3
    Last Post: 26 May 2012, 3:15 AM
  3. HOW to Call function from a different class
    By sachin sachdeva in forum Ext 3.x: Help & Discussion
    Replies: 2
    Last Post: 17 Oct 2010, 5:27 AM
  4. How to call a function in the class from a link?
    By safewolf in forum Ext 3.x: Help & Discussion
    Replies: 2
    Last Post: 5 Oct 2009, 3:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •