3.2. JavaScript¶
JS函数 | 在设计文档功能中的合理使用 |
---|---|
emit() |
地图功能 |
getRow() |
列表函数 |
JSON |
任何 |
isArray() |
任何 |
log() |
任何 |
provides() |
显示函数, 列表函数 |
registerType() |
显示函数, 列表函数 |
require() |
任何,除了 约化和重排函数 |
send() |
列表函数 |
start() |
列表函数 |
sum() |
任何 |
toJSON() |
任何 |
3.2.1. 设计功能上下文¶
每个设计功能在预定义对象、模块和函数的特定上下文中执行:
-
emit
(key, value)¶ 发出a key -`value`对,以便在map函数完成后由CouchDB进一步处理。
参数: - key -- 视图键
- value -- 这个 key 的关联值
function(doc){ emit(doc._id, doc._rev); }
-
getRow
()¶ 从相关视图结果中提取下一行。
返回: 查看结果行 返回类型: 对象 function(head, req){ send('['); row = getRow(); if (row){ send(toJSON(row)); while(row = getRow()){ send(','); send(toJSON(row)); } } return ']'; }
-
isArray
(obj)¶ 一个帮助函数,用于检查提供的值是否为 Array .
参数: - obj -- 任何JavaScript值
返回: true
如果 obj 是 Array -打字,false
否则返回类型: 布尔
-
log
(message)¶ 将消息记录到CouchDB日志(在 INFO 水平)。
参数: - message -- 要记录的消息
function(doc){ log('Procesing doc ' + doc['_id']); emit(doc['_id'], null); }
在map函数运行之后,可以在CouchDB日志中找到以下行(例如 /var/log/couchdb/couch.log ):
[Sat, 03 Nov 2012 17:38:02 GMT] [info] [<0.7543.0>] OS Process #Port<0.3289> Log :: Processing doc 8d300b86622d67953d102165dbe99467
-
provides
(key, func)¶ 为指定的MIME密钥注册可调用处理程序。
参数: - key -- MIME密钥以前由定义
registerType()
- func -- MIME类型处理程序
- key -- MIME密钥以前由定义
-
registerType
(key, *mimes)¶ 按关联项注册MIME类型列表 key .
参数: - key -- MIME类型
- mimes -- MIME类型枚举
预定义映射 (key -`数组“”):
- all :
*/*
- text :
text/plain; charset=utf-8
,txt
- html :
text/html; charset=utf-8
- xhtml :
application/xhtml+xml
,xhtml
- xml :
application/xml
,text/xml
,application/x-xml
- js :
text/javascript
,application/javascript
,application/x-javascript
- css :
text/css
- ics :
text/calendar
- csv :
text/csv
- rss :
application/rss+xml
- atom :
application/atom+xml
- yaml :
application/x-yaml
,text/yaml
- multipart_form :
multipart/form-data
- url_encoded_form :
application/x-www-form-urlencoded
- json :
application/json
,text/x-json
-
require
(path)¶ 通过指定的 path . 路径不应以斜杠开头。
参数: - path -- 从设计文档根开始的CommonJS模块路径
返回: 导出的语句
-
send
(chunk)¶ 发送一个字符串 chunk 作为回应。
参数: - chunk -- 文本块
function(head, req){ send('Hello,'); send(' '); send('Couch'); return ; }
-
start
(init_resp)¶ 启动分块响应。作为一种选择,一种习惯 response 对象可能在此时发送。为 list -仅限功能!
注解
列表函数可以设置 HTTP response code 和 headers 通过调用这个函数。必须先调用此函数
send()
,getRow()
或A return 语句;否则,查询服务器将使用空对象隐式调用此函数 ({{}}
)function(head, req){ start({ "code": 302, "headers": { "Location": "http://couchdb.apache.org" } }); return "Relax!"; }
-
sum
(arr)¶ 和 arr 的项目。
参数: - arr -- 数字数组
返回类型: 数
-
toJSON
(obj)¶ 编码 obj 到JSON字符串。这是
JSON.stringify
方法。参数: - obj -- JSON可编码对象
返回: JSON字符串
3.2.2. CommonJS模块¶
支持 CommonJS Modules (在couchdb0.11.0中引入)允许您创建模块化设计函数,而不需要重复功能。
下面是一个检查用户权限的CommonJS模块:
function user_context(userctx, secobj) {
var is_admin = function() {
return userctx.indexOf('_admin') != -1;
}
return {'is_admin': is_admin}
}
exports['user'] = user_context
每个模块都可以访问其他全局变量:
- module (object): Contains information about the stored module
- id (string): The module id; a JSON path in ddoc context
- current (code): Compiled module code object
- parent (object): Parent frame
- exports (object): Export statements
- exports (object): Shortcut to the
module.exports
object
CommonJS模块可以添加到设计文档中,如下所示:
{
"views": {
"lib": {
"security": "function user_context(userctx, secobj) { ... }"
}
},
"validate_doc_update": "function(newdoc, olddoc, userctx, secobj) {
user = require('views/lib/security').user_context(userctx, secobj);
return user.is_admin();
}"
"_id": "_design/test"
}
模块路径与设计文档的 views
对象,但模块只能从通过引用的对象加载 lib
. 这个 lib
结构仍然可以用于视图函数,只需将视图函数存储在。 views.lib.map
, views.lib.reduce
等。