-
-
Save mren/fe25760ca44414ab2707 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Men, can you share function urlError(). ty