Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// 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

This comment has been minimized.

Show comment Hide comment
@mpadronm90

mpadronm90 Jan 7, 2015

Men, can you share function urlError(). ty

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