user2176152
user2176152

Reputation: 67

Value called from .cljc to .clj always null

My common.cljc file looks like this:

   (ns example.common
     #?(:cljs
    (:require [goog.dom :as gdom])
       )
    )

    #?(:cljs (enable-console-print!))

   (defn mobile? []
        #?(:cljs (def hvpsize (.. (gdom/getViewportSize) -height))) 
        #?(:cljs (def wvpsize (.. (gdom/getViewportSize) -width))) 
        #?(:cljs (if (<= (/ wvpsize hvpsize) 1) true false))
   )

   (def mobile-value (mobile?))

   (println "mobile-value inside .cljc")
   (println mobile-value)

My routes.clj file looks like this:

     (ns example.routes 
        (:gen-class)
        (:use compojure.core
               example.views
               example.common
               [hiccup.middleware :only (wrap-base-url)])
        )

        (use '[ring.util.response :only [response]])
        (use '[ring.adapter.jetty :only [run-jetty]])
        (require '[compojure.route :as route])

        (defn -main
          "I don't do a whole lot ... yet."
          [& args]
          (println "Hello, World!"))

        (defn d-or-m-version [] 
           (.println System/out "Inside d-or-m-version")
           (.println System/out mobile-value)
           (if mobile-value (root-page-mobile) (root-page-desktop))
           )

        (defroutes main-routes 
           (GET "/" [] (d-or-m-version))
           (route/not-found "404"))

        (defonce server (run-jetty #'main-routes {:port 8080 :join? false}))

mobile-value evaluates correctly when reloading the page in the .cljc file and even in a separate .cljs file I have.

The issue is when mobile-value is evaluated in routes.clj it just returns null. Hence the if statement in d-or-v-version does not call the correct hiccup html from a separate views.clj file.

I am assuming mobile-value is returning null because routes.clj is run before common.cljc while mobile-value has no value. However I have no idea how to resolve this, any help would be appreciated.

Upvotes: 1

Views: 116

Answers (1)

BillRobertson42
BillRobertson42

Reputation: 12883

It evaluates to nil when called from Clojure because it's an empty function in Clojure. The #?(:cljs reader conditional tells the Clojure compiler to ignore this form because it is supposed to be evaluated by the Clojurescript compiler only.

Since all of the forms in that function don't exist to the Clojure compiler, the function is empty, and it will always evaluate to nil.

Upvotes: 4

Related Questions