Skip to content

Instantly share code, notes, and snippets.

@jashkenas
Created March 19, 2009 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jashkenas/81993 to your computer and use it in GitHub Desktop.
Save jashkenas/81993 to your computer and use it in GitHub Desktop.
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