Skip to content

Latest commit

 

History

History
237 lines (203 loc) · 6.58 KB

2.clazz_declaration.md

File metadata and controls

237 lines (203 loc) · 6.58 KB

Clazz declaration

< Installation | Main | Properties >

Full form of clazz declaration looks like:

clazz('ClazzName' [, parentClazz], function(self /*, dependency1 , dependency2 ... */) {
    return {
        // Clazz constants
        constants: {
            CONSTANT_1: 'SomeValue',
            CONSTANT_2: {
                SUBCONSTANT_1: 'AnotherValue1',
                SUBCONSTANT_2: 'AnotherValue2'
            }
        }
        // Clazz properties 
        clazz_properties: {
            // see 'properties' (it's like static properties in Java)
        },
        // Clazz methods
        clazz_methods: {
            // see 'methods' (it's like static methods in Java)
        },
        // Clazz events handlers
        clazz_events: {
            // see 'events' (handlers for clazz events)
        }
        // Instance properties
        properties: {
            property1: {
            
                // Property type
                type: ['type', typeParams],
                
                // Default value of this property. This value will 
                // be setted in case no value is specified for this property
                default: 'defaultValue',
                
                // Converters of property value.
                converters: {
                    // Each convertaion has name and convertation logic (function)
                    converter1: function(value) {
                        
                    },
                    converter2: function(value) {
                    },
                    ...
                }
                
                // Constraints of property value. 
                // If value doesn't satisfy to constraint than error will be thrown
                constraints: {
                    // Each constraint has name and constraint logic (function) 
                    constraint1: function(value) {
                        return /* some value checking; true if value is correct, false otherwise */
                    },
                    constraint2: function(value) {
                        ...
                    },
                    ...
                },
 
                // Specified methods will be automaticly generated for this property
                methods: ['get', 'set', 'has', 'is', 'clear', 'remove']
            }
        },
        // Instance methods
        methods: {
            method1: function(param1, param2 /* ... */) {
                // Method logic 
            },
            method2: function(param1, param2 /* ... */) {
                // Method logic
            },
            ...
        },
        // Instance events handlers
        events: {
            "property.changed": function(property, newValue, oldValue) {
            },
            "property.propName.changed": function(newValue, oldValue) {
                
            },
            "property.propName2.cleared": function(oldValue) {
            },
            ...
        },
        // Your first directive
        your_directive1: /* some directive value */
        
        // Your second directive
        your_directive2: /* some directive value */
        
        ...
    }
});

Let's dig into it deeper.

Signature

Sources: core/Clazz/Clazz.js, core/Clazz/Factory.js

'clazz' function takes 3 arguments to declare new clazz:

  • name - clazz name. Type: string. Required.

It can be namespaced clazz name.

Examples:

clazz('Person', ...)
clazz('/Name/Spaced/ClazzName', ...)
clazz('/Animals/Cat', ...)
  • parent - parent clazz. Type: string, constructor function. Optional.

Examples:

clazz('Cat', 'Animal', ...), 
clazz('/Mans/BadMan', '/Persons/Person', ...), 
clazz('Dog', clazz('Animal'), ...)
  • meta | factory - meta directives of factory function. Types: function, object. Required.

It can be object containing meta directives or factory function. First argument of the factory function is new clazz itself followed by clazz dependencies. Factory function can return object containing meta directives. In that case these meta directives will be used for clazz creation.

Examples:

clazz('name', 'parent', {
    clazz_methods: {
        clazzMethod1: function() {
            /* ... */
        },
        clazzMethod2: function() {
            /* ... */
        }
    },
    properties: {
        property1: {
            type: 'string',
            default: 'defaultValue1'
        },
        property2: {
            type: 'number',
            default: 10
        }
    },
    methods: {
        method1: function() {
            helper.helperMethod();
        },
        method2: function() {
            anotherDependency.someMethodOfDependency();
        }
    }
})

clazz('name', function(self, hellper, anotherDependency) {

    self.__construct(data) {
        this.property1 = data.property1;
        this.property2 = data.property2;
    }
    
    // Clazz methods
    self.clazzMethod1 = function() {
        /* ... */
    };
    
    self..clazzMethod2 = function() {
        /* ... */
    };
    
    // Instance methods
    self.prototype.method1 = function() {
        helper.helperMethod();
    };
    
    self.prototype.method2 = function() {
        anotherDependency.someMethodOfDependency();
    };
});

clazz('name', 'parent', function(self, hellper, anotherDependency) {
    return {
        clazz_methods: {
            clazzMethod1: function() {
                /* ... */
            },
            clazzMethod2: function() {
                /* ... */
            }
        },
        properties: {
            property1: {
                type: 'string',
                default: 'defaultValue1'
            },
            property2: {
                type: 'number',
                default: 10
            }
        },
        methods: {
            method1: function() {
                helper.helperMethod();
            },
            method2: function() {
                anotherDependency.someMethodOfDependency();
            }
        }
    };
});

< Installation | Main | Properties >

githalytics.com alpha