Skip to content

Instantly share code, notes, and snippets.

@mren
Created July 2, 2011 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mren/fe25760ca44414ab2707 to your computer and use it in GitHub Desktop.
Save mren/fe25760ca44414ab2707 to your computer and use it in GitHub Desktop.
// This model allows to change the id, but save/put the model to the old Id
var ChangeableIdModel = Backbone.Model.extend({
set: function(attr, options) {
if (attr && attr['id'] && !this.oldId ) this.oldId = this.get('id');
Backbone.Model.prototype.set.call(this, attr, options);
return this;
},
url: function() {
var base = getUrl(this.collection) || this.urlRoot || urlError();
if (this.isNew()) return base;
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.oldId || this.id);
},
save: function(attr, options) {
options || (options = {});
var model = this;
var success = options.success;
options.success = function(resp, status, xhr) {
if (model.changedId) delete model.oldId;
if (success) success(model, resp, xhr);
};
Backbone.Model.prototype.save.call(this, attr, options);
}
});
var getUrl = function(object) {
if (!(object && object.url)) return null;
return _.isFunction(object.url) ? object.url() : object.url;
};
@mpadronm90
Copy link

Men, can you share function urlError(). ty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment