interop - Invoking custom javascript functions in clojurescript -
my project.clj follows :
(defproject cljsbuild-example-simple "0.3.2" :description "a simple example of how use lein-cljsbuild" :source-paths ["src-clj"] :dependencies [[org.clojure/clojure "1.5.1"] [compojure "1.0.4"] [hiccup "1.0.0"]] :plugins [[lein-cljsbuild "0.3.2"] [lein-ring "0.7.0"]] :cljsbuild { :builds [{:source-paths ["src-cljs"] :compiler {:output-to "resources/public/js/main.js" :libs ["src-js/jsfuncs.js"] :optimizations :whitespace :pretty-print true}}]} :ring {:handler example.routes/app})
jsfuncs.js contains following code :
function calculate(a,b,c) { d = (a+b) * c; return d; }
how invoke js function within clojurescript files? tried invoke function via :
(js/calculate 4 5 6)
but did not work. thanks.
the main problem google closure compiler needs read javascript , able understand it. normal js code lacks hints (jsdoc tags) , namespaces necessary compiler.
the jsdoc tags kind of optional (they compiler catch errors , optimize code better) namespace parts need added. be:
goog.provide('jsfuncs'); /** * @param {number} * @param {number} b * @param {number} c * @return {number} */ jsfuncs.calculate = function(a, b, c) { d = (a+b) * c; return d; };
the other method give closure compiler extern file describes js code. i'm not 100% how there pretty documentation google. there large extern files on project's homepage cover jquery, angular, , other common libraries.
after doing 1 of these methods can call calculate this:
(ns some.namespace (:require [jsfuncs :as jsf])) (console/log (jsf/calculate 1 2 3))
Comments
Post a Comment