2015-04-24 47 views
3

所以我開始學習clojurescript,我正在檢查它的不同教程。我無法發現的一件事是在某個html文件上定位一個元素id來放置我的標記。Clojurescript OM定位元素在不同的html上

比方說,我有兩個html文件,index.html和about.html。我想針對下面的元素ID「應用程序」我的代碼上about.html當URL指向http://localhost:3449/about

代碼:

(om/root 
    (fn [data owner] 
    (reify 
     om/IRender 
     (render [_] 
     (dom/p nil (:text data))))) 
    app-state 
    {:target (. js/document (getElementById "app"))}) 

什麼是做到這一點的最好辦法?或者可能是一個參考,所以我可以看看它。或者也許我在這裏錯過了一些觀點,也許有人可以啓發我。

此外,我已經嘗試使用此https://github.com/gf3/secretary,但我不確定是否更好的方法,因爲網址必須有一個hashkey(http://localhost:3449/#/about)才能觸發。

更新:

所以我用下面的答案和它的工作,但是我做它的工作之前,面臨着一些問題。在任何情況下,有人遇到這個帖子,並已使用下面的答案,但得到一個未定義的問題檢查我的最終代碼。您project.clj

:cljsbuild {:builds [{ :id "dev" :source-paths ["src/clj" "src/cljs"] :compiler {:output-to "resources/public/js/main.js" :output-dir "resources/public/js/out/" :optimizations :none :pretty-print true}}]}

:cljsbuild部分列入about.html

<script src="js/out/goog/base.js" type="text/javascript"></script> <script src="js/main.js" type="text/javascript"></script> <script type="text/javascript">goog.require("om_async.about");</script> <script type="text/javascript">om_async.about.init();</script>

回答

2

您需要將JavaScript文件添加到您想要在使用它的HTML文件JS文件。 因此,如果你有兩個不同的html文件索引,你需要兩個不同的cljs文件。

他們都將包含一個方法來初始化JS這樣

(defn ^:export init [] 
    (om/root 
    (fn [data owner] 
     (reify 
     om/IRender 
     (render [_] 
      (dom/p nil (:text data))))) 
    app-state 
    {:target (. js/document (getElementById "app"))})) 

而在你的about.html文件,你會叫這樣的JS:

<script type="text/javascript">your.namespace.about.init();</script> 

和索引.html:

<script type="text/javascript">your.namespace.index.init();</script> 

至少我是這麼做的。我很想知道是否有更優雅的方式來做到這一點。

更新:https://github.com/sveri/siwf/blob/master/src/cljs/de/sveri/siwf/groups.cljs這裏的HTML模板在使用的功能:請在底部導出功能看看這裏https://github.com/sveri/siwf/blob/master/resources/templates/groups.html

這是很難說在去錯了你地方,很可能它是一個命名空間問題。
還請確保在致電之前添加已編譯的js文件:

<script src="/js/app.js"></script> 
<script type="text/javascript">your.namespace.index.init();</script>