-
Notifications
You must be signed in to change notification settings - Fork 108
Add support for setting collection with a promise #103
base: master
Are you sure you want to change the base?
Conversation
Some APIs (flickr) require you to call multiple endpoints before returning useful data. In these cases, you need to return a promise for the data in parseBeforeLocalSave that gets resolved once all the API calls are complete. This change adds support so that the promise is handled properly.
|
I really like where you're going with this. Are you doing the try/catch to catch the case when if resp.done?
resp.done (data) -> setResp data
else
setResp respI think for tests on this new code, it might be most meaningful to add an integration test to spec/integration_spec.coffee, where you create a collection, call fetch, assert that a promise is returned, and that its done callback is called. |
|
The try/catch is for when |
|
Yes. Later down the road if we call a property or method that doesn't exist in getResp, it'll also catch those. For example:
In this case, it's not catastrophic because |
|
Ok. Fine by me. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed you added a do (model) -> closure wrapper, and you also are now passing model to setResp. Is this just to help with understandability with the model/collection naming mess, or is there a functional benefit that I'm missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The closure is needed because .done is aysnc. Without the closure, the value of model will have changed before .done is resolved. http://coffeescript.org/#slices (section directly above this). I'm passing model to setResp to preserve the original functionality.
|
Looking at this at a higher level, I do understand the need to do this: photos.fetch().done -> ...What I don't understand is how this change helps accomplish that. I wrote up a spec to see if it was working despite my understanding. It fails with describe 'deferreds', ->
it 'returns a deferred', ->
{callbackResponse, deferred} = {}
runs ->
model.remote = true
deferred = model.fetch success: (args...) -> callbackResponse = args
waitsFor (-> callbackResponse), "The success callback for 'fetch' should have been called", 100
runs ->
expect(_.isFunction(deferred.done)).toBeTruthy() |
|
I'll get to your previous questions shortly. This PR doesn't address your spec. See Issue #56 for that. |
|
Okay, I think I understand the purpose of this then. The issue was that the |
|
Almost. |
Some APIs (flickr) require you to call multiple endpoints before returning useful data. In these cases, you need to return a promise for the data in parseBeforeLocalSave that gets resolved once all the API calls are complete. This change adds support so that the promise is handled properly.
Usage
models/photos.coffeeThen elsewhere