Skip to content

Instantly share code, notes, and snippets.

@boxxxie
Created September 27, 2012 20:46
Show Gist options
  • Save boxxxie/3796374 to your computer and use it in GitHub Desktop.
Save boxxxie/3796374 to your computer and use it in GitHub Desktop.
couch backbone sync
var con,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; },
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Backbone.couch_connector = con = {
config: {
db_name: "backbone_connect",
ddoc_name: "backbone_example",
view_name: "byCollection",
list_name: null,
global_changes: false,
base_url: null
},
helpers: {
extract_collection_name: function(model) {
var _name, _splitted;
if (model == null) {
throw new Error("No model has been passed");
}
if (!(((model.collection != null) && (model.collection.url != null)) || (model.url != null))) {
return "";
}
if (model.url != null) {
_name = _.isFunction(model.url) ? model.url() : model.url;
} else {
_name = _.isFunction(model.collection.url) ? model.collection.url() : model.collection.url;
}
if (_name[0] === "/") {
_name = _name.slice(1, _name.length);
}
_splitted = _name.split("/");
_name = _splitted.length > 0 ? _splitted[0] : _name;
_name = _name.replace("/", "");
return _name;
},
make_db: function() {
var db;
db = $.couch.db(con.config.db_name);
if (con.config.base_url != null) {
db.uri = "" + con.config.base_url + "/" + con.config.db_name + "/";
}
return db;
}
},
read: function(model, opts) {
if (model.models) {
return con.read_collection(model, opts);
} else {
return con.read_model(model, opts);
}
},
read_collection: function(coll, opts) {
var keys, option, view_options, _ddoc, _i, _len, _list, _opts, _view,
_this = this;
_view = this.config.view_name;
_ddoc = this.config.ddoc_name;
_list = this.config.list_name;
keys = [this.helpers.extract_collection_name(coll)];
if (coll.db != null) {
if (coll.db.changes || this.config.global_changes) {
coll.listen_to_changes();
}
if (coll.db.view != null) {
_view = coll.db.view;
}
if (coll.db.ddoc != null) {
_ddoc = coll.db.ddoc;
}
if (coll.db.keys != null) {
keys = coll.db.keys;
}
if (coll.db.list != null) {
_list = coll.db.list;
}
}
_opts = {
keys: keys,
success: function(data) {
var doc, _i, _len, _ref, _temp;
_temp = [];
_ref = data.rows;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
doc = _ref[_i];
if (doc.value) {
_temp.push(doc.value);
} else {
_temp.push(doc.doc);
}
}
opts.success(_temp);
return opts.complete();
},
error: function(status, error, reason) {
var res;
res = {
status: status,
error: error,
reason: reason
};
opts.error(res);
return opts.complete(res);
}
};
view_options = ["key", "keys", "startkey", "startkey_docid", "endkey", "endkey_docid", "limit", "stale", "descending", "skip", "group", "group_level", "reduce", "include_docs", "inclusive_end", "update_seq"];
for (_i = 0, _len = view_options.length; _i < _len; _i++) {
option = view_options[_i];
if (opts[option] != null) {
_opts[option] = opts[option];
}
}
if ((coll.db != null) && (coll.db.view != null) && !(coll.db.keys != null)) {
delete _opts.keys;
}
if (_list) {
return this.helpers.make_db().list("" + _ddoc + "/" + _list, "" + _view, _opts);
} else {
return this.helpers.make_db().view("" + _ddoc + "/" + _view, _opts);
}
},
read_model: function(model, opts) {
if (!model.id) {
throw new Error("The model has no id property, so it can't get fetched from the database");
}
return this.helpers.make_db().openDoc(model.id, {
success: function(doc) {
opts.success(doc);
return opts.complete();
},
error: function(status, error, reason) {
var res;
res = {
status: status,
error: error,
reason: reason
};
opts.error(res);
return opts.complete(res);
}
});
},
create: function(model, opts) {
var coll, vals;
vals = model.toJSON();
coll = this.helpers.extract_collection_name(model);
if (coll.length > 0) {
vals.collection = coll;
}
return this.helpers.make_db().saveDoc(vals, {
success: function(doc) {
opts.success({
_id: doc.id,
_rev: doc.rev
});
return opts.complete();
},
error: function(status, error, reason) {
var res;
res = {
status: status,
error: error,
reason: reason
};
opts.error(res);
return opts.complete(res);
}
});
},
update: function(model, opts) {
return this.create(model, opts);
},
del: function(model, opts) {
return this.helpers.make_db().removeDoc(model.toJSON(), {
success: function() {
return opts.success();
},
error: function(nr, req, e) {
var res;
if (e === "deleted") {
opts.success();
return opts.complete();
} else {
res = {
status: status,
error: error,
reason: reason
};
opts.error(res);
return opts.complete(res);
}
}
});
}
};
Backbone.sync = function(method, model, opts) {
if (opts.success == null) {
opts.success = function() {};
}
if (opts.error == null) {
opts.error = function() {};
}
if (opts.complete == null) {
opts.complete = function() {};
}
switch (method) {
case "read":
return con.read(model, opts);
case "create":
return con.create(model, opts);
case "update":
return con.update(model, opts);
case "delete":
return con.del(model, opts);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment