Created
March 19, 2009 18:37
-
-
Save jashkenas/81993 to your computer and use it in GitHub Desktop.
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
UNSAFE_PARAM_MATCHER : (/([^!]+)(!?)/) | |
/** | |
* Convert this object into a params hash. Useful for rendering templates | |
* from the model data and for converting the item into server-ready params. | |
* Pass in a list of fields, which will be mapped to the results of their | |
* method (if a method of that name exists) or properties otherwise. If you | |
* append '!' to the field name, you'll get the SafeString#unsafe() version. | |
* @function ? | |
*/ | |
toParams : function() { | |
var fields = $A(arguments); | |
if (fields.length == 0) return this.toObject(); | |
var params = {}; | |
for (i=0; i<fields.length; i++) { | |
var field = fields[i]; | |
var match = field.match(ZenItem.UNSAFE_PARAM_MATCHER); | |
var key = match[1], unsafe = !!match[2]; | |
var val = this[key]; | |
val = val ? (Object.isFunction(val) ? this[key]() : val) : this.get(key); | |
params[key] = unsafe ? val.unsafe() : val; | |
} | |
return params; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment