Created
April 29, 2012 04:44
-
-
Save danshultz/2533090 to your computer and use it in GitHub Desktop.
Promises Blog Post (http://weavingcode.blogspot.com/2012/04/just-promise.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{promise} = require('model_promise') | |
render: (discussion) -> | |
# render stuff | |
change: (discussion_id) -> | |
@discussion_id = discussion_id | |
$.when(promise(Discussion, discussion_id)).then => | |
@render(Discussion.find(discussion_id)) | |
# Multiple models needed - no worries | |
change: (discussion_id, user_id1, user_id2) -> | |
$.when(promise(Discussion, discussion_id), promise(User, user_id1, user_id2)).then => | |
# whatever you need, all models are ensured to be present |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
render: (discussion) -> | |
# render stuff | |
change: (discussion_id) -> | |
@discussion_id = discussion_id | |
# if the discussion has already been retrieved from the server (exists is local storage) | |
# then directly render it | |
if Discussion.exists(discussion_id) | |
@render(Discussion.find(discussion_id)) | |
else | |
# else bind to the refresh event, and just fetch that single discussion | |
Discussion.bind('refresh', @_onDiscussionRefresh) | |
Discussion.fetch(id: discussion_id) | |
_onDiscussionRefresh: => | |
# when invoked, double check to ensure that the item you expect to be present | |
# otherwise wait until it comes back. | |
if Discussion.exists(@discussion_id) | |
Discussion.unbind('refresh', @_render) | |
@render(Discussion.find(@discussion_id)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{flatten} = require('lib/util_functions') | |
promise = (model, ids...) -> | |
ids = flatten(ids) | |
promises = ids.map (id) -> | |
ModelPromise.getPromise(model, id) | |
return $.when.apply($, promises) | |
class ModelPromise | |
@getPromise: (model, id) -> | |
(new @(model, id)).promise() | |
constructor: (model, id) -> | |
@model = model | |
@id = id | |
promise: -> | |
@_promise ||= @deferred().promise() | |
deferred: -> | |
@_deferred ||= @_buildDeferred() | |
_buildDeferred: -> | |
deferred = $.Deferred() | |
if @model.exists(@id) | |
deferred.resolve() | |
else | |
@model.bind('refresh', @_resolve) | |
@model.fetch(id: @id) | |
return deferred | |
_resolve: => | |
if @model.exists(@id) | |
@deferred().resolve() | |
exports.promise = promise | |
exports.ModelPromise = ModelPromise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment