further work on presentations - transformers

This commit is contained in:
David Botton 2022-02-14 18:24:19 -05:00
parent fa80acb1a1
commit ddcb66235d
2 changed files with 19 additions and 8 deletions

View file

@ -34,21 +34,25 @@
;; link-slot-to-form-element ;; ;; link-slot-to-form-element ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro link-slot-to-form-element (object accessor clog-obj) (defmacro link-slot-to-form-element (object accessor clog-obj &key transform)
"Link changes to lisp (ACCESSOR OBJECT) to (value CLOG-OBJ). Only one "Link changes to lisp (ACCESSOR OBJECT) to (value CLOG-OBJ). Only one
element can be bound at a time to a list object." element can be bound at a time to a list object."
`(defmethod (setf ,accessor) :after (new-value (obj (eql ,object))) `(defmethod (setf ,accessor) :after (new-value (obj (eql ,object)))
(setf (value ,clog-obj) new-value))) (setf (value ,clog-obj) (if ,transform
(funcall ,transform new-value)
new-value))))
;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;
;; link-slot-to-element ;; ;; link-slot-to-element ;;
;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro link-slot-to-element (object accessor clog-obj) (defmacro link-slot-to-element (object accessor clog-obj &key transform)
"Link changes to lisp (ACCESSOR OBJECT) to (text CLOG-OBJ). Only one "Link changes to lisp (ACCESSOR OBJECT) to (text CLOG-OBJ). Only one
element can be bound at a time to a list object." element can be bound at a time to a list object."
`(defmethod (setf ,accessor) :after (new-value (obj (eql ,object))) `(defmethod (setf ,accessor) :after (new-value (obj (eql ,object)))
(setf (text ,clog-obj) new-value))) (setf (text ,clog-obj) (if ,transform
(funcall ,transform new-value)
new-value))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; link-form-element-to-object ;; ;; link-form-element-to-object ;;

View file

@ -24,11 +24,18 @@
(i3 (create-form-element body :text (i3 (create-form-element body :text
:label (create-label body :content "Change my-count:")))) :label (create-label body :content "Change my-count:"))))
;; We set up direct relationships between lisp obj and clog objects ;; We set up direct relationships between lisp obj and clog objects
(link-form-element-to-slot i1 lisp-obj my-slot) ;; any change to i1 will change my-slot ;; any change to i1 will change my-slot
(link-slot-to-form-element lisp-obj my-slot i1) ;; any change to my-slot will change i1 (link-form-element-to-slot i1 lisp-obj my-slot)
(link-slot-to-element lisp-obj my-count t1) ;; any change to my-count will change h1 ;; any change to my-slot will change i1 after transforming
;; my-slot to upercase
(link-slot-to-form-element lisp-obj my-slot i1
:transform #'string-upcase)
;; any change to my-count will change t1
(link-slot-to-element lisp-obj my-count t1)
;; any change to i3 will change my-count
;; and i3's value will be transformed to an integer
(link-form-element-to-object (link-form-element-to-object
i3 (my-count lisp-obj) :transform #'parse-integer) ;; any change to i3 will change my-count i3 (my-count lisp-obj) :transform #'parse-integer)
;; This change of my-slot will immediately change in the web page ;; This change of my-slot will immediately change in the web page
(setf (my-slot lisp-obj) "First Value") (setf (my-slot lisp-obj) "First Value")
(set-on-click b1 (set-on-click b1