Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 216
Coding Enthusiast Avatar asked Feb 29 '16 18:02

Coding Enthusiast


People also ask

What is $( document ready () equivalent in JavaScript?

jQuery $(document). ready() Equivalent in JavaScriptThis event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.

How do I use document ready in JavaScript?

$( document ). ready() ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ). on( "load", function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready.

What is ready function in JavaScript?

jQuery ready() MethodThe ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs.


2 Answers

This should work:

(.addEventListener
  js/window
  "DOMContentLoaded"
  (fn [] (.log js/console "DOMContentLoaded callback")))
like image 181
Piotrek Bzdyl Avatar answered Nov 09 '22 01:11

Piotrek Bzdyl


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.

like image 29
Terje Norderhaug Avatar answered Nov 08 '22 23:11

Terje Norderhaug