Skip to content
Browse files

use self.constructor instead of creating a new model with mongoose.mo…

…del. #4
  • Loading branch information...
1 parent e788fef commit 0e7f4bbf5dfd99b78adad9b6148afc80e50818fd @luccastera luccastera committed Oct 25, 2012
Showing with 9 additions and 11 deletions.
  1. +9 −11 lib/nested_set.js
View
20 lib/nested_set.js
@@ -19,7 +19,6 @@ var NestedSetPlugin = function(schema, options) {
schema.pre('save', function(next) {
var self = this;
- var model = mongoose.model(this.constructor.modelName);
if (self.parentId) {
self.parent(function(err, parentNode) {
if (!err && parentNode && parentNode.lft && parentNode.rgt) {
@@ -37,8 +36,8 @@ var NestedSetPlugin = function(schema, options) {
// if it is a leaf node, the maxRgt should be the lft value of the parent
maxRgt = parentNode.lft;
}
- model.update({lft: { $gt: maxRgt}}, {$inc: {lft: 2}}, {multi: true}, function(err, updatedCount) {
- model.update({rgt: { $gt: maxRgt}}, {$inc: {rgt: 2}}, {multi: true}, function(err, updatedCount2) {
+ self.constructor.update({lft: { $gt: maxRgt}}, {$inc: {lft: 2}}, {multi: true}, function(err, updatedCount) {
+ self.constructor.update({rgt: { $gt: maxRgt}}, {$inc: {rgt: 2}}, {multi: true}, function(err, updatedCount2) {
self.lft = maxRgt + 1;
self.rgt = maxRgt + 2;
next();
@@ -66,7 +65,6 @@ var NestedSetPlugin = function(schema, options) {
schema.pre('remove', function(next) {
var self = this;
- var model = mongoose.model(this.constructor.modelName);
if (self.parentId) {
self.parent(function(err, parentNode) {
if (!err && parentNode && parentNode.lft && parentNode.rgt) {
@@ -84,8 +82,8 @@ var NestedSetPlugin = function(schema, options) {
// if it is a leaf node, the maxRgt should be the lft value of the parent
maxRgt = parentNode.lft;
}
- model.update({lft: { $gt: maxRgt}}, {$inc: {lft: -2}}, {multi: true}, function(err, updatedCount) {
- model.update({rgt: { $gt: maxRgt}}, {$inc: {rgt: -2}}, {multi: true}, function(err, updatedCount2) {
+ self.constructor.update({lft: { $gt: maxRgt}}, {$inc: {lft: -2}}, {multi: true}, function(err, updatedCount) {
+ self.constructor.update({rgt: { $gt: maxRgt}}, {$inc: {rgt: -2}}, {multi: true}, function(err, updatedCount2) {
next();
});
});
@@ -159,13 +157,13 @@ var NestedSetPlugin = function(schema, options) {
// returns the parent node
schema.method('parent', function(callback) {
var self = this;
- mongoose.model(self.constructor.modelName).findOne({_id: self.parentId}, callback);
+ self.constructor.findOne({_id: self.parentId}, callback);
});
// Returns the list of ancestors + current node
schema.method('selfAndAncestors', function(callback) {
var self = this;
- mongoose.model(self.constructor.modelName).where('lft').lte(self.lft).where('rgt').gte(self.rgt).run(callback);
+ self.constructor.where('lft').lte(self.lft).where('rgt').gte(self.rgt).run(callback);
});
// Returns the list of ancestors
@@ -184,7 +182,7 @@ var NestedSetPlugin = function(schema, options) {
// Returns the list of children
schema.method('children', function(callback) {
var self = this;
- mongoose.model(self.constructor.modelName).find({parentId: self._id}, callback);
+ self.constructor.find({parentId: self._id}, callback);
});
// Returns the list of children + current node
@@ -202,7 +200,7 @@ var NestedSetPlugin = function(schema, options) {
// Returns the list of descendants + current node
schema.method('selfAndDescendants', function(callback) {
var self = this;
- mongoose.model(self.constructor.modelName).where('lft').gte(self.lft).where('rgt').lte(self.rgt).run(callback);
+ self.constructor.where('lft').gte(self.lft).where('rgt').lte(self.rgt).run(callback);
});
// Returns the list of descendants
@@ -221,7 +219,7 @@ var NestedSetPlugin = function(schema, options) {
// Returns the list of all nodes with the same parent + current node
schema.method('selfAndSiblings', function(callback) {
var self = this;
- mongoose.model(self.constructor.modelName).find({parentId: self.parentId}, callback);
+ self.constructor.find({parentId: self.parentId}, callback);
});
// Returns the list of all nodes with the same parent

0 comments on commit 0e7f4bb

Please sign in to comment.
Something went wrong with that request. Please try again.