clojurescript - Calling a ".done" JS callback -
i'm having trouble wrapping js library because can't .done callback work correctly. in javascript code this:
db.values("inventory").done(function(item) { console.log(item); });
so tried couple (very dirty) clojurescript methods translate this:
(defn log [] (console/log "working?")) (defn stock [] (#(.done % log) (.values db "inventory")))
and
(defn stock [] (js* "db.values('inventory').done(function(item) { console.log(item); })"))
but neither of these worked. error message like: db.values(...).done not function
are there protocol extensions (or else) used here cover js callback? otherwise, can goog.async.deferred somehow intercept callback again?
maybe helps you! i've done node must work browser little details
first demo code i've prepared mock js lib simulate yours (my_api.js)
this my_api.js
console.log("my_api.js"); var db={}; db.item=""; db.values=function(_string_){ db.item="loadign_"+_string_; return this; }; db.done=function(_fn_){ _fn_(db.item); }; var api={hello:"ey ", db:db}; module.exports=api; // pretended chain calls // db.values("inventory").done(function(item) { // console.log(item); // });
and clojurescript code ...
(ns cljs-demo.hello) (defn example_callback [] (let [my-api (js/require "./my_api") ; api.js lib used example db (aget my-api "db") ; db object my_fn (fn [item] ;this callback function (println "printing clojurescript" item) ) ] (do (-> db (.values "inventory") (.done my_fn)) ;; calling js in similar way want in js ;; or (.done (.values db "inventory_bis") my_fn) ;; calling nested form in standar lisp manner ) ) ) (set! *main-cli-fn* example_callback) ;default node function
and console (node.js)
node your_output_node.js
and you'll obtain
printing clojurescript loadign_inventory printing clojurescript loadign_inventory_bis
good luck,
juan
Comments
Post a Comment