Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojurescript 重新帧:在嵌套向量中添加新地图元素_Clojurescript_Reagent_Re Frame - Fatal编程技术网

Clojurescript 重新帧:在嵌套向量中添加新地图元素

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中一样,在中使用更

我在我的“db”中有这个结构(有序映射),关键字是:questions:

我可以在事件处理程序“re frame/reg event db”中添加一个新问题,添加:

(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}]}}]}