loops - 在 Clojure doseq 函数中间暂停?
问题描述
我的程序允许用户选择要导入数据库的文件。在此之前,它一次只允许他们导入一个文件。让他们一次导入多个文件很容易。但我遇到的问题是,如果数据库已经包含一个具有相同标题的页面,我想在使用正在导入的版本覆盖数据库中的版本之前显示警告并获得确认。
这是我到目前为止所拥有的。它在很大程度上遵循了我导入单个文件的做法。
;; Handler for the POST "/import" route.
(defn- post-import-page
"Import the file(s) specified in the upload dialog. Checks the page
title of the import file against existing pages in the database. If
a page of the same name already exists, asks for confirmation before
importing."
[{{file-info "file-info"
referer "referer"} :multipart-params :as req}]
(let [file-vec (if (map? file-info)
(conj [] file-info)
file-info)]
(doseq [fm file-vec]
(let [file-name (:filename fm)
import-map (files/load-markdown-from-file (:tempfile fm))
page-title (get-in import-map [:meta :title])
id-exists? (db/title->page-id page-title)]
(if id-exists?
(build-response
(layout/compose-import-existing-page-warning
import-map file-name referer) req)
(do-the-import import-map file-name req))))))
此函数导入数据库中尚不存在的任何文件,但不会导入任何会覆盖具有相同标题的现有数据库条目的任何文件。它也从不显示要求确认的警告页面。
警告页面的构造如下:
(defn compose-import-existing-page-warning
"Return a page stating that a page with the same title already exists
in the wiki. Ask the user to proceed or cancel the import."
[import-map file-name referer]
(short-form-template
[:div {:class "cwiki-form"}
(form-to {:enctype "multipart/form-data"
:autocomplete "off"}
[:post "proceed-with-import"]
(hidden-field "import-map" import-map)
(hidden-field "file-name" file-name)
(hidden-field "referer" referer)
[:p {:class "form-title"} "Page Already Exists"]
[:div {:class "form-group"}
[:p (str "A page with the title \"" (get-in import-map [:meta :title])
"\" already exists in the wiki.")]
[:p (str "Click \"Proceed\" to delete the existing page and "
"replace it with the contents of the imported file.")]
[:div {:class "button-bar-container"}
(submit-button {:id "proceed-with-import-button"
:class "form-button button-bar-item"}
"Proceed")
[:input {:type "button" :name "cancel-button"
:value "Cancel"
:class "form-button button-bar-item"
:autofocus "autofocus"
:onclick "window.history.back();"}]]])]))
程序如何在doseq
(或其他循环函数)中间暂停以显示确认页面并等待用户做出选择?
解决方案
只需在循环中间使用read-line,然后if
选择所需的分支。以下是您可能会发现有用的其他文档的列表,尤其是Clojure CheatSheet。
推荐阅读
- macos - DMG 无法打开,因为即使在签署 DMG 应用程序堆栈溢出后也无法识别开发者的身份
- lets-encrypt - 如何在 certbot 中再次使用相同的 DNS 质询密钥?
- html - 如何在 Angular 2 中通过 id 获取 .json 对象?
- serenity-bdd - Serenity 报告没有任何 REST 查询
- java - 如何在eclipse中将文本字段中的值添加到jcombobox
- web-services - Netsuite 的用户配置?
- java - 引用 ID 是否与 java 中内存位置的地址相同?
- asp.net-mvc - kendo datepicker 组件当前日期未显示
- talend - Pardot API 从 Prospect 表中检索所有记录
- html - 表单渲染不正确