Skip to content

Instantly share code, notes, and snippets.

@jeffreyiacono
Created December 20, 2011 20:59
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 jeffreyiacono/1503244 to your computer and use it in GitHub Desktop.
Save jeffreyiacono/1503244 to your computer and use it in GitHub Desktop.
another little javascript helper
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