Permalink
Browse files

adding methods to deal with hierarchical items

  • Loading branch information...
1 parent 49ae8b0 commit ed9c3faae0519be8ede0538e5f962a91104a07f9 @mariuskubilius committed Jan 12, 2013
Showing with 22 additions and 17 deletions.
  1. +22 −17 lib/ancestorTree.js
View
@@ -4,7 +4,9 @@
* @author Marius Kubilius
* @todo add methods and statics dealing with the tree.
*/
-var ObjectId = require('mongoose').Schema.ObjectId;
+var Mongoose = require('mongoose');
+var ObjectId = Mongoose.Schema.ObjectId;
+
ancestorTree = function(schema){
@@ -84,26 +86,29 @@ ancestorTree = function(schema){
next();
}
});
-}
-/**
- * @todo write tests for statics.
- */
-ancestorTree.statics = {
- findDirectChildren: function(options, cb){
- options = options || {};
- fields = options.fields || {title: 1, slug: 1};
- this.find({parent: this._id}, fields, cb);
- },
- findChildren: function(options, cb){
+
+ schema.method('findDirectChildren', function(opts, cb) {
+ opts = opts || {};
+ fields = opts.fields || {title: 1, slug: 1};
+ return this.model(this.constructor.modelName).find({parent: this._id}, fields, cb);
+ });
+
+ schema.method('findChildren', function(options, cb) {
options = options || {};
fields = options.fields || {title: 1, slug: 1};
- this.find({hierarchy: this._id}, fields, cb);
- },
- getHierarchy: function(options, cb){
+ return this.model(this.constructor.modelName).find({ancestors: this._id}, fields, cb);
+ });
+
+ schema.method('getAncestors', function(options, cb) {
options = options || {};
fields = options.fields || {title: 1, slug: 1};
- this.find({$in:{_id: this.ancestors}}, fields, cb);
- }
+ return this.model(this.constructor.modelName).find({_id: {$in: this.ancestors}}, fields, cb);
+ });
+
};
+/**
+ * @todo write tests for statics.
+ */
+
module.exports = ancestorTree;

0 comments on commit ed9c3fa

Please sign in to comment.