Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Om: Add example of a component with local state #25

Open
holyjak opened this issue May 8, 2014 · 5 comments
Open

Om: Add example of a component with local state #25

holyjak opened this issue May 8, 2014 · 5 comments

Comments

@holyjak
Copy link
Contributor

holyjak commented May 8, 2014

The current examples show how to create Om components that depend on the global app state but it is not clear how to create one that uses a local component state. Having an example would be therefore nice.

This is what I do, though I am not sure it is the best way - essentially wrapping a snippet in a stateful component:

(defsnippet search-html "index.html"
  [:#search]
  [{:keys [value]} owner]
  {[:#search-input] (kioo/do->
                          (kioo/set-attr :value value)
                          (kioo/set-attr :onChange #(handle-search % owner)))})

(defn search-input 
  "Stateful wrapper around the search Kioo template"
  [app owner]
    (reify
      om/IInitState
      (init-state [_] {:value nil})
      om/IRenderState
      (render-state [this state] (search-html state owner))))

;;;; Somewhere later, when attaching to a root:
;; (om/build search-input (om/graft {} app))

Thank you!

@ckirkendall
Copy link
Owner

@jakubholynet this would be a great example to add to the om example. @BorisKourt has started a kioo-docs project. I would love if we added this there. Once I get some free time that is where I will be concentrating my document efforts.

@BorisKourt
Copy link

I will add this asap. The example actually just helped me out now, thanks! : )
@ckirkendall you should see some real commits coming in to that docs repo soon.

@holyjak
Copy link
Contributor Author

holyjak commented May 10, 2014

Lovely! Do you need any help from me, @BorisKourt ?

@BorisKourt
Copy link

I just have a request @jakubholynet it appears that om/graft has been depreciated as per omcljs/om#152 (unless I am misunderstanding) could you provide a super bare but fully compilable example? I don't want to write something you didn't intend to demonstrate by accident, still fairly new to the 'om way' haha :)

Thanks!

@holyjak
Copy link
Contributor Author

holyjak commented May 13, 2014

Thanks @BorisKourt, I haven't noticed that. Upgrading from 0.5.3 to 0.6.2 now :-)
The right way to use the component without graft is

(om/build search-input {})

according to Nolen's example - and that is also what works for me with om 0.6.2 and cljs 0.0-2197

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants