Last active
December 3, 2015 17:37
-
-
Save misantronic/160c0d1828a051df8be4 to your computer and use it in GitHub Desktop.
Make Backbone.Model's fetch() return the model ifself
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
Backbone.Model.extend({ | |
/** | |
* Return the model itself instead of fetched-data object | |
* @param {Object} [options] | |
* @returns {jQuery} | |
*/ | |
fetch: function (options) { | |
options = options || {}; | |
var self = this; | |
var defer = $.Deferred(); | |
Backbone.Model.prototype.fetch.call(this, _.extend(options, { | |
success: function (/* data */) { | |
defer.resolve(self); | |
}, | |
error: function() { | |
defer.reject.apply(this, arguments); | |
} | |
})); | |
return defer.promise(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment