[cells-devel] Connecting two models

Frank Goenninger frgo at me.com
Wed Oct 3 08:59:59 UTC 2012


Hi Mark,

how about:


;;; ------------------------
;;; ***  Model MY-MODEL  ***
;;; ------------------------
  
(defmd my-model ()
  item-index
  :item-index (c-in 0))

(defobserver item-index ((self my-model))
  (when new-value
    (format *debug-io* "~%~S: New value for slot item-index => ~S."
      self (item-index self))))

(defmacro mk-model (id)
  `(make-instance 'my-model
     :fm-parent *parent*
     :md-name ,id))

;;; -----------------------
;;; ***  Model MY-VIEW  ***
;;; -----------------------

(defmd my-view ()
  item-index)
  
(defmacro mk-view (id model-id)
  `(make-instance 'my-view
     :fm-parent *parent*
     :item-index (c? (let ((model (fm^ ,model-id)))   ;; -> fm^ searches for model in the current family
           (item-index model)))
     :md-name ,id))

(defobserver item-index ((self my-view))
  (when new-value
    (format *debug-io* "~%~S: New value for slot item-index => ~S."
      self (item-index self))))

;;; ---------------------------
;;; ***  Family CONTROLLER  ***
;;; ---------------------------

(defmd my-controller (family)
  )

(defmacro mk-controller (id view-id model-id)
  `(make-instance 'my-controller
     :kids (c? (the-kids
                (mk-model ,model-id)
                (mk-view ,view-id ,model-id)))
     :md-name ,id))

;;; -----------------
;;; ***  TESTING  ***
;;; -----------------

(defun controller-test ()
  
  (let* ((self  (mk-controller :controller :view :model))
         (model (fm-find-kid self :model))
         (view  (fm-find-kid self :view)))
    
    (setf (item-index model) 1)
  
  (values)))

Then:

CL-USER > (controller-test)

MODEL: New value for slot item-index => 0.
VIEW: New value for slot item-index => 0.
MODEL: New value for slot item-index => 1.
VIEW: New value for slot item-index => 1.

?

Cheers
   Frank


Am 03.10.2012 um 03:47 schrieb Mark Cox:

> G'day,
> 
> I think my mental model of Cells is wrong. Perhaps someone can help.
> 
> I have two model classes:
> 
> (defmodel model ()
>  ((item-index
>    :initarg :item-index
>    :accessor item-index))
>  (:default-initargs
>   :item-index (c-in 0)))
> 
> (defmodel view ()
>  ((item-index
>    :initarg :item-index
>    :accessor item-index))
>  (:default-initargs
>   :item-index (c-in 0)))
> 
> What I would like to do is to connect the item-index slot of an
> instance of MODEL to the item-index in an instance of VIEW. i.e.
> 
> (defun connect-model-and-view (model view)
>  (setf (item-index view) (c? (item-index model))))
> 
> which does not appear to be possible.
> 
> I have navigated around the issue by introducing a third model class,
> CONTROLLER, that has an item-index slot initialised with (c?
> (item-index (^model))) and an observer that updates the view. However, I
> am wondering if this is the correct way as I seem to require inserting
> WITH-INTEGRITY in the observer.
> 
> Thanks
> Mark
> 
> _______________________________________________
> cells-devel site list
> cells-devel at common-lisp.net
> http://common-lisp.net/mailman/listinfo/cells-devel





More information about the cells-devel mailing list