equivalent of $(document).ready(function(){}) in clojurescript?
Asked Answered
K

3

6

how implement $(document).ready(function(){}) in clojurescript. I tried this:

 (. ready js/document ());;but i am trying to achieve the callback function

But doesn't seem right to me. Any ideas?

new to clojurescript so i am bit confused as to how to do this.

Kootenay answered 29/2, 2016 at 18:12 Comment(0)
V
8

This should work:

(.addEventListener
  js/window
  "DOMContentLoaded"
  (fn [] (.log js/console "DOMContentLoaded callback")))
Vadim answered 29/2, 2016 at 19:23 Comment(0)
F
3

For simply a clojurescript entry point, you may implement a main function in e.g. the core namespace:

(ns app.core)

(defn main []
  (activate-app))

Then call the entry point at the end of the module:

(main)

The idea is to have the entry point main function called after all code has been loaded. Hence the module with the entry point call should not itself be required by any other modules.

A variation sets up an entry point explicitly called from javascript after the compiled clojurescript has been loaded:

(defn ^:export main []
  (activate-app))

(set! js/cljs-entry-point main)

This entry point can now be called from a script element at the bottom of the body of the associated html document:

<script>cljs_entry_point()</script>

A benefit with the latter approach is that other modules still can require the module containing the entry point.

Frohne answered 1/3, 2016 at 21:43 Comment(0)
I
3

Consider a function main which calls a number of other functions.

(defn main []
  (app-instruction-1)
  (app-instruction-2))

(set! (.-onload js/window) main)
Incontestable answered 24/8, 2017 at 22:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.