spine / spine

Lightweight MVC library for building JavaScript applications

Home Page:http://spine.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Model.find and Model.fetch with done & fail options

richard-flosi opened this issue · comments

Maybe there is a better way of handling my case, but I'm finding myself wanting to be able to use the done and fail options with fetch and/or find on a model like I can on save.

e.g.

MyModel.fetch({
    id: 1234
}, {
    done: function() { alert('done'); },
    fail: function() { alert('fail'); }
});

Right now if a fetch fails I need to listen for the ajaxError event and handle it there. But this gets messy b/c in another part of my code I need to handle the case when an instance of a model is destroyed, trying to use the fail option to catch the error, but it's hitting my ajaxError handler first.

How would you feel about adding support for done and fail options to find and/or fetch or is there something I'm missing here?

I think mostly the model events system needs to be suplementeed by some sort of consistant promise system. In my head I am tossing around a fairly major overhaul to spines model implementation that uses promises. My loosely held opinion is that adding done and fail type stuff in this manner would be mostly a temporary patch that will only sort of work. That said if you want to implement it and such I would be open to it. I am just planning to tackle the issue from a different direction.... some day.

I really like returning a promise so that chaining can be used. Example:

model.fetch().done(-> console.log(arguments)).fail(-> console.error(arguments))

jQuery.Deferred pattern seems like the most standard option for client-side promises, IMO.

@thesmart there's been a bunch of discussion about promises over in #532. I don't think we want to use jQuery.Deferred because that API isn't compatible with native ES6 promises that are going to become the standard.

Really looking forward to having promise support in Spine though!

what about using the new fetch apis that browsers are starting to implement?