Thin(ish) wrapper around React
1: (def app-state 2: (atom {:navbar-expanded? false 3: :search-field "example" 4: :user {:id 10 5: :name "Natsume Soseki"}}))
1: (ns om-intro.components 2: (:require [om.core :as om] 3: [om.dom :as dom])) 4: 5: (defn comment-box [app] 6: (om/component 7: (dom/div #js {:className "comment-box"} 8: (dom/h1 nil "Leave a comment") 9: (dom/h3 nil (get-in app [:user :name])) 10: (dom/text {:id "new-comment"})))) 11: 12: (om/root 13: comments-box app-state 14: {:target (sel1 :#comments-container)})
(om/build comments-box app-state)
1: <div id='comments-container'> 2: <div class='comment-box'> 3: <h1>Leave a comment</h1> 4: <h3>Natsume Soseki</h3> 5: <textarea id='new-comment' /> 6: </div> 7: </div>
Mutate the state to trigger a re-render
(swap! app-state assoc-in [:user :name] "Mikhail Bulgakov")
1: <div id='comments-container'> 2: <div class='comment-box'> 3: <h1>Leave a comment</h1> 4: <h3>Mikhail Bulgakov</h3> 5: <textarea id='new-comment' /> 6: </div> 7: </div>
Laziest hard-working library:
Declaratively compose your component, at the right layer of abstraction
Current state-of-the-art:
1: <navbar> 2: <ul class="menu"> 3: <li> 4: <a href="#">Dropdown</a> 5: <ul> 6: <li><a href="#">Some Action 1</a></li> 7: <li><a href="#">Some Action 2</a></li> 8: <li><a href="#">Some Action 3</a></li> 9: <li><a href="#">Some Action 4</a></li> 10: </ul> 11: </li> 12: </ul> 13: </navbar>
1: $(document).ready(function() { 2: $('.menu').dropit(); 3: });
1: (om/navbar 2: (om/build drop-down 3: {:children [{:id :action-1 :title "Some Action 1"} 4: {:id :action-2 :title "Some Action 2"} 5: {:id :action-3 :title "Some Action 3"} 6: {:id :action-4 :title "Some Action 4"}]}))
Example of composable, reusable, components:
1: (om/build draggable-window 2: {:title "Data Inspector" 3: :content-com anhk/inspector 4: :content-data (:user app-state) 5: :content-opts {}})
Components operate on data, so unexpectedly nice use cases come up:
We need a Bootstrap that operates on the component-layer: Table views, drop-downs, media players, sliders.
A client for the Kandan chat app. Source: https://github.com/sgrove/omchaya Demo: http://sgrove.github.io/omchaya/prod.html