开发者

How do you post data to CouchDB both with and without using JavaScript

开发者 https://www.devze.com 2023-03-14 06:30 出处:网络
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 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).

Install Couchapp

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

Create a couchapp

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

Create a template

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 }}<开发者_StackOverflow社区;/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>

Create a show

See the tutorial above for hwo to do this.

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')
    }
}

Update the document with a new name by ...

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.

Thanks.

Edit:

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.

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.

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)

Update Handler (in your Design Document)

{
    "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>

JavaScript

$(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. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消