将JavaScript websql转换为Clojurescript

时间:2016-01-20 00:51:26

标签: javascript clojure clojurescript web-sql

我正在阅读以下教程

https://developer.apple.com/library/iad/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/UsingtheJavascriptDatabase/UsingtheJavascriptDatabase.html

并且在将data转换为数据处理函数

中的cljs时遇到问题
select substr(column1, 6, 5)
from Table1;

这是我的cljs代码

function dataHandler(transaction, results)
{
// Handle the results
var string = "Green shirt list contains the following people:\n\n";
for (var i=0; i<results.rows.length; i++) {
    // Each row is a standard JavaScript array indexed by
    // column names.
    var row = results.rows.item(i);
    string = string + row['name'] + " (ID "+row['id']+")\n";
}
alert(string);
}

所以我的问题是如何将js dataHandler转换为我的cljs success-handler?

2 个答案:

答案 0 :(得分:2)

尝试将结果的行转换为序列,而不是整个结果。

(defn success-handler [tx, results]
   (println "success handler")
   (println results)

  (doseq [result (.-rows results)]
    (prn result)))

如果行没有实现Iterable,那么就采用旧式的方式

(defn success-handler [tx, results]
   (println "success handler")
   (println results)

  (let [rows (.-rows results)]
   (doall
    (for [i (range (.-length rows))]
      (let [row (nth rows i)]
        (prn result))))))

答案 1 :(得分:0)

以下是解决方案:

(defn success-handler [tx, results]
  (let [rows (.-rows results)]
    (doseq [i (range (.-length rows))]
      (prn (.item rows i)))))