Skip to content
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

error handling for failed ajax request with the Model/Collection functions #99

Open
Kanaye opened this issue Oct 7, 2015 · 4 comments

Comments

@Kanaye
Copy link
Collaborator

Kanaye commented Oct 7, 2015

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 ?

@astoilkov
Copy link
Owner

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.

@Kanaye
Copy link
Collaborator Author

Kanaye commented Oct 8, 2015

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]

@astoilkov
Copy link
Owner

I agree.

@jakewhiteley
Copy link

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')
   }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants