Skip to content

Instantly share code, notes, and snippets.

@sarink
Last active August 29, 2015 14:04
Show Gist options
  • Save sarink/323ed6a7e85ef919feb9 to your computer and use it in GitHub Desktop.
Save sarink/323ed6a7e85ef919feb9 to your computer and use it in GitHub Desktop.
// Adds an `append` option to the Backbone Router
Backbone.Router.prototype.navigate = (function(origNavigate) {
return function(fragment, options) {
if (options && options.append) {
fragment = Backbone.history.fragment + fragment;
delete options.append;
}
return origNavigate.call(this, fragment, options);
};
})(Backbone.Router.prototype.navigate);
// Converts to camelCase keys
Backbone.Model.prototype.parse = (function(origParse) {
return function(response, options) {
response = _.deepCamelizeKeys(response);
return origParse.call(this, response, options);
};
})(Backbone.Model.prototype.parse);
// Converts to camelCase keys
Backbone.Collection.prototype.parse = (function(origParse) {
return function(response, options) {
response = _.deepCamelizeKeys(response);
return origParse.call(this, response, options);
};
})(Backbone.Collection.prototype.parse);
// Because Backbone performs a `set()` on the passed in `attrs`, and we might pass in snake case attrs that we don't want to set...
// Intercept all calls to `save`, and modify the behavior to suit our needs
// See: https://github.com/jashkenas/backbone/issues/3232
Backbone.Model.prototype.save = (function(origSave) {
return function(key, val, options) {
// Handle both `"key", value` and `{key: value}` -style arguments.
var attrs;
if (key == null || typeof key === "object") {
attrs = key;
options = val;
} else {
(attrs = {})[key] = val;
}
// Temporarily override `toJSON` to return just the `attrs` we want, and also convert them to snake case
var origToJSON = this.toJSON;
this.toJSON = function() { return _.deepSnakeizeKeys(attrs || origToJSON()); };
// Store the result of save that we want to return
var result = origSave.call(this, null, options);
// Restore the `toJSON` function
this.toJSON = origToJSON;
// Return the xhr object we got back from proto's `save`
return result;
};
})(Backbone.Model.prototype.save);
// Uncomment once https://github.com/jashkenas/backbone/issues/3232 has been merged in
/*
Backbone.Model.prototype.save = (function(origSave) {
return function(key, val, options) {
// Handle both `"key", value` and `{key: value}` -style arguments.
var attrs;
if (key == null || typeof key === "object") {
attrs = key;
options = val;
} else {
(attrs = {})[key] = val;
}
// Convert attributes to snake case, see: https://github.com/jashkenas/backbone/issues/3232
options = _.extend({deepSnakeizeKeys: true}, options || {});
if ((options.attrs && options.deepSnakeizeKeys) || (attrs && !options.attrs && options.patch && options.deepSnakeizeKeys)) options.attrs = _.deepSnakeizeKeys(attrs);
return origSave.call(this, attrs, options);
};
})(Backbone.Model.prototype.save);
Backbone.AssociatedModel.prototype.toJSON = (function(origToJSON) {
return function(options) {
return (options.deepSnakeizeKeys) ? _.deepSnakeizeKeys(origToJSON.call(this, options)) : origToJSON.call(this, options);
};
})(Backbone.AssociatedModel.prototype.toJSON);
Backbone.Model.prototype.toJSON = (function(origToJSON) {
return function(options) {
return (options.deepSnakeizeKeys) ? _.deepSnakeizeKeys(origToJSON.call(this, options)) : origToJSON.call(this, options);
};
})(Backbone.Model.prototype.toJSON);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment