Created
April 5, 2012 02:43
-
-
Save unscriptable/2307598 to your computer and use it in GitHub Desktop.
wire plugin to create functions without violating JSON
This file contains 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
define({ | |
myFunc: { | |
expr: { | |
body: 'alert(e.target.textContent);', | |
params: ['e'] | |
} | |
}, | |
body: { | |
getElement: document.body, | |
init: { | |
addEventListener: [ | |
'mouseover', | |
{ $ref: 'expr!this.style.color="red";' }, | |
false | |
] | |
}, | |
ready: { | |
addEventListener: [ | |
'click', | |
{ $ref: 'myFunc' }, | |
false | |
] | |
} | |
}, | |
expr: { module: 'wire-expr/expr' }, | |
dom: { module: 'wire/dom' }, | |
debug: { module: 'wire/debug' } | |
}); |
This file contains 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
(function (define) { | |
define(['when'], function (when) { | |
"use strict"; | |
/** | |
* Create a new function from JSON-compatible strings. | |
* @param [params] {Array} names of the formal parameters | |
* @param body {String} body of function | |
* @returns {Function} | |
*/ | |
function exprToFunc (params, body) { | |
var args; | |
// if no body, params is the body | |
args = arguments.length > 1 ? params.concat(body) : [params]; | |
// return a function | |
return Function.apply(this, args); | |
} | |
exprToFunc.wire$plugin = function(ready, destroyed, options) { | |
return { | |
resolvers: { | |
expr: exprResolver, | |
expression: exprResolver | |
}, | |
factories: { | |
expr: exprFactory | |
} | |
} | |
}; | |
return exprToFunc; | |
function exprFactory (resolver, spec, wire) { | |
when(wire(spec.expr), function (options) { | |
if (typeof options == 'string') { | |
return exprToFunc(options); | |
} | |
else { | |
return createExpr(options); | |
} | |
}).then(resolver.resolve, resolver.reject); | |
} | |
function exprResolver (resolver, expression, refObj, wire) { | |
when(wire(refObj.params), function (params) { | |
return createExpr({ body: expression, params: params }); | |
}).then(resolver.resolve, resolver. reject); | |
} | |
function createExpr (options) { | |
var params, body; | |
body = options.body || options.js || options.$ref; | |
params = options.params && [].concat(options.params) || []; | |
return exprToFunc(params, body); | |
} | |
}); | |
}( | |
typeof define == 'function' && define.amd | |
? define | |
: function (deps, factory) { module.exports = factory(deps.map(require)); } | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @briancavalier,
I know how you feel about connecting to methods versus bare functions, but I just wanted to see how hard it would be to construct bare functions. Turns out its wicked easy! :)
-- J