Created
July 22, 2009 09:28
-
-
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.
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
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