Permalink
Browse files

add map-bound, fix small bug in subatom pathing and issue with vector…

…s > 32 elements

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information...
1 parent a08ef99 commit df669304b9f8ed2ffa000538380585bcfeba3c68 @ibdknox committed Sep 7, 2012
Showing with 33 additions and 15 deletions.
  1. +28 −12 src/crate/binding.cljs
  2. +5 −3 src/crate/compiler.cljs
View
@@ -1,5 +1,4 @@
-(ns crate.binding
- (:require [clojure.set :as set]))
+(ns crate.binding (:require [clojure.set :as set]))
;;*********************************************************
;; subatom
@@ -21,20 +20,21 @@
(doseq [[key f] watches]
(f key this oldval newval)))
(-add-watch [this key f]
- (set! (.-watches this) (assoc watches key f)))
+ (when f
+ (set! (.-watches this) (assoc watches key f))))
(-remove-watch [this key]
(set! (.-watches this) (dissoc watches key)))
IHash
(-hash [this] (goog.getUid this)))
(defn subatom
- ([atm path]
+ ([atm path]
(let [path (if (coll? path)
path
[path])
[atm path] (if (instance? SubAtom atm)
- [(.-atm atm) (concat (.-path atm) path)]
+ [(.-atm atm) (concat (.-path atm) path)]
[atm path])
k (gensym "subatom")
sa (SubAtom. atm path (hash (get-in @atm path)) nil)]
@@ -105,8 +105,8 @@
bindable
(-value [this] (map :elem (vals (.-stuff this))))
(-on-change [this func]
- (add-watch notif
- (gensym "bound-coll")
+ (add-watch notif
+ (gensym "bound-coll")
(fn [_ _ _ [event el v]]
(func event el v)))))
@@ -142,10 +142,10 @@
(let [prev (.-stuff bc)
pset (into #{} (keys prev))
nset (->keyed neue (opt bc :keyfn))
- added (set/difference nset pset)
- removed (set/difference pset nset)]
+ added (into (sorted-set) (set/difference nset pset))
+ removed (into (sorted-set) (set/difference pset nset))]
(doseq [a added]
- (bc-add bc (->path bc a) a))
+ (bc-add bc a a))
(doseq [r removed]
(bc-remove bc r))))
@@ -160,12 +160,25 @@
opts (if-not (:keyfn opts)
(assoc opts :keyfn first)
(assoc opts :keyfn (comp (:keyfn opts) second)))
- bc (bound-collection. atm (notifier. nil) opts {})]
+ bc (bound-collection. atm (notifier. nil) opts (sorted-map))]
(add-watch atm (gensym "bound-coll") (fn [_ _ _ neue]
(bc-compare bc neue)))
- (bc-compare bc @atm keyfn)
+ (bc-compare bc @atm (:keyfn opts))
bc))
+(defn map-bound [as atm & [opts]]
+ (let [opts (assoc opts :as as)
+ atm (if-not (:path opts)
+ atm
+ (subatom atm (:path opts)))
+ opts (if-not (:keyfn opts)
+ (assoc opts :keyfn first)
+ (assoc opts :keyfn (comp (:keyfn opts) second)))
+ bc (bound-collection. atm (notifier. nil) opts (sorted-map))]
+ (add-watch atm (gensym "bound-coll") (fn [_ _ _ neue]
+ (bc-compare bc neue)))
+ (bc-compare bc @atm (:keyfn opts))
+ bc))
(defn binding? [b]
(satisfies? bindable b))
@@ -176,6 +189,9 @@
(defn value [b]
(-value b))
+(defn index [sub-atom]
+ (last (.-path sub-atom)))
+
(defn on-change [b func]
(-on-change b func))
View
@@ -36,7 +36,9 @@
(defmethod dom-binding :style [_ [k b] elem]
(bind/on-change b (fn [v]
- (dom-style elem k v))))
+ (if k
+ (dom-style elem k v)
+ (dom-style elem v)))))
(defn dom-add [bc parent elem v]
(if-let [adder (bind/opt bc :add)]
@@ -69,8 +71,8 @@
(map? v) (doseq [[k v] v]
(dom-style elem k v))
(bind/binding? v) (do
- (capture-binding :attr [:style v])
- (dom-style (bind/value v))))
+ (capture-binding :style [nil v])
+ (dom-style elem (bind/value v))))
elem)
([elem k v]
(let [v (if (bind/binding? v)

0 comments on commit df66930

Please sign in to comment.