如何通过 Clojure Datascript 填充和显示列表?
How do I fill and display a list by Clojure Datascript?
我已经用这个填满了我的数据库
(def fixtures [
[:db/add 0 :system/group :all]
{
:product/name "Donut Keurig"
:product/category "snack"
:product/brand "Grocery&GourmetFood"
:product/height "2.1"
:product/width "3.2"
:product/notes "The Original Donut Shop Keurig Single-Serve K-Cup Pods, Regular Medium Roast Coffee"
}
{
:product/name "Ferrero Rocher Hazelnut Chocolates"
:product/category "Candy"
:product/brand "Candy&Chocolate"
:product/height "3.4"
:product/width "2"
:product/notes "A tempting combination of smooth chocolaty cream surroiunding a whole hazelnut within a delciate, crisp wafer all enveloped in milk chocolate and finely chopped hazelnuts"
}
])
(def conn (d/transact! conn u/fixtures))
但是告诉我这个错误:
未捕获错误:断言失败:(conn?conn) 在 Function.datascript.core.transact_BANG_.cljs$core$IFn$_invoke$arity$3
此外,我想像这样显示数据库的结果:
(defmulti read om/dispatch)
(defmethod read :product/name
[{:keys [state query]} _ _]
{:value (d/q '[:find [(pull ?e ?selector) ...]
:in $ ?selector
:where [?e :product/name]]
(d/db state) query)})
(defui product-view
static om/IQuery
(query [this] [{:product/name [:db/id :product/name]}])
Object
(render [this]
(let [{:keys [product/name] :as entity}
(get-in (om/props this) [:product/name ""])]
(dom/tr nil
(dom/td nil name)))))
(defui products-view
static om/IQuery
(query [this] [{:product/name [:db/id :product/name]}])
Object
(render [this]
(dom/table #js {:className "table table-bordered"}
(dom/thead nil
(dom/tr #js {:className "row-garden"}
(dom/th nil "Product Name")
(dom/th nil "Category")
(dom/th nil "Brand")
(dom/th nil "Height")
(dom/th nil "Width")
(dom/th nil "Notes")))
(dom/tbody
(let [{:keys [product/name] :as entity}
(get-in (om/props this) [:product/name ""])]
(println conn)
(dom/tr nil
(dom/td nil name)))))))
(om/add-root!
reconciler
products-view
(gdom/getElement "table-products"))
但是不起作用:(
谢谢!
您似乎没有正确初始化 Datascript 连接。请参阅。您的代码应遵循此模型:
(ns clj.core
(:require [tupelo.core :as t]
[datascript.core :as d]
[clojure.set :as set] ))
(t/refer-tupelo)
(def data
[ {:type :x :local/id 1, :obs/A "11", :obs/value 2.0, :obs/color "yellow"}
{:type :x :local/id 2, :obs/A "12", :obs/value 4.0, :obs/color "blue"}
{:type :x :local/id 3, :obs/A "13", :obs/value 3.0, :obs/color "green"}
{:type :x :local/id 3, :obs/A "15", :obs/value 7.0, :obs/color "red"}
{:type :y :local/id 2, :obs/A "11", :obs/value 7.0, :obs/shape "square"}
{:type :y :local/id 2, :obs/A "13", :obs/value 4.0, :obs/shape "circle"}
{:type :y :local/id 6, :obs/A "15", :obs/value 3.0, :obs/shape "triangle"} ] )
(def conn (d/create-conn {}))
(d/transact! conn data)
.....
这里是 DataScript 的作者。
首先,交易! returns交易报告,不是连接。它改变作为参数传入的 conn
的内容:
(def conn (d/create-conn schema))
...
(d/transact! conn u/fixtures)
其次,您不能在查询中使用动态绑定拉模式。使用正常拉取 API:
(let [db (d/db state)
datoms (d/datoms db :aevt :product/name)
eids (map :e datoms)]
(d/pull-many db query eids))
很遗憾,我无法在 Om.Next 部分为您提供帮助。如果有什么错误我就不知道了
希望对您有所帮助。
我已经用这个填满了我的数据库
(def fixtures [
[:db/add 0 :system/group :all]
{
:product/name "Donut Keurig"
:product/category "snack"
:product/brand "Grocery&GourmetFood"
:product/height "2.1"
:product/width "3.2"
:product/notes "The Original Donut Shop Keurig Single-Serve K-Cup Pods, Regular Medium Roast Coffee"
}
{
:product/name "Ferrero Rocher Hazelnut Chocolates"
:product/category "Candy"
:product/brand "Candy&Chocolate"
:product/height "3.4"
:product/width "2"
:product/notes "A tempting combination of smooth chocolaty cream surroiunding a whole hazelnut within a delciate, crisp wafer all enveloped in milk chocolate and finely chopped hazelnuts"
}
])
(def conn (d/transact! conn u/fixtures))
但是告诉我这个错误:
未捕获错误:断言失败:(conn?conn) 在 Function.datascript.core.transact_BANG_.cljs$core$IFn$_invoke$arity$3
此外,我想像这样显示数据库的结果:
(defmulti read om/dispatch)
(defmethod read :product/name
[{:keys [state query]} _ _]
{:value (d/q '[:find [(pull ?e ?selector) ...]
:in $ ?selector
:where [?e :product/name]]
(d/db state) query)})
(defui product-view
static om/IQuery
(query [this] [{:product/name [:db/id :product/name]}])
Object
(render [this]
(let [{:keys [product/name] :as entity}
(get-in (om/props this) [:product/name ""])]
(dom/tr nil
(dom/td nil name)))))
(defui products-view
static om/IQuery
(query [this] [{:product/name [:db/id :product/name]}])
Object
(render [this]
(dom/table #js {:className "table table-bordered"}
(dom/thead nil
(dom/tr #js {:className "row-garden"}
(dom/th nil "Product Name")
(dom/th nil "Category")
(dom/th nil "Brand")
(dom/th nil "Height")
(dom/th nil "Width")
(dom/th nil "Notes")))
(dom/tbody
(let [{:keys [product/name] :as entity}
(get-in (om/props this) [:product/name ""])]
(println conn)
(dom/tr nil
(dom/td nil name)))))))
(om/add-root!
reconciler
products-view
(gdom/getElement "table-products"))
但是不起作用:(
谢谢!
您似乎没有正确初始化 Datascript 连接。请参阅
(ns clj.core
(:require [tupelo.core :as t]
[datascript.core :as d]
[clojure.set :as set] ))
(t/refer-tupelo)
(def data
[ {:type :x :local/id 1, :obs/A "11", :obs/value 2.0, :obs/color "yellow"}
{:type :x :local/id 2, :obs/A "12", :obs/value 4.0, :obs/color "blue"}
{:type :x :local/id 3, :obs/A "13", :obs/value 3.0, :obs/color "green"}
{:type :x :local/id 3, :obs/A "15", :obs/value 7.0, :obs/color "red"}
{:type :y :local/id 2, :obs/A "11", :obs/value 7.0, :obs/shape "square"}
{:type :y :local/id 2, :obs/A "13", :obs/value 4.0, :obs/shape "circle"}
{:type :y :local/id 6, :obs/A "15", :obs/value 3.0, :obs/shape "triangle"} ] )
(def conn (d/create-conn {}))
(d/transact! conn data)
.....
这里是 DataScript 的作者。
首先,交易! returns交易报告,不是连接。它改变作为参数传入的 conn
的内容:
(def conn (d/create-conn schema))
...
(d/transact! conn u/fixtures)
其次,您不能在查询中使用动态绑定拉模式。使用正常拉取 API:
(let [db (d/db state)
datoms (d/datoms db :aevt :product/name)
eids (map :e datoms)]
(d/pull-many db query eids))
很遗憾,我无法在 Om.Next 部分为您提供帮助。如果有什么错误我就不知道了
希望对您有所帮助。