Created
May 19, 2009 16:42
-
-
Save subtleGradient/114213 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // MooTools Method Wrappers | |
| var MyClass = new Class({ | |
| Implements: Bind, | |
| MethodMutators:true, | |
| initilize: function(){ | |
| this.setBinds(); | |
| }, | |
| classicMethod: function(){ | |
| // works the same way as a method in 1.2 | |
| }, | |
| 'bind normalMethod': function(){ | |
| // `this` is always the instance | |
| }, | |
| 'public normalMethod1': function(){ | |
| // `this` is always the instance | |
| }, | |
| 'public stringMethod:String:default("Falsy Value")': function(arg){ | |
| return arg; // will convert the result into a String, always | |
| }, | |
| 'handler eventHandlerMethod': function(event){ | |
| // event is always passed | |
| event.preventDefault(); | |
| // `this` is always the instance | |
| }, | |
| 'evented eventedMethod': function(arg){ | |
| // always fires events before and after, before events can prevent this method from being called | |
| alert('eventedMethod was called!'); | |
| }, | |
| 'setter set': function(key,value){ | |
| this.data[key] = value; | |
| // no need to return this, the wrapper does that by default | |
| }, | |
| 'getter get': function(key){ | |
| return this.data[key]; | |
| }, | |
| 'evented setter setEvented': function(key,value){ | |
| this.data[key] = value; | |
| // no need to return this, the wrapper does that by default | |
| }, | |
| 'cache(100) cachedMethod': function(){ | |
| return this.extremelyExpensiveOperation(); | |
| }, | |
| 'private privateMethod': function(){ | |
| alert('privateMethod'); | |
| }, | |
| 'public callPrivateMethod': function(arg){ | |
| return this.privateMethod(arg); | |
| }, | |
| }); | |
| var o = new MyClass(); | |
| o.privateMethod(); // cannot be called directly | |
| o.callPrivateMethod(); // can be called from another public method | |
| o.classicMethod(); | |
| o.normalMethod(); | |
| o.normalMethod1(); | |
| // Always passes an event | |
| o.eventHandlerMethod(); | |
| $('thing').addEvent('click', o.eventHandlerMethod); | |
| setTimeout(o.eventHandlerMethod, 10); | |
| o.addEvent('eventedMethod:before', function(event, arg){ if (!arg) event.preventDefault(); }); | |
| o.addEvent('eventedMethod:after', function(event, arg){ alert('eventedMethod:after'); }); | |
| o.eventedMethod(true); // alerts & fires after event | |
| o.eventedMethod(false); // no alert & doesn't fire after event | |
| o.set('key1','value'); | |
| o.set({ key1:'value', key2:'value' }); | |
| o.get('key1'); | |
| o.get(['key1','key2']); // ? | |
| o.addEvent('setEvented:before', function(event, arg){ if (!arg) event.preventDefault(); }); | |
| o.addEvent('setEvented:after', function(event, arg){ alert('setEvented:after'); }); | |
| o.setEvented('key1','value'); | |
| o.setEvented({ key1:'value', key2:'value' }); | |
| o.addEvent('setEvented:before:match(foo)', function(event,key,value) { | |
| // if the first argument matches "foo" then this event will fire | |
| alert('setEvented:before:match(foo)'); | |
| }); | |
| o.setEvented('foo','bar'); // alerts | |
| o.cachedMethod(); // The first time it's called it will actually call the method and cache and return the result | |
| o.cachedMethod(); // The next time it's called it will instantly return the cached result | |
| setTimeout(o.cachedMethod, 1000); // It will keep returning the cached result until the timeout clears it | |
| o.stringMethod('abc'); // returns 'abc' | |
| o.stringMethod(123); // returns '123' | |
| o.stringMethod(function(){}); // returns '' ? | |
| o.stringMethod(false); // returns 'Falsy Value' | |
| o.stringMethod(); // returns 'Falsy Value' | |
| o.stringMethod(undefined); // returns 'Falsy Value' | |
| o.stringMethod(NaN); // returns 'Falsy Value' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // MooTools Method Wrappers | |
| var MyClass = new Class({ | |
| classicMethod: function(){ | |
| // works the same way as a method in 1.2 | |
| }, | |
| normalMethod: function(){ | |
| // `this` is always the instance | |
| }.bind(), | |
| 'public normalMethod1': function(){ | |
| // `this` is always the instance | |
| }, | |
| 'public String:stringMethod': function(arg){ | |
| return arg; // will convert the result into a String, always | |
| }.bindDefault('Falsy Value'), | |
| eventHandlerMethod: function(event){ | |
| // event is always passed | |
| event.preventDefault(); | |
| // `this` is always the instance | |
| }.bindAsEventHandlerMethod(), | |
| eventedMethod: function(arg){ | |
| // always fires events before and after, before events can prevent this method from being called | |
| alert('eventedMethod was called!'); | |
| }.bindAsEvented(), | |
| set: function(key,value){ | |
| this.data[key] = value; | |
| // no need to return this, the wrapper does that by default | |
| }.bindAsSetter(), | |
| get: function(key){ | |
| return this.data[key]; | |
| }.bindAsGetter(), | |
| setEvented: function(key,value){ | |
| this.data[key] = value; | |
| // no need to return this, the wrapper does that by default | |
| }.bindAsSetter().bindAsEvented(), | |
| cachedMethod: function(){ | |
| return this.extremelyExpensiveOperation(); | |
| }.bindCached(100), | |
| privateMethod: function(){ | |
| alert('privateMethod'); | |
| }.protect(), | |
| 'private privateMethod1': function(){ | |
| alert('privateMethod'); | |
| }, | |
| 'public callPrivateMethod': function(arg){ | |
| return this.privateMethod(arg); | |
| }, | |
| }); | |
| var o = new MyClass(); | |
| o.privateMethod(); // cannot be called directly | |
| o.privateMethod1(); // cannot be called directly | |
| o.callPrivateMethod(); // can be called from another public method | |
| o.classicMethod(); | |
| o.normalMethod(); | |
| o.normalMethod1(); | |
| // Always passes an event | |
| o.eventHandlerMethod(); | |
| $('thing').addEvent('click', o.eventHandlerMethod); | |
| setTimeout(o.eventHandlerMethod, 10); | |
| o.addEvent('eventedMethod:before', function(event, arg){ if (!arg) event.preventDefault(); }); | |
| o.addEvent('eventedMethod:after', function(event, arg){ alert('eventedMethod:after'); }); | |
| o.eventedMethod(true); // alerts & fires after event | |
| o.eventedMethod(false); // no alert & doesn't fire after event | |
| o.set('key1','value'); | |
| o.set({ key1:'value', key2:'value' }); | |
| o.get('key1'); | |
| o.get(['key1','key2']); // ? | |
| o.addEvent('setEvented:before', function(event, arg){ if (!arg) event.preventDefault(); }); | |
| o.addEvent('setEvented:after', function(event, arg){ alert('setEvented:after'); }); | |
| o.setEvented('key1','value'); | |
| o.setEvented({ key1:'value', key2:'value' }); | |
| o.addEvent('setEvented:before:match(foo)', function(event,key,value) { | |
| // if the first argument matches "foo" then this event will fire | |
| alert('setEvented:before:match(foo)'); | |
| }); | |
| o.setEvented('foo','bar'); // alerts | |
| o.cachedMethod(); // The first time it's called it will actually call the method and cache and return the result | |
| o.cachedMethod(); // The next time it's called it will instantly return the cached result | |
| setTimeout(o.cachedMethod, 1000); // It will keep returning the cached result until the timeout clears it | |
| o.stringMethod('abc'); // returns 'abc' | |
| o.stringMethod(123); // returns '123' | |
| o.stringMethod(function(){}); // returns '' ? | |
| o.stringMethod(false); // returns 'Falsy Value' | |
| o.stringMethod(); // returns 'Falsy Value' | |
| o.stringMethod(undefined); // returns 'Falsy Value' | |
| o.stringMethod(NaN); // returns 'Falsy Value' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // MooTools Method Wrappers | |
| var MyClass = new Class({ | |
| classicMethod: function(){ | |
| // works the same way as a method in 1.2 | |
| }, | |
| normalMethod: new Method(function(){ | |
| // `this` is always the instance | |
| }), | |
| 'public normalMethod1': function(){ | |
| // `this` is always the instance | |
| }, | |
| 'public String:stringMethod': new Method.String(function(arg){ | |
| return arg; // will convert the result into a String, always | |
| }), | |
| eventHandlerMethod: new Method.EventHandler(function(event){ | |
| // event is always passed | |
| event.preventDefault(); | |
| // `this` is always the instance | |
| }), | |
| eventedMethod: new Method.Evented(function(arg){ | |
| // always fires events before and after, before events can prevent this method from being called | |
| alert('eventedMethod was called!'); | |
| }), | |
| set: new Method.Setter(function(key,value){ | |
| this.data[key] = value; | |
| // no need to return this, the wrapper does that by default | |
| }), | |
| get: new Method.Getter(function(key){ | |
| return this.data[key]; | |
| }), | |
| setEvented: new Method.Evented.Setter(function(key,value){ | |
| this.data[key] = value; | |
| // no need to return this, the wrapper does that by default | |
| }), | |
| cachedMethod: new Method.Cached(100, function(){ | |
| return this.extremelyExpensiveOperation(); | |
| }), | |
| privateMethod: new Method.Private(function(){ | |
| alert('privateMethod'); | |
| }), | |
| 'private privateMethod1': new Method.Private(function(){ | |
| alert('privateMethod'); | |
| }, | |
| 'public callPrivateMethod': new Method(function(arg){ | |
| return this.privateMethod(arg); | |
| }, | |
| }); | |
| var o = new MyClass(); | |
| o.privateMethod(); // cannot be called directly | |
| o.privateMethod1(); // cannot be called directly | |
| o.callPrivateMethod(); // can be called from another public method | |
| o.classicMethod(); | |
| o.normalMethod(); | |
| o.normalMethod1(); | |
| // Always passes an event | |
| o.eventHandlerMethod(); | |
| $('thing').addEvent('click', o.eventHandlerMethod); | |
| setTimeout(o.eventHandlerMethod, 10); | |
| o.addEvent('eventedMethod:before', function(event, arg){ if (!arg) event.preventDefault(); }); | |
| o.addEvent('eventedMethod:after', function(event, arg){ alert('eventedMethod:after'); }); | |
| o.eventedMethod(true); // alerts & fires after event | |
| o.eventedMethod(false); // no alert & doesn't fire after event | |
| o.set('key1','value'); | |
| o.set({ key1:'value', key2:'value' }); | |
| o.get('key1'); | |
| o.get(['key1','key2']); // ? | |
| o.addEvent('setEvented:before', function(event, arg){ if (!arg) event.preventDefault(); }); | |
| o.addEvent('setEvented:after', function(event, arg){ alert('setEvented:after'); }); | |
| o.setEvented('key1','value'); | |
| o.setEvented({ key1:'value', key2:'value' }); | |
| o.addEvent('setEvented:before:match(foo)', function(event,key,value) { | |
| // if the first argument matches "foo" then this event will fire | |
| alert('setEvented:before:match(foo)'); | |
| }); | |
| o.setEvented('foo','bar'); // alerts | |
| o.cachedMethod(); // The first time it's called it will actually call the method and cache and return the result | |
| o.cachedMethod(); // The next time it's called it will instantly return the cached result | |
| setTimeout(o.cachedMethod, 1000); // It will keep returning the cached result until the timeout clears it | |
| o.stringMethod('abc'); // returns 'abc' | |
| o.stringMethod(123); // returns '123' | |
| o.stringMethod(function(){}); // returns '' ? | |
| o.stringMethod(false); // returns 'Falsy Value' | |
| o.stringMethod(); // returns 'Falsy Value' | |
| o.stringMethod(undefined); // returns 'Falsy Value' | |
| o.stringMethod(NaN); // returns 'Falsy Value' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment