Skip to content

Instantly share code, notes, and snippets.

@dch
Forked from agaoglu/USAGE.md
Created April 18, 2011 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dch/926469 to your computer and use it in GitHub Desktop.
Save dch/926469 to your computer and use it in GitHub Desktop.
{
activate: function(e, id) {
// activate the form, enabling edits
if (id == "new"){
$(this).trigger('newitem');
} else {
$(this).trigger('loaditem', id);
}
},
loaditem: function(e, id) {
// load data from couchdb and display it in the form
this.reset();
var that = this;
$$(this).app.db.openDoc(id, {
error: function() {
$$(that).ldoc = {_id:id}
that.reset();
},
success: function(doc){
$$(that).ldoc = doc;
var fdoc = $.flatten(doc);
for (var name in fdoc){
$(':input[name='+name+']', that).val(fdoc[name]);
$(':checkbox[name='+name+']', that).attr('checked',fdoc[name]);
}
$(that).trigger('postconstruct');
}
})
},
newitem: function(e) {
// activate form for new items
this.reset();
$$(this).ldoc = {};
$(this).trigger('postconstruct');
},
removeitem: function(e){
// Self-explanatory, delete doc from couchdb
if (confirm("Are you sure?")){
$$(this).app.db.removeDoc($$(this).ldoc);
$(this).trigger('predestroy');
}
},
submit: function(e){
// encode the data in form and save it to couchdb
var reg = $.extend({},$$(this).ldoc, $(this).toDeepJson());
var that = this;
$$(this).app.db.saveDoc(reg,{
success: function(res){
reg._id = res.id;
reg._rev = res.rev;
$$(that).ldoc = reg;
$(that).trigger('postconstruct');
}
});
return false;
}
}
(function( $ ){
$.fn.toDeepJson = function() {
function parse(val){
if (val == ""){ return null; }
if (val == "true"){ return true; }
if (val == "false"){ return false; }
if (val == String(parseInt(val))){ return parseInt(val); }
if (val == String(parseFloat(val))){ return parseFloat(val); }
return val;
}
function toNestedObject(obj, arr){
var key = arr.shift();
if (arr.length > 0) {
obj[key] = toNestedObject(obj[key] || {}, arr);
return obj;
}
return key;
}
if (this.length == 1){
return $.makeArray(this[0].elements)
.filter(function(e){
return e.name != "" && (e.type == 'radio' ? e.checked : true);
})
.map(function(e){
var names = e.name.split('.');
if (e.type == 'checkbox') {
e.value = e.checked;
}
names.push(parse(e.value));
return names;
})
.reduce(toNestedObject, {});
} else {
throw({error:"Can work on a single form only"})
}
};
$.flatten = function (obj){
var ret = {}
for (var key in obj){
if (typeof obj[key] == 'object'){
var fobj = $.flatten(obj[key]);
for (var extkey in fobj){
ret[key+"."+extkey] = fobj[extkey];
}
} else {
ret[key] = String(obj[key]);
}
}
return ret;
}
})( jQuery );

CRUD requires jquery.deepjson.js to work so it should be included

<script type="text/javascript" src="js/jquery.deepjson.js"></script>

Then you can assign whatever's in crud.json to a variable or you can put crud.json into your evently directory and let couchapp push it to your application.

Simplest way to use it is

$.couch.app(function(app) {
  $('#form').evently(app.ddoc.evently.crud, app);
}

If you want to put some lifecycle methods around (you usually need)

$.couch.app(function(app) {
  $('#form').evently($.extend({}, app.ddoc.evently.crud, {
    postconstruct: function(e){
      // after form readies itself for editing
      // may show form or delete buttons here
    },
    predestroy: function(e) {
      // triggers on object deletion only
      // useful to hide form
    }
  }), app);
}

Then you can add function to your controls like

$('.items a').click(function(e){
    // edit item, doc id presumed set to id attr of a
    $('#form').trigger('activate',$(this).attr('id'));
});
$('button.new').click(function(e){
    // ready form for a new item
    $('#form').trigger('activate', 'new');
});
$('button.save').click(function(e){
    // save the form
    $('#form').submit();
});
$('button.delete').click(function(e){
    // delete the item set in form
    $('#form').trigger('removeitem');
});

It is possible to use $$('#form').ldoc to access the doc itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment