public
Created

another little javascript helper

  • Download Gist
helpers.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
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;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.