Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

destroyAll for SubScopes #49

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions lib/abstract-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function AbstractClass(data) {
}.bind(this));

function getDefault(attr) {
var def = properties[attr]['default']
var def = properties[attr]['default'];
if (isdef(def)) {
if (typeof def === 'function') {
return def();
Expand All @@ -94,7 +94,7 @@ function AbstractClass(data) {
}

this.trigger("initialize");
};
}

AbstractClass.setter = {};
AbstractClass.getter = {};
Expand Down Expand Up @@ -267,7 +267,7 @@ AbstractClass.count = function (where, cb) {

AbstractClass.toString = function () {
return '[Model ' + this.modelName + ']';
}
};

/**
* Save instance. When instance haven't id, create method called instead.
Expand Down Expand Up @@ -368,7 +368,7 @@ AbstractClass.prototype.destroy = function (cb) {
this._adapter().destroy(this.constructor.modelName, this.id, function (err) {
removeFromCache(this.constructor, this.id);
destroyed(function () {
cb && cb(err);
if(cb) cb(err);
});
}.bind(this));
});
Expand Down Expand Up @@ -510,7 +510,7 @@ AbstractClass.belongsTo = function (anotherClass, params) {
var fk = params.foreignKey;

this.schema.defineForeignKey(this.modelName, fk);
this.prototype['__finders__'] = this.prototype['__finders__'] || {}
this.prototype['__finders__'] = this.prototype['__finders__'] || {};

this.prototype['__finders__'][methodName] = function (id, cb) {
anotherClass.find(id, function (err,inst) {
Expand All @@ -521,7 +521,7 @@ AbstractClass.belongsTo = function (anotherClass, params) {
cb(new Error('Permission denied'));
}
}.bind(this));
}
};

this.prototype[methodName] = function (p) {
if (p instanceof AbstractClass) { // acts as setter
Expand Down Expand Up @@ -613,8 +613,29 @@ function defineScope(cls, targetClass, name, params, methods) {
this.build(data).save(cb);
}

function destroyAll(id, cb) {
// implement me
/*
Callback
- The callback will be called after all elements are destroyed
- For every destroy call which results in an error
- If fetching the Elements on which destroyAll is called results in an error
*/
function destroyAll(cb) {
targetClass.all(this._scope, function (err, data) {
if (err) {
cb(err);
} else {
(function loopOfDestruction (data) {
if(data.length > 0) {
data.shift().destroy(function(err) {
if(err && cb) cb(err);
loopOfDestruction(data);
});
} else {
if(cb) cb();
}
}(data));
}
});
}

function mergeParams(base, update) {
Expand Down