Created
December 20, 2011 20:59
-
-
Save jeffreyiacono/1503244 to your computer and use it in GitHub Desktop.
another little javascript helper
This file contains hidden or 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 ApplicationHelpers = { | |
/** | |
* Note: requires Underscore.js | |
* | |
* Enable a (sencha touch) form to have a link between | |
* model and form (ie. MyModel.some_attribute <=> textfield.some_attribute) | |
* AND allow for POST / PUT submission in the rails-friendly namespaced format | |
* (ie. {my_model[some_attribute] : "some value"}). | |
* | |
* To get above behavior, treate model and forms in normal fashion and use | |
* helper when settings the params for ajax submission (note: using ST 2 PR3): | |
* Ext.Ajax.request({ | |
* url: '/my_model.json', | |
* method: 'POST', | |
* params: helpers.namespace_keys('my_model', params), // <= hooray! | |
* ... | |
*/ | |
namespace_keys: function(namespace, obj) { | |
var self = this, | |
keys = _.keys(obj); | |
return _.reduce(keys, function(newObj, k) { | |
newObj[namespace + '[' + k + ']'] = obj[k]; | |
return newObj; | |
}, {}); | |
} | |
}; | |
// alias (it's not just a tv show) | |
var helpers = ApplicationHelpers; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment