jsblocks
jsblocks copied to clipboard
error handling for failed ajax request with the Model/Collection functions
I think jsblocks should provide an api for the user for handling failed request. I see some possible ways to register such a handler: The often in nodejs used way:
myCollection.read(function(data, err) {
// err here as the second argument to not break compatibility
});
Or the event way:
myCollection.on('error' /* or maybe ajax error */, function (err) {...});
Or when creating the Model/Collection:
var MyModel = App.Model({...}, {
options: {
read: {
url: '...',
error: function(err) {...}
}
}
});
What do you think ?
I like the first solution. It will be the most suitable for the current implementation and usage.
I also like the second one but if we implement this we should add support for all other events.
The third one looks strange because we are placing event handlers in the configuration which is a precedent for this configuration options.
I vote for the first one. If you have any considerations for one of the others please share them.
I agree with you. The first one is also my favorite, that's why it's the first :). The last one is weird, but commonly implemented as a "default" handle in frameworks and I thought I mention it.
I also like the second but to be consitent we should implement for all events and I think currently we have more important features/bugs for jsblocks.
[edit: fixed typo]
I agree.
Can this be extended to provide a callback function regardless of response, and to fire on a sync()
method as well?
This is because as jsBlocks currently stands, if I want to use sync()
to create a record, no response is returned and so I can't display a success message to a user reliably. I could of course listen to AJAX related events, but there is no guarantee it would be the correct event if two syncs are called at the same time.
Currently users resort to using other ajax implementations to create/delete/update records instead of the built in sync()
due to this reason.
myCollection.read(function(data, XHR) {
// Similar to a jQuery AJAX call, the second parameter could be an XHR object so that the status code can be read giving an extra level of feedback
});
// seeing as one sync operation could include create/update/delete requests all at once, it makes sense to also include an event parameter, or some other way of letting the user know which operation was performed
myCollection.sync(function(e, data, XHR) {
var data = data || {success: false};
if (e.type = 'blocks.delete' && data.success == true) {
// render success message by updating an alerts property in the view or similar
self.alerts.push('Successfully Deleted')
}
});