-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reverting changes from #2003 and 1f3f45252f #2173
Conversation
I dunno about this -- isn't there a way that we can keep things DRY and support the hooks required for folks to be able to extend things comfortably? |
Can't we just enhance plugin extendability as a way to extend functionality w/o stink hooks? Sent via BlackBerry by AT&T -----Original Message----- I dunno about this -- isn't there a way that we can keep things DRY and support the hooks required for folks to be able to extend things comfortably? Reply to this email directly or view it on GitHub: |
After trying out multiple approaches to this, I still think the current implementation is correct. The solution to your example is pretty simple here: var sync = Backbone.sync;
Backbone.sync = function (__, model, options) {
if (model.jsonp) _.extend(options, {dataType: 'jsonp'});
return sync.apply(this, arguments);
};
var ajax = Backbone.ajax;
Backbone.ajax = function (options) {
var success = options.success;
options.success = function (resp, __, xhr) {
if (options.dataType !== 'jsonp') return success(resp);
var meta = xhr.meta = resp.meta;
var data = xhr.data = resp.data;
if (meta.status < 300) return success(data);
options.error(xhr);
};
return ajax.apply(this, arguments);
};
var GitHubUser = Backbone.Model.extend({
url: 'https://api.github.com/users',
jsonp: true
});
var MyUser = Backbone.Model.extend({
url: '/users'
}); |
From the docs:
I understand that not all service layers are RESTful. However, I don't think that the default As for #2003, I think that |
@jashkenas - it wasn't really the hooks I was concerned with (those can be private for that matter), it's more the fact that keeping success/error handlers inside the @caseywebdev's solution seems to work, and after talking a little more with @braddunbar, it sounds like I might be in the minority on that one. I'm not entirely sold on #2003, I don't quite see how it makes sync much more transport agnostic than it was previously (have any sync implementations had issue with this before?) and it doesn't seem to pass the 90% litmus test for cases where someone isn't using jQuery. This is a good example of where it ends up adding an extra step. |
Closing in favor of #2221. |
The changes in 1f3f452 were made to consolidate the
error
andsuccess
handlers to be called/triggered withinBackbone.sync
function. This change makes the other collection/model methods DRY-er internally, but it also encapsulates the event triggers withinBackbone.sync
, making it more difficult to create customsync
handlers directly on the model/collection for cases where the default Backbone sync behavior isn't desired.A good example would be a case where an application is interacting with multiple api's at once - where most models are using a traditional path with correct http codes and response formats, but one is passing through a json-p endpoint where everything returns a 200, and the error code/response is contained in the response.
Previously you'd only need to extend sync like so:
now, you need to also take care of the trigger, and prevent the "sync" event/success handlers which are now is now called internally. To get the same functionality you'd effectively need to add something like this:
The same functionality which was just a few lines previously is now much more work to patch, basically requiring a new sync implementation - and if more functionality is moved at some point out of the individual methods and into the Backbone.sync handler, the developer needs to add more code to create these custom handlers. The
Backbone.wrapError
(while undocumented as a public API) served as a convenient place to centralize any error logic, keeping everything else contained in the methods themselves.The changes from #2003 were aiming to standardize the arguments to make Backbone more "agnostic" with regards to the sync handler, but it seems to complicate the api unnecessarily - of the (
resp
,status
,xhr
), theresp
is the only argument used by Backbone internally. So as long as the first argument is a valid object, any type of sync handler can be dropped in without issue.I could go for keeping 1f3f452 if the argument for keeping these pieces outside of the "Backbone.sync" handler isn't strong enough, but #2003 seems to be a step backward for keeping the "sync" simple, with the need to pass along an options object and model rather than shadowing them from the function calling sync.
Related: #2161, #2140, #2031, #2145, #2094
If this is something we want to eventually merge, I can update the docs as well.
/cc @kpdecker