1) You can pass a {wait:True} as the second argument to collection.create(), and the add event won't be fired until after a successful sync. If you want error code, you add error:funciton(){..} to the create options hash.
2) You can change the event listened for from 'add', to 'sync', and still have the error handling code in the options hash.
3) You can manually create the model, call save() and have the success callback be code to add the model to the collection, and the error code do something else.
In all of these, you'd just have to add a function to the view to indicate error, and do what you want to handle it. (Depending on the specifics, this could be for the NewStatusView or for the StatusesView..)
> 1) You can pass a {wait:True} as the second argument to collection.create(), and the add event won't be fired until after a successful sync. If you want error code, you add error:funciton(){..} to the create options hash.
This is problematic. Now you completely lose the "add" event, so if you wanted to do something like add a spinner while the request is in flight, that is not possible.
> 2) You can change the event listened for from 'add', to 'sync', and still have the error handling code in the options hash.
But now you are asking about the sync status from a collection. It doesn't seem relevant to ask a collection about it's sync status when you are interested in the status of a form submission.
> 3) You can manually create the model, call save() and have the success callback be code to add the model to the collection, and the error code do something else.
This seems to be the best option to me. It puts the success, error, and model all in the same frame, which puts us in the same place as the original jQuery.
The jQuery code was far more elegant that the author would have you believe. At the core, you are handling a user interaction which comes in units of a "submission". The jquery code, wraps a "submit" into a singe activation frame and handles it.
The backbone wraps this "submit" unit up, passes it through a collection, some automated saving, and now you have no way to reason about what comes out the other end. When you submit 5 times, and have 2 failures, what do you do? What is a collection going to know?
I recognize the problem with the original jquery, but this ain't the right solution.
1) the add event isn't lost. It just doesn't fire until a success. Want to add a spinner, have it fire in the appropriate view as a response to the "submit form" event.
2) That isn't how sync is defined, and sure there may be a better name for it, but this isn't what it means. Lots of information is passed into the callback. Useful information, stuff that contradicts your objections.
3) Not the same as JQuery - you can reason about what comes out the other end, because the information is actually in the collection, with an api for getting it. The events have a great set of arguments for them.
Muddling the conversation with such incorrect assertions (aka FUD) is counter-productive.
You can listen to the collection event "error" to display error messages.
Regarding MVC I don't really know. Everyone seems to have their own definition of what MVC should be.
http://alexmaccaw.com/posts/async_ui http://www.infoq.com/presentations/Asynchronous-UI
Next, we leveraged the Promise API to handle success and error callbacks, specifically:
collection.create(bleh)
.done(function (model) { var xhr = this; })
.fail(function (resp) { var xhr = this; });
This is far cleaner that screwing around with success/error callbacks which don't work well when you're trying to mock the backend. If you want to get fancy (we did), you can build a default fail function and always pass that.
This looks like one hell of a bastardization of MVC. It seems the view is really a controller+view, and the model does the sync in a way that requires coupling the view to the model to handle any sort of error.
Edit: I could be wrong here, but can someone show me how to fix the Backbone code to handle an error correctly? Error handling can be added to the jQuery code by adding 'error:function() { // do something simple },'