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

Two simple macros to ease dealing with callbacks #24

Closed
wants to merge 1 commit into from
Closed

Two simple macros to ease dealing with callbacks #24

wants to merge 1 commit into from

Conversation

pyr
Copy link

@pyr pyr commented Nov 23, 2012

with-json is more of an example, it could use
a more complex form where settings would be merged

It already allows constructs like:

(when-done 
  (with-json data "/api/some/method" (update-model-with data))
  (refresh-view-from-model))

`with-json` is more of an example, it could use
a  more complex form where settings would be merged
@mpenet
Copy link
Collaborator

mpenet commented Nov 23, 2012

Hello Pyr, thanks for the proposal.

I am not sure if you noticed, but I added support for deferreds yesterday (it translates 1:1 to jquery, nothing really fancy), it allows to do almost the same but in a more composable way imho.

Some details about the PR: .done callback can be called with an argument, and the error handler using console.log could be problematic in production (even though it's possible to overwrite console.* fns to avoid that, I believe it's often better to handle the error).

I was thinking about maybe adding something like a deferred monad:

(let-deferred-m [a (ajax {:method :get  :url "foo"})
                 b (ajax {:method :get  :url "bar")
                 c (ajax {:method :get  :url "baz"})
                 :when (seq c)]
  (merge a b c))

Roy has something similar (ajaxRequest monad), it is really pleasant to use, but I am not so sure if that belongs jayq (maybe too alien in the clojure world, I don't know).

type Request = {url: String, payload: String}

let ajaxRequest = {
  return: \x -> x
  bind: \(x : Request) f ->
    $.get x.url x.payload f
}

let v = (do ajaxRequest
  value <- {url: '/examples/helloworld.roy', payload: 'stuff'}
  console.log value
  return value
)

console.log v

I'll try to think more about it this evening (gotta work now! :)), but this opens an interesting discussion nonetheless.

@pyr
Copy link
Author

pyr commented Nov 23, 2012

I didn't notice, sorry about that, TBH it's just a plain paste of the macros I had in a local project. I'll see if there's
a better way to wrap this based on your deffered fn's

I like your deffered monad idea, maybe there's a place for a simple construct for the simple query case though (wrapping a body when an ajax call returns)

maybe something like

(cond-ajax "/api/foo" {:method :post :data my-data} data
   200   (body-on-success)
   404   (body-on-not-found)
   :else  (default-body))

@mpenet
Copy link
Collaborator

mpenet commented Nov 23, 2012

Closing, as discussed on irc

@mpenet mpenet closed this Nov 23, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants