Skip to content

Instantly share code, notes, and snippets.

@zackdouglas
Created January 19, 2012 05:59
Show Gist options
  • Save zackdouglas/1638258 to your computer and use it in GitHub Desktop.
Save zackdouglas/1638258 to your computer and use it in GitHub Desktop.
Partials, Memoes, and Bindings
var
myObject = {
myfield: 'Not empty'
},
validations = {
myfield: Namespace.Validators.getNotEmpty('myfield', 'My Field')(myObject)
};
(function ( u ) {
u.memoize = function ( fn, context ) {
if ( fn.__mcache__) { return fn; }
var slice = Array.prototype.slice;
fn.__mcache__ = {};
return function ( _, __ ) {
var __margs__ = slice.call( arguments ),
__masig__ = JSON.stringify( __margs__ );
if (! fn.__mcache__[__masig__] ) { fn.__mcache__[__masig__] = fn.call( context, __margs__ ); }
return fn.__mcache__[__masig__];
}
};
})( Namespace.Utils );
(function (v, u) {
v.getNotEmpty = u.memoize( function ( fieldName, displayName, errMsg ) {
displayName = displayName || fieldName;
errMsg = errMsg || displayName + ' cannot be blank';
return function ( content ) {
var v = content[fieldName];
if ( 1 + [null, undefined, ''].indexOf(v) ) {
return new Error( errMsg );
}
}
});
})( Namespace.Validators, Namespace.Utils );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment