Created
January 19, 2012 05:59
-
-
Save zackdouglas/1638258 to your computer and use it in GitHub Desktop.
Partials, Memoes, and Bindings
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
var | |
myObject = { | |
myfield: 'Not empty' | |
}, | |
validations = { | |
myfield: Namespace.Validators.getNotEmpty('myfield', 'My Field')(myObject) | |
}; |
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 ( 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 ); |
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 (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