Merge pull request #103 from Katoka/master

Modified my previous pull requests commit
This commit is contained in:
Anatoliy Chakkaev 2012-07-14 05:11:06 -07:00
commit 752e711bca
1 changed files with 29 additions and 8 deletions

View File

@ -103,7 +103,7 @@ AbstractClass.prototype._initProperties = function (data, applySetters) {
}.bind(this)); }.bind(this));
function getDefault(attr) { function getDefault(attr) {
var def = properties[attr]['default'] var def = properties[attr]['default'];
if (isdef(def)) { if (isdef(def)) {
if (typeof def === 'function') { if (typeof def === 'function') {
return def(); return def();
@ -116,7 +116,7 @@ AbstractClass.prototype._initProperties = function (data, applySetters) {
} }
this.trigger("initialize"); this.trigger("initialize");
}; }
AbstractClass.setter = {}; AbstractClass.setter = {};
AbstractClass.getter = {}; AbstractClass.getter = {};
@ -419,7 +419,7 @@ AbstractClass.count = function (where, cb) {
*/ */
AbstractClass.toString = function () { AbstractClass.toString = function () {
return '[Model ' + this.modelName + ']'; return '[Model ' + this.modelName + ']';
} };
/** /**
* Save instance. When instance haven't id, create method called instead. * Save instance. When instance haven't id, create method called instead.
@ -537,7 +537,7 @@ AbstractClass.prototype.destroy = function (cb) {
this._adapter().destroy(this.constructor.modelName, this.id, function (err) { this._adapter().destroy(this.constructor.modelName, this.id, function (err) {
removeFromCache(this.constructor, this.id); removeFromCache(this.constructor, this.id);
destroyed(function () { destroyed(function () {
cb && cb(err); if(cb) cb(err);
}); });
}.bind(this)); }.bind(this));
}); });
@ -731,7 +731,7 @@ AbstractClass.belongsTo = function (anotherClass, params) {
var fk = params.foreignKey; var fk = params.foreignKey;
this.schema.defineForeignKey(this.modelName, fk); 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) { this.prototype['__finders__'][methodName] = function (id, cb) {
anotherClass.find(id, function (err,inst) { anotherClass.find(id, function (err,inst) {
@ -742,7 +742,7 @@ AbstractClass.belongsTo = function (anotherClass, params) {
cb(new Error('Permission denied')); cb(new Error('Permission denied'));
} }
}.bind(this)); }.bind(this));
} };
this.prototype[methodName] = function (p) { this.prototype[methodName] = function (p) {
if (p instanceof AbstractClass) { // acts as setter if (p instanceof AbstractClass) { // acts as setter
@ -838,8 +838,29 @@ function defineScope(cls, targetClass, name, params, methods) {
this.build(data).save(cb); 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) { function mergeParams(base, update) {