Merge branch 'feature/changed-method'

This commit is contained in:
Ritchie Martori
2012-10-24 10:26:30 -07:00
7 changed files with 79 additions and 5 deletions

View File

@@ -96,6 +96,28 @@ Prevents a property from being updated.
// Example: On Put
// Protect a property
protect('createdDate');
### changed()
changed(property)
Returns whether a property has been updated.
// Example: On Put
// Validate the title when it changes
if(changed('title') && this.title.length < 5) {
error('title', 'must be over 5 characters');
}
### previous
An `Object` containing the previous values of the item to be updated.
// Example: On Put
if(this.votes < previous.votes) {
emit('votes have decreased');
}
### emit()

View File

@@ -470,13 +470,20 @@ Collection.prototype.save = function (ctx, fn) {
protect: function(property) {
delete item[property];
},
changed: function (property) {
if(item.hasOwnProperty(property)) return true;
return false;
},
'this': item,
data: item
data: item,
previous: {}
};
function put() {
var id = query.id
, sanitizedQuery = collection.sanitizeQuery(query);
, sanitizedQuery = collection.sanitizeQuery(query)
, prev = {};
store.first(sanitizedQuery, function(err, obj) {
if(!obj) {
if (Object.keys(sanitizedQuery) === 1) {
@@ -489,12 +496,14 @@ Collection.prototype.save = function (ctx, fn) {
// merge changes
Object.keys(item).forEach(function (key) {
prev[key] = obj[key];
obj[key] = item[key];
});
item = obj;
domain['this'] = item;
domain.data = item;
domain.previous = prev;
collection.execCommands('update', item, commands);

View File

@@ -13,7 +13,6 @@ function Script(src, path) {
} catch(ex) {
this.error = ex;
}
}
/**
@@ -147,6 +146,7 @@ function wrapAsyncFunctions(asyncFunctions, sandbox, events, done, sandboxRoot)
var args = _.toArray(arguments);
var callback;
var callbackIndex;
var result;
for(var i = 0; i < args.length; i++) {
if(typeof args[i] == 'function') {
@@ -161,7 +161,7 @@ function wrapAsyncFunctions(asyncFunctions, sandbox, events, done, sandboxRoot)
args[callbackIndex] = function() {
if (sandboxRoot._error) return;
try {
callback.apply(sandboxRoot._this, arguments);
result = callback.apply(sandboxRoot._this, arguments);
events.emit('finishCallback');
} catch (err) {
var wrappedErr = wrapError(err);
@@ -176,12 +176,16 @@ function wrapAsyncFunctions(asyncFunctions, sandbox, events, done, sandboxRoot)
});
}
try {
asyncFunctions[k].apply(sandboxRoot._this, args);
result = asyncFunctions[k].apply(sandboxRoot._this, args);
} catch(err) {
var wrappedErr = wrapError(err);
sandbox._error = wrappedErr;
return done(wrappedErr);
}
if(result !== undefined) {
return result;
}
};
} else if (typeof asyncFunctions[k] === 'object') {
sandbox[k] = sandbox[k] || {};

View File

@@ -794,5 +794,23 @@ describe('Collection', function() {
});
});
describe('changed()', function(){
it('should detect when a value has changed', function(done) {
dpd.changed.post({name: 'original'}, function (c) {
dpd.changed.put(c.id, {name: 'first name change'}, function (c) {
if(c.name !== 'saw first name changed previous original') {
throw Error('missed name change');
}
done();
});
});
});
afterEach(function (done) {
this.timeout(10000);
cleanCollection(dpd.changed, done);
});
});
});

View File

@@ -0,0 +1,13 @@
{
"type": "Collection",
"properties": {
"name": {
"name": "name",
"type": "string",
"typeLabel": "string",
"required": false,
"id": "name",
"order": 0
}
}
}

View File

@@ -0,0 +1,3 @@
if(!(changed('name') && previous.name === undefined)) {
cancel('changed and/or previous are broken...');
}

View File

@@ -0,0 +1,5 @@
if(this.name === 'first name change') {
if(changed('name')) {
this.name = 'saw first name changed previous ' + previous.name;
}
}