开发者

How to generate HTML Forms for a CouchApp ( or for any JSON documents )

开发者 https://www.devze.com 2023-03-27 05:48 出处:网络
Traditional web frameworks like Rails and Django have ways to generate HTML forms based on the \"models\", which correspond to the schema -- the TABLEs in the database.

Traditional web frameworks like Rails and Django have ways to generate HTML forms based on the "models", which correspond to the schema -- the TABLEs in the database.

How does that work with CouchDB? ( which has no tables and no schema )

Do you just create custom forms by hand, and let those act as your "model/schema"? Do you have "models" defined in javascript? Does that allow auto-generated forms? Do you have "models" or something similar defined as JSON documents?

Also, traditional HTML forms handle a single "record" -- with one value per "key". JSON documents can have a list of values under one key, or another nested document! -- traditional HTML forms don't handle that! JSON documents ( even documents that are of a known "doctype" ) could have "extra" ( undefined ) fields on them -- they could have missing fields -- 开发者_运维问答they could have fields whose value is of a different type than what is expected. All of these things are not handled by traditional HTML forms.

Is there a better way for a user to interact with a JSON document than traditional HTML forms?


The Kanso CouchApp framework provides the most comprehensive schema and form generating tools for CouchDB: http://kansojs.org

Take a look at the tutorial for example usage. The schema definitions were created from the group up to work with CouchDB and allow embedded objects and other complex JSON-like structures, it's an incredibly flexible format (much more so than JSON-schema).

You can also automatically generate forms from your schema definitions, or extend these with custom fields, widgets, validators, permissions, etc.


Although CouchDB holds json with no schema, you could define a json schema on the presentation side. And then you can feed this json schema to inputEx which will create a form for you.

See here: http://neyric.github.com/inputex/examples/json-schema.html

InputEx is very flexible, and will handle arrays, objects, and special editors for properties. Just understand that the json schema standard is not well defined. If you are just using it to do CRUD operations, you should be fine.

Over time, if your form evolves and the schema changes, it really does not matter to couchdb. But you may need to provide users with a way to migrate data to a new schema.

0

精彩评论

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

关注公众号