Skip to content

Instantly share code, notes, and snippets.

@aaronc
Last active August 29, 2015 14:11
Show Gist options
  • Save aaronc/5d497aa61e27ce924178 to your computer and use it in GitHub Desktop.
Save aaronc/5d497aa61e27ce924178 to your computer and use it in GitHub Desktop.
freactive items-view ideas

items-view will be a function taking a map of arguments:

(items-view
  {:items items
   :container [:ul]
   :template (fn [item] [:li @item])})

We can do sorting like this:

(def items (observable-vector [{:a 0 :b 2}]))
(items-view
  {:items items
   :container [:ul]
   :template (fn [item] [:li (rx (:a @item))])
   :sort-by sort-by-atom})

where sort-by-atom is some reactive data source (that responds to a user clicking something like column headings for instance) which contains a function that could be passed as the comparator argument to sorted-map-by.

I would propose support for the following keys:

Key Description
:items an instance of IObservableCollection (required) (not bindable)
:template a function that will be used to create the item view for each item should take a single item for the cursor to the item (required) (not bindable)
:container the container in which to bind the items as children (defaults to [:span]) (not bindable but can contain attribute bindings)
:sort-by a comparator as would be passed to sorted-map-by (defaults to nil) (bindable)
:sort-order :asc or :desc (defaults to :asc) (bindable)
:filter a predicate as would be passed to filter (bindable)
:lower-index specifies the lowest index (in sort order) to be displayed (using data.avl's nth) (bindable)
:upper-index specifies the highest index (in sort order) to be displayed (using data.avl's nth) (bindable)
:lower-test a vector [test key] where test is > or >= as would be passed to subkey for efficiently setting a lower bound (using data.avl) (bindable)
:upper-test a vector [test key] where test is < or <= as would be passed to subkey for efficiently setting an upper bound (using data.avl) (bindable)
:header a vector of elements to be inserted as the first children of the container before dynamic items (not bindable but can contain bindings)
:footer a vector of elements to be inserted as the last children of the container after dynamic items (not bindable but can contain bindings)
:placeholder a single element or nil that can be inserted as a place holder (for things like sorting and drag and drop) when :placeholder-idx is not nil (bindable)
:placeholder-idx the index where :placeholder should be inserted or nil (bindable)

Sorting will be provided by data.avl and will need to be explicitly enabled like a plugin (to reduce code size when not needed). Alternatively there could be a simple items view which doesn't support sorting and a more advanced version which does.

An items-view could also potentially work as a plugin to freactive's normal syntax:

[:freactive/items-view
  {:items items
   :container [:ul]
   :template (fn [item] [:li @item])}]
@aaronc
Copy link
Author

aaronc commented Dec 18, 2014

Here's the proposal for IObservableCollection and the default implementations observable-map and observable-vector for discussion: https://gist.github.com/aaronc/0654151190b9145dd473

@aaronc
Copy link
Author

aaronc commented Dec 18, 2014

PLEASE post comments here: aaronc/freactive#23

Gist does not support notifications on comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment