Skip to content

Instantly share code, notes, and snippets.

@jfsiii jfsiii/logs.txt
Created Oct 15, 2012

Embed
What would you like to do?
resourceful after create hook
→ node no_args.js
BEFORE { id: null, resource: 'Resource', name: 'Attempt 1' }
httpRequest
trigger httpResponse
httpResponse
BEFORE { id: null, resource: 'Resource', name: 'Attempt 2' }
AFTER { id: null, resource: 'Resource', name: 'Attempt 2' }
→ node one_arg.js
BEFORE { id: null, resource: 'Resource', name: 'Attempt 1' }
httpRequest
trigger httpResponse
httpResponse
BEFORE { id: null, resource: 'Resource', name: 'Attempt 2' }
AFTER { id: null, resource: 'Resource', name: 'Attempt 2' }
→ node two_args.js
BEFORE { id: null, resource: 'Resource', name: 'Attempt 1' }
httpRequest
trigger httpResponse
httpResponse
BEFORE { id: null, resource: 'Resource', name: 'Attempt 2' }
var resourceful = require('resourceful');
var resource = resourceful.define('resource');
// set handlers
resource.before('create', beforeCreate);
resource.after('create', afterCreate);
resource.on('httpRequest', onHttpRequest);
resource.on('httpResponse', onHttpResponse);
// kick it off
resource.create({name: 'Attempt 1'});
function beforeCreate(values, next) {
console.log('BEFORE', values);
if (resource.someCondition) {
next();
}
else {
resource.emit('httpRequest');
}
}
function onHttpRequest() {
console.log('httpRequest');
setTimeout(function () {
console.log('trigger httpResponse');
resource.emit('httpResponse', {name: 'Attempt 2'});
}, 2000);
}
function onHttpResponse(data) {
console.log('httpResponse');
resource.someCondition = true;
resource.create(data);
}
function afterCreate(err, created) {
if (err) throw err;
console.log('AFTER', created);
resource.emit('create', created);
}
var resourceful = require('resourceful');
var resource = resourceful.define('resource');
// set handlers
resource.before('create', beforeCreate);
resource.after('create', afterCreate);
resource.on('httpRequest', onHttpRequest);
resource.on('httpResponse', onHttpResponse);
// kick it off
resource.create({name: 'Attempt 1'});
function beforeCreate(values, next) {
console.log('BEFORE', values);
if (resource.someCondition) {
next(null);
}
else {
resource.emit('httpRequest');
}
}
function onHttpRequest() {
console.log('httpRequest');
setTimeout(function () {
console.log('trigger httpResponse');
resource.emit('httpResponse', {name: 'Attempt 2'});
}, 2000);
}
function onHttpResponse(data) {
console.log('httpResponse');
resource.someCondition = true;
resource.create(data);
}
function afterCreate(err, created) {
if (err) throw err;
console.log('AFTER', created);
resource.emit('create', created);
}
var resourceful = require('resourceful');
var resource = resourceful.define('resource');
// set handlers
resource.before('create', beforeCreate);
resource.after('create', afterCreate);
resource.on('httpRequest', onHttpRequest);
resource.on('httpResponse', onHttpResponse);
// kick it off
resource.create({name: 'Attempt 1'});
function beforeCreate(values, next) {
console.log('BEFORE', values);
if (resource.someCondition) {
next(null, values);
}
else {
resource.emit('httpRequest');
}
}
function onHttpRequest() {
console.log('httpRequest');
setTimeout(function () {
console.log('trigger httpResponse');
resource.emit('httpResponse', {name: 'Attempt 2'});
}, 2000);
}
function onHttpResponse(data) {
console.log('httpResponse');
resource.someCondition = true;
resource.create(data);
}
function afterCreate(err, created) {
if (err) throw err;
console.log('AFTER', created);
resource.emit('create', created);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.