Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a clean way to add functions to a dynamically created namespace?
    text
    copied!<p>I am creating a noir webapp, and I need to dynamically create new views and models. I've been following the noir examples, in which the view and the controller for a resource have separate namespaces, and I've found it to be a very clean approach.</p> <p>In keeping with this, I need to be able to dynamically create new namespaces corresponding to views and models, and then intern the appropriate functions in them. My idea was to have macros specified in a separate namespace which, when called in the new namespace, would provide the appropriate routes/partials/whatever. </p> <p>For example (forgive my first defmacro):</p> <pre><code>(ns project.views.proto (:use noir.core hiccup.core hiccup.element hiccup.form)) (defmacro def-all-page [path] `(defpage ~path [] (html [:h1 "Ya'll here"]))) </code></pre> <p>is called from...</p> <pre><code>(ns project.proto (:use [clojure.contrib.with-ns :only [with-ns]]) (create-ns 'foo) (intern 'foo 'path "path") ; In reality, the path is dynamic which is why I intern it (with-ns 'foo (clojure.core/refer-clojure) (use 'noir.core 'hiccup.core 'hiccup.element '[project.views.proto :only [def-all-page]]) (def-all-page path) </code></pre> <p>However, calling this from within my new namespace gives me a NullPointerException. I'd greatly appreciate any help, and whether or not there is a better approach. Like, just using refer for a namespace which contains all the necessary definitions?</p> <p>First post, and I don't think it's a repeat of <a href="https://stackoverflow.com/questions/8371850/variable-passed-to-macro-gets-resolved-in-wrong-namespace">this</a>. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload