简体   繁体   English

Om-next远程同步教程send-to-chan

[英]Om-next Remote Sync Tutorial send-to-chan

I am probably doing something wrong but I believe one of the om-next tutorials has some issues; 我可能做错了,但我相信其中一个om-next教程有一些问题; specifically the autocomplete example. 特别是自动完成示例。 I was able to figure out one of the issues but there is another issue that is causing me some problems. 我能够弄清楚其中一个问题,但还有另一个问题导致我遇到一些问题。

Once I put in more then two letters in the input box for autocompletion, the following code: 一旦我在输入框中输入了两个以上的字母以进行自动完成,请输入以下代码:

(defn send-to-chan [c]
  (fn [{:keys [search]} cb]
    (when search
      (let [{[search] :children} (om/query->ast search)
            query (get-in search [:params :query])]
        (put! c [query cb])))))

produces the following error: 产生以下错误:

Uncaught TypeError: Cannot read property 'call' of undefined
core.js?zx=3jufl8vymlgw [452]   om_tutorial.core.send_to_chan
next.js [3034]  om.next.Reconciler.om$next$protocols$IReconciler$send_BANG_$arity$1
protocols.js [303]  om$next$protocols$send_BANG_
next.js [1656]  anonymous

I am not sure why this is the case. 我不确定为什么会这样。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Not sure if this is the right way to do things, but this is what I did to solve this issue. 不确定这是否是正确的做事方式,但这是我为解决这个问题所做的。

  1. Checkout om from github. 从github结帐。 ( https://github.com/omcljs/om ) https://github.com/omcljs/om
  2. cd om cd om
  3. lein install 莱恩安装

Now the latest om is available on your system. 现在您的系统上可以使用最新的om。 (you can not just put it in your project file, because it is not on https://clojars.org/repo/ yet). (你不能只把它放在你的项目文件中,因为它不在https://clojars.org/repo/上 )。

  1. Now this is where I think the confusion happened for me. 现在这就是我认为混乱发生在我身上的地方。 Earlier in the tutorial, before the auto-correction example; 在本教程的前面,在自动更正示例之前; there is a project file that is defined with [org.omcljs/om "1.0.0-alpha23"] . 有一个用[org.omcljs/om "1.0.0-alpha23"]定义的项目文件。 Then when the auto-correction example comes up, I used the same project configurations since there was no mention of how to configure the project file again. 然后,当自动更正示例出现时,我使用了相同的项目配置,因为没有提到如何再次配置项目文件。 It turns out that you have to use [org.omcljs/om "1.0.0-alpha29"] . 事实证明你必须使用[org.omcljs/om "1.0.0-alpha29"]

Once that happens everything works although I get the following warning. 一旦发生这一切,虽然我得到以下警告但一切正常。

Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `om_tutorial$core$AutoCompleter`. See https://fb.me/react-warning-keys for more information.

That will be a fight for another day. 这将是另一天的斗争。

BTW. BTW。 Since I used the older version of om originally, just installing the new one did not solve the problem. 由于我最初使用旧版本的om,只是安装新版本并没有解决问题。 lein clean did not solve the problem either. lein clean也没有解决问题。 I had to manually delete my om-tutorial/resources/public/js folder. 我不得不手动删除我的om-tutorial/resources/public/js文件夹。 Then run lein run -m clojure.main script/figwheel.clj . 然后运行lein run -m clojure.main script/figwheel.clj

The error about each child array requiring a "key" prop has to do with React more than it does Om. 关于需要“关键”道具的每个子阵列的错误与React有关,而不是Om。 React requires every subcomponent to have a unique id. React要求每个子组件都具有唯一的ID。

If you iterate over a factory method it won't generate a new id for each subcomponent automatically. 如果迭代工厂方法,它将不会自动为每个子组件生成新的id。 You have to specify a key function: 您必须指定一个关键功能:

(def app-state 
  (atom {:items [{:id 1
                  :title "Foo"}
                 {:id 2
                  :title "Foo"}]}

(defui Item
 static om/IQuery
 (query [this] [:id :title])
 Object
 (render [this]
   (dom/li nil (:title (om/props this))))

;; Specify key function as follows
(def item (om/factory Item {:keyfn :id})

(defui List
 static om/IQuery
 (query [this] [{:items (om/get-query Item)}])
 Object 
 (render [this]
   (dom/ul nil (map item (:items (om/props this)))))

The key function doesn't have to return a number, but it does have to return some kind of uniquely identifying piece of information for each item being iterated over (which in this case, the title is not). 键函数不必返回一个数字,但它必须为每个被迭代的项返回某种唯一标识信息(在这种情况下,标题不是)。

Incidentally, you could also use map-indexed to generate a number to feed into the key function, or use a random number generator. 顺便提一下,您还可以使用map-indexed生成一个数字以输入关键函数,或使用随机数生成器。

I think the problem is om.next/query->ast isn't defined in 1.0.0-alpha23 -- that was the source of the call on undefined error. 我认为问题是om.next/query->ast未在1.0.0-alpha23中定义 - 这是未定义错误调用的来源。

Here's a hacky work-around: 这是一个hacky work-around:

    (defn send-to-chan [c]
      (fn [{:keys [search] :as x} cb]
        (when search ;; e.g. [(:search/results {:query "xxx"})]
          (let [query (-> search first second :query)]
            (put! c [query cb])))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM