Coder Social home page Coder Social logo

Comments (19)

swannodette avatar swannodette commented on August 16, 2024

Please create a minimal example that only uses Om so we can minimize the variables. Thanks.

from om.

weihsiu avatar weihsiu commented on August 16, 2024

per request

(ns om-sandbox.test-list
  (:require [om.core :as om :include-macros true]
            [om.dom :as dom :include-macros true]))

(enable-console-print!)

(defn rand-value []
  (let [possible-values "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"]
    (rand-nth possible-values)))

(defn gen-list [n]
  (vec (repeatedly n rand-value)))

(defn test-list-item [item owner]
  (om/component
   (dom/li nil item)))

(defn test-list [items owner]
  (reify
    om/IWillMount
    (will-mount [_]
                (js/setInterval
                 #(om/transact! items [0] (constantly (rand-value)))
                 1000))
    om/IRender
    (render [_]
            (dom/div nil
             (dom/span nil (get items 0))
             (dom/ul nil
              (om/build-all test-list-item items))))))

(def items (gen-list 5))

(om/root items test-list (.getElementById js/document "content"))

from om.

swannodette avatar swannodette commented on August 16, 2024

The problem is that you want to build a component from a non-cursor value. This is no longer allowed and I've exposed knobs to make it possible to wrap simple values in cursors, 5498b64

from om.

swannodette avatar swannodette commented on August 16, 2024

A working version of your program based on master:

(deftype DefaultCursor [value state path]
  om/ICursor
  (-path [_] path)
  om/IValueCursor
  (-value [_] value))

(extend-type default
  om/IToCursor
  (-to-cursor
    ([n state] (DefaultCursor. n state []))
    ([n state path]
      (DefaultCursor. n state path))))

(defn rand-value []
  (let [possible-values "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"]
    (rand-nth possible-values)))

(defn gen-list [n]
  (vec (repeatedly n rand-value)))

(defn test-list-item [item owner opts]
  (reify
    om/IRender
    (render [_]
      (dom/li nil (om/value item)))))

(defn test-list [items owner]
  (reify
    om/IWillMount
    (will-mount [_]
      (js/setInterval
        #(om/transact! items [0] (constantly (rand-value)))
        1000))
    om/IRender
    (render [_]
      (dom/div nil
        (dom/span nil (om/value (get items 0)))
        (dom/ul nil
          (om/build-all test-list-item items))))))

(def items (gen-list 5))

(om/root items test-list (.getElementById js/document "app"))

from om.

swannodette avatar swannodette commented on August 16, 2024

On second thought I'm not really all that happy with my solution suggested above, as it then means you have to use om/value everywhere. I see two options at the moment, automatically coerce, or supply two new explicit fns something like build-value and build-values. Better ideas welcome.

from om.

thheller avatar thheller commented on August 16, 2024

Maybe make build/build-all wrap the value in something atom'ish, basically a wrapper around the root state + a path. build may then derive child paths and transact! takes on swap! like semantics (aka no need to provide paths)

so (dom/span nil (om/value (get items 0))) turns into (dom/span nil (get @items 0))

(om/swap! items update-in [0] (rand-value))

from om.

swannodette avatar swannodette commented on August 16, 2024

I have a real solution coming that leverages the new specify feature.

from om.

swannodette avatar swannodette commented on August 16, 2024

fixed in master, bc81082

You just need to extend string to ICloneable. Then we can make the JS value become a cursor via specify.

(extend-type string
  ICloneable
  (-clone [n] (js/String. n)))

(defn rand-value []
  (let [possible-values "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"]
    (rand-nth possible-values)))

(defn gen-list [n]
  (vec (repeatedly n rand-value)))

(defn test-list-item [item owner opts]
  (reify
    om/IRender
    (render [_]
      (dom/li nil item))))

(defn test-list [items owner]
  (reify
    om/IWillMount
    (will-mount [_]
      (js/setInterval
        #(om/transact! items [0] (constantly (rand-value)))
        1000))
    om/IRender
    (render [_]
      (dom/div nil
        (dom/span nil (get items 0))
        (dom/ul nil
          (om/build-all test-list-item items))))))

(def items (gen-list 5))

(om/root items test-list (.getElementById js/document "app"))

from om.

weihsiu avatar weihsiu commented on August 16, 2024

nice! thanks for the prompt response and a cool solution.

On Fri, Jan 3, 2014 at 5:58 AM, David Nolen [email protected]:

Closed #36 #36.


Reply to this email directly or view it on GitHubhttps://github.com//issues/36
.

.......__o
.......<,
....( )/ ( )...

from om.

aluuu avatar aluuu commented on August 16, 2024

There is a problem in last example. If you update not only first item in items, then calling of om/build-all will not update <li>'s.

from om.

swannodette avatar swannodette commented on August 16, 2024

@aluuu can you give the full source that does not work for you. Thanks!

from om.

aluuu avatar aluuu commented on August 16, 2024

There it is. Only transact! call changed.

(extend-type string
  ICloneable
  (-clone [n] (js/String. n)))

(defn rand-value []
  (let [possible-values "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"]
    (rand-nth possible-values)))

(defn gen-list [n]
  (vec (repeatedly n rand-value)))

(defn test-list-item [item owner opts]
  (reify
    om/IRender
    (render [_]
      (dom/li nil item))))

(defn test-list [items owner]
  (reify
    om/IWillMount
    (will-mount [_]
      (js/setInterval
        #(om/transact! items (constantly (gen-list 5)))
        1000))
    om/IRender
    (render [_]
      (dom/div nil
        (dom/span nil (get items 0))
        (dom/ul nil
          (om/build-all test-list-item items))))))

(def items (gen-list 5))

(om/root items test-list (.getElementById js/document "app"))

from om.

swannodette avatar swannodette commented on August 16, 2024

Thanks looking into it.

from om.

swannodette avatar swannodette commented on August 16, 2024

This works fine for me using master. What version of Om are you using?

from om.

aluuu avatar aluuu commented on August 16, 2024

I'm in master too.
Just tried to execute git pull && lein install in local om repository and got the same.
In dependencies I have [om "0.1.4"]. I'll try to investigate what's wrong and I'll tell you.

from om.

swannodette avatar swannodette commented on August 16, 2024

You need to run lein cljsbuild clean if you switch Om versions.

from om.

aluuu avatar aluuu commented on August 16, 2024

Oh, wow. That works. Thanks!

from om.

aluuu avatar aluuu commented on August 16, 2024

And also one change. I updated my dependencies to get it work:

[org.clojure/clojurescript "0.0-2138" :scope "provided"]
[org.clojure/core.async "0.1.267.0-0d7780-alpha" :scope "provided"]

from om.

swannodette avatar swannodette commented on August 16, 2024

yes the README documents what your project.clj should look like.

from om.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.