1. #1
    Ext JS Premium Member
    Join Date
    Dec 2010
    Location
    Hamburg, Germany
    Posts
    196
    Vote Rating
    7
    winkelmann will become famous soon enough

      0  

    Default Static mixins?

    Static mixins?


    Hi there,

    I'm trying to get a generic static logger method into my objects that will simply put [MyClassName] in front of the log messages, What I am doing is

    Code:
    Ext.define('MyApp.log.ClassLoggerSupport', {
        statics{
            log: function(){
                console.log('[' + this.getName() + '] ' + msg, arguments[1]||'info');
            }
    }
    });
    
    Ext.define('MyApp.myClass', {
        mixins: {
            logger: 'MyApp.log.ClassLoggerSupport'
        }
        
        statics: {
            myfunc: function(){
            }
        }
    });
    However, there doesn't seem to be any log function anywhere in my object. At least I can't find it anywhere :-( I'm guessing statics cannot be mixed in?

    cheers
    Jan

  2. #2
    Ext JS Premium Member westy's Avatar
    Join Date
    Feb 2009
    Location
    Bath, UK
    Posts
    966
    Vote Rating
    72
    westy is just really nice westy is just really nice westy is just really nice westy is just really nice westy is just really nice

      0  

    Default


    If it's static then it doesn't have to be a mixin.
    Just call MyApp.log.ClassLoggerSupport.log from your function.

  3. #3
    Ext JS Premium Member
    Join Date
    Dec 2010
    Location
    Hamburg, Germany
    Posts
    196
    Vote Rating
    7
    winkelmann will become famous soon enough

      0  

    Default


    I could do that, but that would result in log messages always containing "[ClassLoggerSupport]" as prefix, instead of the class name that is actually logging.

  4. #4
    Ext JS Premium Member westy's Avatar
    Join Date
    Feb 2009
    Location
    Bath, UK
    Posts
    966
    Vote Rating
    72
    westy is just really nice westy is just really nice westy is just really nice westy is just really nice westy is just really nice

      0  

    Default


    I use:

    Code:
            logEntryPoint: function(name) {
                var caller = this.logEntryPoint.caller;
                this.log.debug(Ext.isEmpty(caller.displayName) ? name : caller.displayName,
                               caller.arguments);
            }
    Sometimes class name is not set, so I still pass it.
    Oh, and in a static method 'this' refers to the class.

    HTH

  5. #5
    Ext JS Premium Member
    Join Date
    Dec 2010
    Location
    Hamburg, Germany
    Posts
    196
    Vote Rating
    7
    winkelmann will become famous soon enough

      0  

    Default


    We do have a centralized logging class as well, but imho It would simply be great to be able to call this.log(). In PHP we have logging facilities integrated into our base class (statically) to allos self::log(). Is there any way I could achieve that kind of handling?

    I also thought about faking the whole thing as an internal singleton like so:
    Code:
    ...
    myFunc: function(){
    },
    _instance: null,
    instance: function(){
    return (this.instance||this.instance=new this());
    },
    
    statics: {
      myFunc: function(){
    this.instance.myFunc();
      }
    }
    which would then enable me to use ClassLoggerSupport as mixin. I don't find it very beautiful that way though :-(

  6. #6
    Ext JS Premium Member
    Join Date
    Dec 2010
    Location
    Hamburg, Germany
    Posts
    196
    Vote Rating
    7
    winkelmann will become famous soon enough

      0  

    Default


    I finally found out that i can actually force my object to be a singleton by defining {singleton: true}. This is quite awesome, now I can have ClassLoggerSupportas mixin, using this.self.getName() to get the current class name and I don't even have to worry about the static stuff, thats pretty awesome :-)

  7. #7
    Ext JS Premium Member westy's Avatar
    Join Date
    Feb 2009
    Location
    Bath, UK
    Posts
    966
    Vote Rating
    72
    westy is just really nice westy is just really nice westy is just really nice westy is just really nice westy is just really nice

      0  

    Default


    Yeah, nice idea having a singleton mixin, sure there are many applications for that trick.
    Thanks for sharing

Similar Threads

  1. Replies: 2
    Last Post: 17 Apr 2011, 6:22 PM
  2. [OPEN-EXTJSIV-207] Mixins issues
    By LesJ in forum Ext:Bugs
    Replies: 3
    Last Post: 21 Mar 2011, 12:58 PM
  3. [INFOREQ]allow singleton as mixins
    By christophe.geiser in forum Ext:Bugs
    Replies: 4
    Last Post: 14 Mar 2011, 6:36 AM
  4. [CLOSED]Ext.AbstractComponent mixins
    By gevik in forum Ext:Bugs
    Replies: 5
    Last Post: 28 Feb 2011, 9:56 PM

Thread Participants: 1