Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Modified my previous pull requests commit #103

Merged
merged 2 commits into from

2 participants

@tanertopal

Modified my previous pull requests commit. I hope this version is better. I still have to write a test for it but maybe someone else does it first.

Taner Topal added some commits
@anatoliychakkaev anatoliychakkaev merged commit 752e711 into 1602:master
@anatoliychakkaev
Collaborator

Nice stuff. Merged! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 21, 2012
  1. Implemented destroyAll for sub-scopes

    Taner Topal authored
Commits on Jul 13, 2012
  1. modified destroyAll

    Taner Topal authored
This page is out of date. Refresh to see the latest.
Showing with 29 additions and 8 deletions.
  1. +29 −8 lib/abstract-class.js
View
37 lib/abstract-class.js
@@ -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();
@@ -94,7 +94,7 @@ function AbstractClass(data) {
}
this.trigger("initialize");
-};
+}
AbstractClass.setter = {};
AbstractClass.getter = {};
@@ -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.
@@ -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));
});
@@ -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) {
@@ -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
@@ -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) {
Something went wrong with that request. Please try again.