0

As a follow-up to Project organization and cljsbuild config to require namespace. If I only have one output file, how do I specify what code to run on each separate web page?

One approach might be to include a script tag <script>myproject.somepage.init();</script> in each page. Is there a generally accepted approach?

Community
  • 1
  • 1
deadghost
  • 5,017
  • 3
  • 34
  • 46
  • 1
    I don't see a more obvious way than that. Nothing wrong with your approach. Make sure you `^:export` your function so it survives advanced compilation. But maybe you want to use modules? – ClojureMostly Aug 13 '15 at 23:44

1 Answers1

2

I think the previous question was interpreted in the context of single page applications where it doesn't make sense to have more than one output file.

If you need different behaviors in separate web pages, I suggest you have an entry namespace for each webpage, using the :main option:

:cljsbuild {:build [{:id "login"
                     :main my-login.entry
                     :output-to "resources/public/js/login.js"
                     :optimizations :advanced}
                    {:id "feed"
                     :main my-feed.entry
                     :output-to "resources/public/js/feed.js"
                     :optimizations :advanced}]}}

And then require those files on each webpage.

This approach is worth the trouble if my-feed.entry and my-login.entry will require different code, and it would be a waste to deliver all the code for feed also to login. If the behavior is almost the same, and is just a matter of an argument, or one function call, your suggestion is fine.

sbensu
  • 1,491
  • 10
  • 9