about summary refs log blame commit diff stats
path: root/v.el
blob: 7a5eb8b6ba86cf7a0a4e1cbc4ac5e0f27fcc1711 (plain) (tree)
1
2
3
4
5

                                                                                                          

                                    
 






























                                                                                                          
;;; Port of https://stackoverflow.com/questions/3984296/model-view-controller-design-pattern-code-example.
;;; View class.
;;;
;;; Probably better to just use ltk.

(require 'eieio)
(eval-when-compile (require 'cl-lib))
(eval-when-compile (require 'cl-generic))
(require 'jsonrpc)
(defclass v-view ()
  ((button :accessor button)
    (model :accessor model)
    (controller :writer controller)
    ))
(cl-defmethod initialize-instance :after ((self v-view) &rest initargs)
  (oset self button (make-instance (find-class 'button) 'm "Click Me"))
  (oset self model (make-instance (find-class 'v-model-stub)))
  (oset self controller (make-instance (find-class 'v-controller-stub) 'm (model self) 'b (button self))))
(defconst v--port-num 8194)
(defvar v--conn (make-instance 'jsonrpc-process-connection
                    :process (lambda ()
                               (open-network-stream
                                 "*MVC JSON-RPC*" nil
                                 "localhost" v--port-num))))
(defclass v-model-stub () (id :reader id))
(cl-defmethod initialize-instance :after ((self v-model-stub) &rest initargs)
  (oset self id (jsonrpc-request v--conn :create-model)))
(cl-defgeneric get-number (model))
(cl-defmethod get-number ((model v-model-stub))
  (jsonrpc-request v--conn :get-number '(:id (id model))))
(defclass v-controller-stub () (id :reader id))
(cl-defmethod initialize-instance :after ((self v-controller-stub) &rest initargs)
  (oset self id (jsonrpc-request v--conn :create-controller)))
(cl-defgeneric on-button-clicked (controller))
(cl-defmethod on-button-clicked ((controller v-controller-stub))
  (jsonrpc-request v--conn :on-button-clicked '(:id (id controller))))