Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting Moment to work in Clojurescript with cljsjs/moment

In my project.clj I've defined the dependency

[cljsjs/moment "2.10.6-0"]

and I'm requiring it in my cljs file using

(:require [cljsjs.moment :as m])

I try and use it like this

(m/from-now 3485736776)

But it says Uncaught ReferenceError: cljsjs is not defined

I also tried

(m/fromNow 3485736776)

Just in case the function call was wrong, but I get the same error ... any ideas?

like image 536
stukennedy Avatar asked Oct 23 '15 15:10

stukennedy


2 Answers

There's a explanation in cljsjs's wiki page => A Quick JavaScript Interoperability Refresher

basically, require cljsjs's package without :as or :refer. and the library--moment in your case, available in js global namespace. Here is a example from my cljs repl:

(require '[cljsjs.moment])
;;=> nil
(js/moment)
;;=> #object[Moment Sat Oct 24 2015 04:29:19 GMT+0700]
(.fromNow (js/moment) 3485736776)
;;=>"a few seconds"
like image 192
mavbozo Avatar answered Jan 03 '23 14:01

mavbozo


In (:require [cljsjs.moment :as m]) remove the :as m and that should work. After this the functions in moment will appear under js/.

See https://github.com/cljsjs/packages/wiki/Using-Packages for an example using cljsjs.Showdown.

like image 22
Asim Jalis Avatar answered Jan 03 '23 14:01

Asim Jalis