如何使用和不使用JavaScript都将数据发布到CouchDB [英] How do you post data to CouchDB both with and without using JavaScript

查看:79
本文介绍了如何使用和不使用JavaScript都将数据发布到CouchDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表演,显示一个带有从文档填充的字段的表单。我想更改字段中的值,然后保存更新的文档。

I have a show which displays a form with fields populated from a document. I'd like to change the values in the field and then save the updated document.

我很难找到一个清晰,简洁的方法来执行此操作。

I'm having trouble finding a clear, concise example of how to do this.

严重的是,仅完成此示例将为许多人带来奇迹(为简洁起见,我会遗漏很多东西)。

Seriously, just finishing this example would work wonders for so many people (I'm going to leave a lot of stuff out to make this concise).

安装Couchapp

这不在我的问题范围内,但这是说明以确保完整性。

This is outside the scope of my question, but here are the instructions for completeness.

创建一个沙发应用

同样,这超出了我的问题范围。这是关于如何创建沙发应用程序的非常简洁的教程

Again, this is kind outside the scope of my question. Here is a perfectly concise tutorial on how to create a couchapp.

创建模板

在沙发应用的根目录中创建一个名为 templates 的文件夹em>。在 templates 文件夹中,创建一个名为 myname.html 的HTML页面。

Create a folder in the root of your couchapp called templates. Within the templates folder create an HTML page called myname.html. Put the following in it.

<html>
    <head>
        <title>{{ title }}</title>
    </head>
    <body>
        <form method='post' action='#'>
            <fieldset>
                Hello <input type='text' name='name' value='{{ name }}'>
                <input type='submit' name='submit' value='submit'>
        </form>
    </body>
</html>

创建节目

请参见上面的教程,了解如何执行此操作。

See the tutorial above for hwo to do this.

将此代码添加到名为myname的节目中。

Add this code to a show called myname.

function(doc, req) { 
    if (doc) {  

        var ddoc = this
        var Mustache = require("vendor/couchapp/lib/mustache");

        var data = {
            title: "The Name",
            name: "Bobbert"
        }

        return Mustache.to_html(ddoc.templates.myname, data)
    } else {
        return ('nothing here baby')
    }
}

通过...用新名称更新文档...

谁可以通过客户端和服务器端完成此步骤?

So who can complete this step via both the client side and the server side?

请不要将我指向该指南,我需要用您的语言阅读。

Please don't point me to the guide, I need to read it in your words.

谢谢。

编辑:

尽管返回值不是很漂亮,但是只要将表单发布到更新处理程序中,就会更新文档。

Although the return value isn't pretty, just posting a form to the update handler will update the document.

推荐答案

您可能需要研究 更新处理程序函数

You will probably want to look into update handler functions.

更新处理程序可处理精细的文档转换。因此,您可以采用一种具有不同目的的表格,并且仅通过更新处理程序来更新文档中的相关字段。

An update handler handles granular document transformations. So you can take 1 form, that has one distinct purpose, and only update the relevant fields in your document via the update handler.

您的更新处理程序将需要使用一个表单中的 PUT 请求。浏览器无法直接执行此操作,因此您需要一些JavaScript才能为您处理。如果您使用的是jQuery,此插件可以采用您的表单,并使用<$通过AJAX无缝提交。 c $ c> PUT 。

Your update handler will need to take a PUT request from your form. A browser can't do this directly, so you'll need some javascript to handle this for you. If you're using jQuery, this plugin can take your form and submit it seamlessly via AJAX using PUT for you.

在函数内部,您可以接受要接受的字段,在这种情况下,名称,并将其直接应用于文档。 (输入验证可通过 validate_doc_update 函数)

Inside the function, you can take the fields you are accepting, in this case name and apply that directly to the document. (input validation can be handled via the validate_doc_update function)

{
    "updates": {
        "name": function (doc, req) {
            doc.name = req.form.name;
            return [doc, "Name has been updated"];
        }
    }
}



HTML



< form id = myForm action = / db / _design / ddoc / _update / name / doc_id> ...< / form>

$(document).ready(function() {
    $('#myForm').ajaxForm({
        type: "PUT",
        success: function () { 
            alert("Thank you"); 
        }
    }); 
}); 

一旦您已经建立并运行了这个基本示例,添加更多示例并不难。您的更新处理程序的高级功能。 :)

Once you've gotten this basic example up and running, it's not much more difficult to add some more advanced features to your update handlers. :)

这篇关于如何使用和不使用JavaScript都将数据发布到CouchDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆