Skip to content

Instantly share code, notes, and snippets.

@cblunt
Created July 22, 2009 09:28
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 cblunt/151902 to your computer and use it in GitHub Desktop.
Save cblunt/151902 to your computer and use it in GitHub Desktop.
Adds an attributize() method to javascript String objects. Converts the sender string into an object attribute format for submission to a server.
Object.extend(String.prototype, {
/**
* Converts an underscore string into object attribute format for submission
* to a server (Based on Rails).
*
* To attributize the string client_firstName
* "client_firstName".attributize()
* => client[first_name]
*
* To attributize the string client_addresses__street
* "client_addresses__street".attributize()
* => client[addresses][][street]
*/
attributize: function() {
var parts = this.split("_");
var attributized_string = "";
parts.each(function(s, index) {
s = s.underscore();
if(index > 0)
s = '[' + s + ']';
attributized_string += s;
});
return attributized_string;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment