在不同的html上克隆OM目标元素



所以我开始学习clojudescript,并查看了不同的教程。有一件事我没能找到,那就是在某个html文件上定位一个元素id来进行标记。

假设我有两个html文件,index.html和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但我不确定这是否是一个更好的方法,因为url必须有一个hashkey(http://localhost:3449/#/about)以触发。

更新:

所以我使用了下面的答案,它确实有效,但在开始工作之前我遇到了一些问题。在任何情况下,有人遇到这篇文章,并使用了下面的答案,但得到了一个未定义的问题,请检查我的最终代码。

project.clj:cljsbuild部分

: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}}]}

about.html上包含的js文件

<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>

您需要将javascript文件添加到要使用它的html文件中。因此,如果您有两个不同的html文件index and about,您将需要两个不同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>

在index.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>

最新更新