This commit proves the technique of invoking a CLJS function from JavaScript,

and consequently from an onclick attribute. This solves the problem of
apparently not being able to set up dommy listeners on dynamically generated
objects.
This commit is contained in:
simon 2016-03-04 18:17:47 +00:00
parent 3dbda68cd7
commit e75a2bccf4

View file

@ -78,7 +78,12 @@
;; Rules page ;; Rules page
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn ^:export rule-ok-click-handler
"Handle the click action on the rule `ok` button with this `index`."
[index]
(let [rule-input (sel1 (keyword (str "#rule-input-" index)))
rule-text (if rule-input (dommy/attr rule-input :value) "Rule input not found")]
(.log js/console (str "rule-ok-click-handler called with index " index ": " rule-text))))
(deftemplate rule-editor (deftemplate rule-editor
;; "Constructs an editor for this `rule` with this `index` ;; "Constructs an editor for this `rule` with this `index`
@ -87,24 +92,14 @@
{:id (str "rule-editor-" index) :class "rule-editor"} {:id (str "rule-editor-" index) :class "rule-editor"}
[:input {:type "text" :id (str "rule-input-" index) :class "rule-input" :value rule}] [:input {:type "text" :id (str "rule-input-" index) :class "rule-input" :value rule}]
[:div {:id (str "rule-controls-" index) :class "rule-controls"} [:div {:id (str "rule-controls-" index) :class "rule-controls"}
[:input {:type "button" :id (str "rule-ok-" index) :class "rule-ok" :value "ok"}] ;; ✔ [:input {:type "button" :id (str "rule-ok-" index) :class "rule-ok" :value "ok"
:onclick (str "mw3.core.rule_ok_click_handler(" index ")")}] ;; ✔
[:input {:type "button" :id (str "rule-up-" index) :class "rule-up" :value "up"}] ;; ↑ [:input {:type "button" :id (str "rule-up-" index) :class "rule-up" :value "up"}] ;; ↑
[:input {:type "button" :id (str "rule-down-" index) :class "rule-down" :value "down"}] ;; ↓ [:input {:type "button" :id (str "rule-down-" index) :class "rule-down" :value "down"}] ;; ↓
[:input {:type "button" :id (str "rule-delete-" index) :class "rule-delete" :value "delete"}]] ;; ✘ [:input {:type "button" :id (str "rule-delete-" index) :class "rule-delete" :value "delete"}]] ;; ✘
[:pre {:id (str "rule-feedback-" index) :class "rule-feedback"}] [:pre {:id (str "rule-feedback-" index) :class "rule-feedback"}]
]) ])
;; (deftemplate rule-editors
;; ;; Constructs, as a `div`, a set of rule editors for the rules in the ruleset with
;; ;; this `ruleset-name`.
;; [ruleset-name]
;; [:div
;; (vec
;; (map
;; #(rule-editor % %)
;; (rulesets/rulesets ruleset-name)
;; (range)))])
(defn load-ruleset (defn load-ruleset
"Loads the ruleset with the specified `name` into a set of rule editors" "Loads the ruleset with the specified `name` into a set of rule editors"
[name] [name]