Clojurescript 重新帧:在嵌套向量中添加新地图元素
我在我的“db”中有这个结构(有序映射),关键字是:questions: 我可以在事件处理程序“re frame/reg event db”中添加一个新问题,添加:Clojurescript 重新帧:在嵌套向量中添加新地图元素,clojurescript,reagent,re-frame,Clojurescript,Reagent,Re Frame,我在我的“db”中有这个结构(有序映射),关键字是:questions: 我可以在事件处理程序“re frame/reg event db”中添加一个新问题,添加: (assoc-in db [:questions (:id response)] new-map-stuff) 但我不知道如何在“:answers”键中添加地图。此外,我担心每次添加新答案时都会呈现所有问题 我读过关于“路径”拦截器(类似于“更新中”)的文章,但找不到关于如何使用它的示例 就像在普通clojure中一样,在中使用更
(assoc-in db [:questions (:id response)] new-map-stuff)
但我不知道如何在“:answers”键中添加地图。此外,我担心每次添加新答案时都会呈现所有问题
我读过关于“路径”拦截器(类似于“更新中”)的文章,但找不到关于如何使用它的示例 就像在普通clojure中一样,在中使用
更新。首先从Clojure备忘单开始:
或ClojureScript版本:
查看中更新的文档:
你需要两种成分
一条导航到突变位点的路径
执行(1)中给定点的突变的函数
您没有完全指定数据。我假设它看起来是这样的:
(def db {:questions [
{:33 {:question "one", :id 33,
:answers [{:id 22, :question_id 33, :answer "one", :correct false}
{:id 4, :question_id 33, :answer "two", :correct false}]}},
{:39 {:question "two", :id 39, :answers []}},
{:41 {:question "three", :id 41,
:answers [{:id 29, :question_id 41, :answer "one", :correct false}
{:id 35, :question_id 41, :answer "two", :correct true}]}}]})
我将制作一个新的答案
地图以添加:
(def new-answer {:id 42, :question_id 442, :answer "The Ultimate", :correct false})
此代码以片段的形式显示流程
(let [submap (get-in db [:questions 0 :33 :answers])
modified (conj submap new-answer)
final-answer (update-in db [:questions 0 :33 :answers] conj new-answer) ]
首先导航以获取submap
:
submap =>
[{:id 22, :question_id 33, :answer "one", :correct false}
{:id 4, :question_id 33, :answer "two", :correct false}]
以及添加新答案的方式:
modified =>
[{:id 22, :question_id 33, :answer "one", :correct false}
{:id 4, :question_id 33, :answer "two", :correct false}
{:id 42, :question_id 442, :answer "The Ultimate", :correct false}]
并将其全部整合到一个操作中:
final-answer =>
{:questions
[{:33
{:question "one",
:id 33,
:answers
[{:id 22, :question_id 33, :answer "one", :correct false}
{:id 4, :question_id 33, :answer "two", :correct false}
{:id 42, :question_id 442, :answer "The Ultimate", :correct false}]}}
{:39 {:question "two", :id 39, :answers []}}
{:41
{:question "three",
:id 41,
:answers
[{:id 29, :question_id 41, :answer "one", :correct false}
{:id 35, :question_id 41, :answer "two", :correct true}]}}]}
所有这些在ClojureScript中的作用与Clojure中的相同
不要担心使用路径
拦截器。我认为这会使事情变得混乱,而不是有帮助。不必担心渲染速度,除非:
你有它的工作和测试,并知道它是正确的
您已经测量了渲染速度,可以记录存在问题以及需要加速的量
对于长列表,请查看可以添加到每个列表元素中的键
元数据。见:
更新
请注意,submap
和modified
仅用于演示以显示中更新的工作原理。
表达式中的更新是代码中实际包含的唯一内容。另外,请注意,
中的更新仅使用
db
和新答案
非常感谢!需要注意的是,在dotest块中,最终调用是:final answer(在db中更新[:questions 0:33:answers]conj modified)对吗?否。我们使用conj
将新答案添加到现有:answers
数组的末尾。研究更新
和更新
。找到modified
的路径会以静默方式插入对目标的引用,作为函数conj
的第一个参数。新答案
成为conj
的第二个参数。然后将conj
的结果缝合回层次结构的深处。
final-answer =>
{:questions
[{:33
{:question "one",
:id 33,
:answers
[{:id 22, :question_id 33, :answer "one", :correct false}
{:id 4, :question_id 33, :answer "two", :correct false}
{:id 42, :question_id 442, :answer "The Ultimate", :correct false}]}}
{:39 {:question "two", :id 39, :answers []}}
{:41
{:question "three",
:id 41,
:answers
[{:id 29, :question_id 41, :answer "one", :correct false}
{:id 35, :question_id 41, :answer "two", :correct true}]}}]}