Function.decorate 1.1.1

Adds decorator functionality and some basic decorators



Details

Author
Maksim Horbachevsky
Current version
1.1.1
GitHub
fantactuka/mootools-decorator
Downloads
10492
Category
Native
Tags
Report
GitHub Issues

Releases


Dependencies

  • _self_/_current_:
    • /Native
    • /$util

How to use

Function.decorate

Function method that adds decorator pattern.

var method = function() {
    ...
}.decorate(decorator(arg1, arg2, ...));

OR

var method = function() {
    ...
}.decorate('decoratorName', arg1, arg2, ...);

decorator function that passed into has two arguments: method to be decorated and its arguments, so you can controll arguments, returned values, decide when the default method should be called or not called at all, etc. Also it could be a name of the decorator from the collection


Function Decorators Add

Adds decoraton into collection that it can be applied by name

var outputFormat = function(limit) {
    return function(method, args) {
        return method.apply(this, args).substr(0, limit);
    }
}

Function.Decorators.add('outputFormat', outputFormatter);

And later:

var getOuput = function() {
    ...
    return someString;
    }.decorate('outputFormat', 30); // Limit output string to 30 chars

OR it's still possible to use it dirrectly from collection

var getOuput = function() {
    ...
    return someString;
}.decorate(Function.Decorators.Collection.outputFormatter(30));

Function Decorators Collection

strictArguments

Decorate function with parameters validation.

function(numParam, strParam, boolParam) {
    ...
}.decorate('strictArguments', 'number', 'string', 'boolean')

Will require exact 3 arguments with 'number', 'string', 'boolean' types, otherwise throw error message.

strictReturn

Decorate function with returned value validation.

function() {
    ...
    retrun result;
}.decorate('strictReturn', 'number')

Will require 'number' type value to be returned by the function, otherwise throw error message.

throttle

Decorate function with trottle pattern: it allows to call function only once per interval other calls within this interval will be ignored.

function throttled() {
    ...
}.decorate('throttle', 3000)

In case throttled() called it will run immediately and other calls within 3000ms will be ignored.

debounce

Decorate function with debounce pattern: it allows to slow down the function calls, in case debounced function is called it will run only after interval. In case function will be called within this interval it will clear old one and create new interval.

It could be used with auto-suggestion that runs request after keypressed: Press some key, in case no other key pressed within 300 ms it will make ajax request.

$('auto-suggested-field').addEvent(
    'keyup',
    sendRequest.decorate('debounce', 300)
);

queue

Decorate function with queue pattern: all function calls will be run one by one after interval ms.

function queued() {
    alert('call')
}.decorate('queue', 3000)

queued();
queued();
queued();

alert('call'); // Shown immediately
alert('call'); // After 3000 ms
alert('call'); // After 6000 ms

profile

Decorate function with fireBug profiler, so each run will be profiled with it

var calculating = function() {
    ...
}.decorate('profile', 'Profiling calculating() method');

deprecate

Decorate function deprecation warning

var getElementByClass = function() {
    ...
}.decorate('deprecate', 'This method is deprecated. Use $$ instead');

Console will show the deprecation warning: This method is deprecated. Use $$ instead + call stack that will help to find where this method was used

Check source for more docs


Discuss

A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.

Found a bug in this plugin? Please report it this repository's Github Issues.

blog comments powered by Disqus