web2py and pouchdb

56 views
Skip to first unread message

黄祥

unread,
Sep 20, 2017, 10:55:33 PM9/20/17
to web2py-users
is web2py can work with pouchdb ?
any example of DAL pouchdb connection ?

thanks and best regards,
stifan

黄祥

unread,
Oct 8, 2017, 10:17:37 PM10/8/17
to web2py-users
tested with min app from js on views side, but the result is not expected, the form field name store in indexeddb {{=request.vars.your_name}} as is not the form field value
controllers/default.py
def pouchdb():
    form = SQLFORM.factory(
        Field('your_name', requires = IS_NOT_EMPTY() )
        )
    if form.process().accepted:
        response.flash = 'form accepted'
    elif form.errors:
        response.flash = 'form has errors'
    return dict(form = form)

views/default/pouchdb.html
{{extend 'layout.html'}}
{{=form}}
<script src="{{=URL('static','js/test.js')}}"></script>
{{=response.toolbar() }}

static/js/test.js
(function() {
var db = new PouchDB('test');
document.getElementsByClassName('form-horizontal')[0].addEventListener("submit", myFunction);
function myFunction() {
  var doc = {
    "_id": "mittens",
    "name": "{{=request.vars.your_name}}" // already tried "{{request.vars.your_name}}", "{{=form.vars.your_name}}", "{{form.vars.your_name}}"
  };
  db.put(doc);
}
})();

any idea how to work with pouchdb in web2py?

António Ramos

unread,
Oct 9, 2017, 7:22:38 AM10/9/17
to web...@googlegroups.com
Why do you want pouchdb with web2py ? for client cache?
pouchdb is a client only database unless you sync it to a couchdb server.

Web2py is a server side framework.

Anyway your problem is a javascript/web2py problem , not a pouchdb/web2py problem...

maybe this helps you a litle


Regards

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

黄祥

unread,
Oct 9, 2017, 5:52:47 PM10/9/17
to web2py-users
yes, you are right, it's javascript/web2py problem, fix it with:
controllers/default.py
def pouchdb():
    form = SQLFORM.factory(
        ...
        )
    if form.process(formname = request.function).accepted:
        response.flash = 'form accepted'
    elif form.errors:
        response.flash = 'form has errors'
    return dict(form = form)

static/js/test.js
(function() {
var localDB = new PouchDB('test');
/*
var remoteDB = new PouchDB('http://localhost:5984/test');
localDB.sync(remoteDB).on('complete', function () {
  console.log('Synced!');
}).on('error', function (err) {
  console.log('Error!');
});
*/
document.getElementsByClassName('form-horizontal')[0].addEventListener("submit", addFunction);
function addFunction() {
  var doc = {
    _id: new Date().toISOString(),
    form: jQuery('input[name = _formname]').val(),
    show: {
      testcheckbox0: jQuery('#testcheckbox0').val(),
      testcheckbox1: jQuery('#testcheckbox1').val()
    },
    testselect: jQuery('select[name = testselect]').val(),
    testinput: jQuery('input[name = testinput]').val()
  };
  localDB.put(doc);
}
})();
Reply all
Reply to author
Forward
0 new messages