Created
September 13, 2013 14:42
-
-
Save jepetko/6551635 to your computer and use it in GitHub Desktop.
This code simulates AngularJS auto mapping of arguments.
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
.arg { | |
border: 1px solid blue; | |
background: lightgrey; | |
margin: 5px; | |
} | |
pre { | |
border: 1px dotted blue; | |
background: lightgrey; | |
padding: 5px; | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="[add your bin description]" /> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> | |
<meta charset=utf-8 /> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
</body> | |
</html> |
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
'use strict'; | |
/** | |
* this will just publish the arguments passed to the particular controllers | |
*/ | |
function publishArguments() { | |
var constr = this.__proto__.constructor.toString(); | |
$('<br><br><div>object of the class <pre>' + constr + '</pre> created by auto-mapping with the values from the global namespace:</div>').appendTo('body'); | |
var len = arguments.length; | |
for(var i=0; i<len; i++) { | |
var el = arguments[i]; | |
$('<div class="arg">' + el + '</div>').appendTo('body'); | |
} | |
} | |
/** | |
* the first controller demands 3 arguments. | |
*/ | |
function MyControllerTripple(a, b, c) { | |
publishArguments.apply(this, arguments); | |
} | |
/** | |
* the second controller demands 2 arguments. | |
*/ | |
function MyControllerDouble(c, d) { | |
publishArguments.apply(this, arguments); | |
} | |
/** | |
* the first controller demands just a single argument. | |
*/ | |
function MyControllerSingle(e) { | |
publishArguments.apply(this, arguments); | |
} | |
/** | |
* implementation of the framework which can recognize the arity and pass the correct arguments as long as they exist in the global namespace | |
*/ | |
var framework = {}; | |
framework.create = function(str) { | |
if( typeof window[str] !== 'function' ) { | |
throw new Error(str + ' must be a function in global namespace'); | |
} | |
var F = window[str]; //constructor MyController* | |
var serialized = F.toString(); | |
var result = /\(.*\)/g.exec(serialized)[0].replace(/^\(/,'').replace(/\)$/,''); | |
var params = result.split(','); | |
var realValues = []; | |
for( var i=0; i<params.length; i++ ) { | |
var param = params[i].replace(/^\s*/g,'').replace(/\s*$/g, ''); | |
if( typeof window[param] === 'undefined' ) { | |
throw new Error('the demanded parameter ' + param + ' not available'); | |
} | |
realValues.push(window[param]); | |
} | |
return this.wrap(F, realValues); | |
}; | |
/** | |
* this magical wrap method is used to auto apply the detected arguments. | |
*/ | |
framework.wrap = function(f, realValues) { | |
function Wrap(args) { | |
return f.apply(this, args); | |
} | |
Wrap.prototype = f.prototype; | |
return new Wrap(realValues); | |
}; | |
var a = 'A', b = 'B', c = 'C', d = 'D', e = 'E'; | |
framework.create( 'MyControllerTripple' ); | |
framework.create( 'MyControllerDouble' ); | |
framework.create( 'MyControllerSingle' ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment