       (event/listen! (css/sel new-item "button[rel=decrement]") :click #(quantity-changed :dec true (event/target %)))
       (event/listen-once! (css/sel new-item "button[rel=complete]") :click #(completed-item (event/target %))))))
 
+(defn load-basket []
+  (when-let [basket-id (basket-id)]
+    (srm/rpc
+     (api/load-basket basket-id) [basket]
+     :on-success (do
+                   (d/destroy-children! items-list)
+                   (doseq [item (:items basket)]
+                     (add-item-to-list item)))
+     :on-error (js/alert (str "Error loading basket: " basket-id)))))
+
+
+(defn nav-handler [{:keys [token navigation?]}]
+  (when navigation?
+    (load-basket)))
+
+(def session-history (history/history nav-handler))
+
+(defn save-basket []
+    (srm/rpc
+     (api/save-basket (serialise-basket)) [response]
+     :on-success (do (js/alert (str "Basket saved! Id: " (:id response)))
+                     (history/set-token (str (:id response)))
+                     (set-basket-saved!)
+                     (load-user-baskets))
+     :on-error (js/alert (str "Error saving basket"))))
+
+
+(def item-added (pubsub/publishize
+                 (fn [] {:item-name (d/value item-name-field)
+                         :qty (to-int (d/value item-qty-field))}) bus))
+
+(defn on-enter [e f]
+  (when (= 13 (:keyCode e)) (event/prevent-default e) (f)))
+
+(defn on-plus [e f]
+  (when (= 43 (:charCode e)) (event/prevent-default e) (f)))
+
+(defn on-minus [e f]
+  (when (= 45 (:charCode e)) (event/prevent-default e) (f)))
+
+(event/listen! item-name-field :keypress
+               #(on-plus % (partial quantity-changed :inc false item-name-field)))
+
+(event/listen! item-name-field :keypress
+               #(on-minus % (partial quantity-changed :dec false item-name-field)))
+
+(event/listen! add-item-button :click #(item-added))
+(event/listen! item-name-field :keypress #(on-enter % item-added))
+(event/listen! item-qty-field :keypress #(on-enter % item-added))
+(event/listen! save-basket-button :click #(save-basket))
+
 (defn item-completed [{:keys [item]}]
   (d/add-class! item "btn-success")
   (d/append! completed-items-list (d/detach! (x/xpath item "../.."))))

                          :item e}) bus))

(def completed-item (pubsub/publishize (fn [e] {:item e}) bus))

(defn on-enter [e f]
  (when (= 13 (:keyCode e)) (event/prevent-default e) (f)))

(defn on-plus [e f]
  (when (= 43 (:charCode e)) (event/prevent-default e) (f)))

(defn on-minus [e f]
  (when (= 45 (:charCode e)) (event/prevent-default e) (f)))

(event/listen! item-name-field :keypress
               #(on-plus % (partial quantity-changed :inc false item-name-field)))

(event/listen! item-name-field :keypress
               #(on-minus % (partial quantity-changed :dec false item-name-field)))

(event/listen! add-item-button :click #(item-added))
(event/listen! item-name-field :keypress #(on-enter % item-added))
(event/listen! item-qty-field :keypress #(on-enter % item-added))
(event/listen! save-basket-button :click #(save-basket))


(defn add-item-to-list [item]
  (when (and (< 0 (:qty item)) (not-empty (:item-name item)))

(defn log-to-console [o]
  (js/console.log "event: " o))

(defn display-user-details []
  (srm/rpc
     (api/user-details) [details]
