dnalot
20 Feb 2008, 10:03 PM
Have been rolling out a lot of production js lately and these have been very helpful. Whether just writing application code to a given namespace or deriving a class with a mixin, these have both reduced copy/paste and made it easier to remember what my code is doing by readability.
The applyMixin method automatically gets your namespace, passes it to a function that is expected to return your application object, and defers to your callback with namespace scope. NB: The module parameter your mixin function receives is only useful within callback functions. Additionally, module and mixin arguments will accept type Object.
/**
* intended to wrap module files
* @param {String/Object} module value automatically namespaced
* @param {Function/Object} mixin passed single argument of module expected to return Object
* @param {Function} callback (optional) called after mixin has been applied to module
* @param {Object} scope (optional) of the callback defaults to module
* @return {Object} module
*//*
* <code>
* Ext.ux.applyMixin('Ext.ux.application', function( $module ) { return {
* //application methods and properties
* init : function() {}
* }; }, function() { Ext.onReady(this.init); });
* </code>
*/
Ext.ux.applyMixin = function( module, mixin, callback, scope ) {/*...*/};
extendMixin mirrors applyMixin and additionally passes your superclass prototype as a parameter to your mixin function and automatically registers an xtype if your last argument provided is a String. If your mixin provides a Constructor property, that method will be wrapped by your Class constructor. Check out my source (http://www.docudesk.com/scripts/domcmp.js) from another extension for an extended example of this method.
/**
* intended to wrap class files
* @param {String/Function} subclass value automatically namespaced
* @param {Function} superclass
* @param {Function/Object} mixin passed subclass and superclass.prototype expected to return Object
* @param {Function} callback (optional) called after mixin has been applied to module
* @param {Object} scope (optional) of the callback defaults to subclass
* @param {String} xtype (optional) final String argument registers xtype
* @return {Function} subclass
*//*
* <code>
* Ext.ux.extendMixin('Ext.ux.NewClass', Ext.Superclass, function( $super, $class ) { return {
* //static methods and properties
* Constructor : function() {
* //...
* $super.constructor.apply(this, arguments);
* //...
* }; }, 'xtype');
* </code>
*/
Ext.ux.extendMixin = function( subclass, superclass, mixin, callback, scope ) {/*...*/};
Updates
-- 2008-02-24 --
[added] - Support for classes without namespaces by simplifying underlying initialization
-- 2008-02-26 --
[changed] - Reversed arguments passed to mixin by extendMixin
[added] - Support for reading Constructor from mixin by extendMixin
-- 2008-03-05 --
[added] - Automatic ctype property identifies fully qualified class name useful for firebugging
The applyMixin method automatically gets your namespace, passes it to a function that is expected to return your application object, and defers to your callback with namespace scope. NB: The module parameter your mixin function receives is only useful within callback functions. Additionally, module and mixin arguments will accept type Object.
/**
* intended to wrap module files
* @param {String/Object} module value automatically namespaced
* @param {Function/Object} mixin passed single argument of module expected to return Object
* @param {Function} callback (optional) called after mixin has been applied to module
* @param {Object} scope (optional) of the callback defaults to module
* @return {Object} module
*//*
* <code>
* Ext.ux.applyMixin('Ext.ux.application', function( $module ) { return {
* //application methods and properties
* init : function() {}
* }; }, function() { Ext.onReady(this.init); });
* </code>
*/
Ext.ux.applyMixin = function( module, mixin, callback, scope ) {/*...*/};
extendMixin mirrors applyMixin and additionally passes your superclass prototype as a parameter to your mixin function and automatically registers an xtype if your last argument provided is a String. If your mixin provides a Constructor property, that method will be wrapped by your Class constructor. Check out my source (http://www.docudesk.com/scripts/domcmp.js) from another extension for an extended example of this method.
/**
* intended to wrap class files
* @param {String/Function} subclass value automatically namespaced
* @param {Function} superclass
* @param {Function/Object} mixin passed subclass and superclass.prototype expected to return Object
* @param {Function} callback (optional) called after mixin has been applied to module
* @param {Object} scope (optional) of the callback defaults to subclass
* @param {String} xtype (optional) final String argument registers xtype
* @return {Function} subclass
*//*
* <code>
* Ext.ux.extendMixin('Ext.ux.NewClass', Ext.Superclass, function( $super, $class ) { return {
* //static methods and properties
* Constructor : function() {
* //...
* $super.constructor.apply(this, arguments);
* //...
* }; }, 'xtype');
* </code>
*/
Ext.ux.extendMixin = function( subclass, superclass, mixin, callback, scope ) {/*...*/};
Updates
-- 2008-02-24 --
[added] - Support for classes without namespaces by simplifying underlying initialization
-- 2008-02-26 --
[changed] - Reversed arguments passed to mixin by extendMixin
[added] - Support for reading Constructor from mixin by extendMixin
-- 2008-03-05 --
[added] - Automatic ctype property identifies fully qualified class name useful for firebugging