Requires

Provides

Class.Macros.js

A few functions that simplify definition of everyday methods with common logic

License:
MIT-style license.
  1. 21
  2. 22
  3. 23
  4. 24
  5. 25
  6. 26
Class.hasParent = function(klass) { var caller = klass.$caller; return !!(caller.$owner.parent && caller.$owner.parent.prototype[caller.$name]); }; Macro = {};

Make stackable function what executes it’s parent before itself

  1. 31
  2. 32
  3. 33
  4. 34
  5. 35
  6. 36
Macro.onion = function(callback) { return function() { if (!this.parent.apply(this, arguments)) return; return callback.apply(this, arguments) !== false; }; };

Make getter-function with cache. Returned function alculates values on first call, after return this[name]. To reset cache use:

delete this[name];

  1. 45
  2. 46
  3. 47
  4. 48
  5. 49
  6. 50
Macro.getter = function(name, callback) { return function() { if (!this[name]) this[name] = callback.apply(this, arguments); return this[name]; }; };

Make function that runs it’s parent if it exists, and runs itself if does not

  1. 56
  2. 57
  3. 58
  4. 59
  5. 60
  6. 61
  7. 62
  8. 63
  9. 64
Macro.defaults = function(callback) { return function() { if (Class.hasParent(this)) { return this.parent.apply(this, arguments); } else { return callback.apply(this, arguments); } }; };

Make function what returns property ‘name’ of passed argument

  1. 69
  2. 70
  3. 71
  4. 72
  5. 73
Macro.map = function(name) { return function(item) { return item[name]; }; };

Make function Macro.map but diference that Macro.proc calls ‘name’ method

  1. 78
  2. 79
  3. 80
  4. 81
  5. 82
Macro.proc = function(name, args) { return function(item) { return item[name].apply(item, args || arguments); }; };

Make function what call method ‘method’ of property this[name] with passed arguments

  1. 87
  2. 88
  3. 89
  4. 90
  5. 91
Macro.delegate = function(name, method) { return function() { if (this[name]) return this[name][method].apply(this[name], arguments); }; };