I'm trying to make a ClojureScript website using re-frame, reagent, and bootstrap. I'm trying to get subscriptions to work globally instead of locally. Here's the test page:
Why does the dispatch :set-file-to-upload work when it's not specifically defined as a namespace? It's not in the test-page namespace.
Assistant
In Clojure and ClojureScript, symbols (like keywords and function names) can be either fully qualified (including the namespace) or not, depending on how they are used and defined. When you see a symbol being used without an explicit namespace, it's often because it's either:
User
Then how can I set that to work with the img and items subscriptions?
Assistant
To ensure that your and subscriptions work across your application in a similar, streamlined manner—as seen with the dispatch—you should follow a structured process. This strategy involves defining your subscriptions globally, in a central place, much like you did with event handlers. This way, no matter where in your application you are, as long as you've correctly set up and referred to these subscriptions, they'll be accessible.
The test_page.cljs is in a "pages" subfolder. What do I need to do in order to remove the duplicate img and items subscription code from the test page and have it work for all pages?
To eliminate the duplication of subscription code for ::img and ::items across different pages and have a global working solution, you should define these subscriptions once, at a global level, rather than repeating them in each page file. From what you've shared, it seems you're already on the right track since you defined ::img and ::items subscriptions in your jimmystore.events namespace, which appears to serve as a global file for event and subscription definitions.
However, to ensure these subscriptions are accessible and used across different pages, follow these steps:
1. Ensure Global Access to Subscriptions
Make sure that your jimmystore.events namespace (or whichever namespace contains the global subscriptions and events) is required wherever you need to use these subscriptions. This step ensures that subscriptions like ::img and ::items are defined once but can be used wherever needed.
2. Remove Local Definitions
Since you've mentioned that you have definitions for ::img and ::items subscriptions in both the page file (test-page.cljs) and in the events file, you should remove these definitions from the page file to avoid duplication. You already made a correct step by having these subscriptions in your events.cljs, which implies they are defined globally.
3. Use Subscriptions in Pages
When you need to use the ::img or ::items subscriptions in any page, ensure that you require the appropriate namespace where these subscriptions are defined. For example, in your test-page.cljs, you would have:
(ns jimmystore.pages.test-page
(:require
...
[jimmystore.events :as events])) ;; assuming your subscriptions are defined here
And when subscribing to either ::img or ::items, do it as you normally would:
Your global events and subscriptions namespace is loaded when your application starts. This can usually be ensured by having a core or app namespace that requires all necessary namespaces upon initialization.
Don't forget to use namespaced keywords when dealing with global events and subscriptions to avoid clashes and ensure clarity.
Through these steps, you will have successfully refactored your code to have ::img and ::items subscriptions work globally across all pages, eliminating any need for duplicate definitions. This also adheres to DRY (Don't Repeat Yourself) principles and makes your codebase easier to maintain and extend.
Defined in the same namespace where it is being used.
Imported explicitly into the current namespace from another namespace.
Referenced in a way that relies on Clojure(Script)'s resolution of unqualified symbols.
Considering your case with :set-file-to-upload, while the dispatch keyword itself doesn't show the namespace explicitly in the usage you showed, there are a few possibilities for why it works:
Implicit Namespace Resolution: When you use keywords (like :set-file-to-upload) in functions like rf/dispatch, ClojureScript attempts to resolve these references within the current namespace first. If it doesn’t find a match, it will rely on any :require statements that might have included :refer or :as aliases to resolve these symbols. If :set-file-to-upload was defined in another namespace that's been required in your current namespace (with or without an alias), it will be accessible.
Global Registration with re-frame: With re-frame, when you register an event handler using re-frame.core/reg-event-db or other registration functions, you're registering it within a global registry that doesn't care about your namespace structure directly. What matters is the uniqueness of the event key (in this case, the keyword :set-file-to-upload). After registration, this event can be dispatched from anywhere within your ClojureScript application, as long as the event key is known and accessible.
Given this, your :set-file-to-upload dispatch works not because the keyword is namespaced or not, but because of how re-frame manages its global registry of event handlers and subscriptions. As long as you've registered an event handler for :set-file-to-upload somewhere in your application that gets required and loaded at runtime, you can dispatch this event from any part of your application without needing to explicitly qualify it with a namespace. This is a powerful feature of re-frame that simplifies event management across different components or modules of your application.
::img
::items
:set-file-to-upload
1. Define Subscriptions Globally
Start by defining your subscriptions (::img, ::items) globally, similar to how you register events with re-frame. Typically, you'd do this in a core or common namespace that's meant for global definitions. For example:
(ns jimmystore.subs
(:require
[re-frame.core :as rf]))
;; Assuming ::items and ::img are namespaced keywords that you've already defined
(rf/reg-sub
:jimmystore/items
(fn [db _]
(:items db)))
(rf/reg-sub
:jimmystore/img
(fn [db _]
(:img db)))
2. Use These Subscriptions Anywhere
Once defined, you can use these subscriptions in any component or part of your application, similar to how you dispatch events. First, make sure to require your subs namespace where you're managing subscriptions.
(ns jimmystore.pages.some-page
(:require
[re-frame.core :as rf]
[jimmystore.subs :as subs])) ;; Assuming this is where your global subs are defined
Then, subscribe in your components using the namespaced keyword:
(defn some-component []
(let [items (rf/subscribe [:jimmystore/items]) ;; Now using the namespaced keyword
img (rf/subscribe [:jimmystore/img])]
;; component definition where you use items and img
))
3. Consistent, Global Access Pattern
This approach ensures that your subscriptions work seamlessly across your application, mirroring the global access pattern you've seen with event dispatches.
Remember, the key to re-frame's power in structuring ClojureScript applications is its global event handler and subscription registry. Once events and subscriptions are registered, they can be accessed and used from anywhere in your application, given the correct namespace qualification where necessary. This decouples component definitions from the specifics of state management, leading to a more modular and maintainable codebase.
By following these steps, you can eliminate redundancy and ensure that your ::img and ::items subscriptions, like your event dispatches, maintain global accessibility and consistency throughout your application.