Skip to content

Instantly share code, notes, and snippets.

@J-O-N
Created March 2, 2012 02:47
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 J-O-N/1955158 to your computer and use it in GitHub Desktop.
Save J-O-N/1955158 to your computer and use it in GitHub Desktop.
make parent
var make_parent = {
accounts_db: $.couch.db('accounts'),
parent_id: '9a07ed4820fd17b54efbf53dcaece757',
get_accounts: function(callback) {
var THIS = this;
THIS.accounts_db.view('accounts/listing_by_realm', {
success: function(data) {
callback(data.rows);
}
});
},
get_doc: function(db, id, callback) {
var THIS = this;
db.openDoc(id, null, {
async: false,
success: function(data) {
callback(data);
}
});
},
save_doc: function(db, doc, callback) {
var THIS = this;
db.saveDoc(doc, {
async: false,
success: function(data) {
callback(data);
}
});
},
fix_account_doc: function(db, account_id) {
var THIS = this;
THIS.get_doc(db, account_id, function(doc) {
if($.inArray(THIS.parent_id, doc.pvt_tree) > -1) {
return false;
}
if(doc.pvt_tree.length == 0 || doc.pvt_tree[0] == "") {
doc.pvt_tree = [THIS.parent_id];
}
else {
doc.pvt_tree.unshift(THIS.parent_id);
}
THIS.save_doc(db, doc, function(new_doc) {
if($.inArray(THIS.parent_id, new_doc.pvt_tree) > -1) {
console.log('success!');
}
else {
console.log('failure!');
}
});
});
},
run: function() {
var THIS = this;
THIS.get_accounts(function(accounts) {
$.each(accounts, function() {
var account_id = this.value.account_id,
account_db = $.couch.db(decodeURIComponent(this.value.account_db));
if(account_id == THIS.parent_id) {
return true;
}
THIS.fix_account_doc(account_db, account_id);
THIS.fix_account_doc(THIS.accounts_db, account_id);
});
});
}
};
make_parent.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment