Last active
August 29, 2015 14:02
-
-
Save feelinc/89c84e613a0e1ff3701d to your computer and use it in GitHub Desktop.
REST API Resource Template Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Module dependencies. | |
*/ | |
var async = require('async') | |
, countryTemplate = require('./country') | |
, _ = require('underscore'); | |
function aname(models, model, options, callback) { | |
if ( ! _.isObject(model)) { | |
if (typeof callback != 'undefined') { | |
callback(new Error('Model is undefined')); | |
} else { | |
return null; | |
} | |
} | |
if (typeof options == 'function') { | |
callback = options; | |
options = {}; | |
} | |
options = _.extend(options, defaultOptions); | |
var self = this; | |
Object.defineProperty(this, '__data', { | |
writable: true, | |
enumerable: false, | |
configurable: true, | |
value: {} | |
}); | |
function setAttribute(field, value) { | |
if (typeof value == 'undefined') { | |
if ( ! _.has(model.__data, field)) { | |
self.__data[field] = null; | |
} else { | |
self.__data[field] = model.__data[field]; | |
} | |
} else { | |
self.__data[field] = value; | |
} | |
} | |
/* Define each resource attribute */ | |
async.waterfall([ | |
function(done) { | |
setAttribute('id', model.uniqueCode); | |
done(null); | |
}, | |
function(done) { | |
setAttribute('slug'); | |
done(null); | |
}, | |
function(done) { | |
setAttribute('name'); | |
done(null); | |
}, | |
function(done) { | |
models.countries.findOne({ | |
where: {code: model.countryCode} | |
}, function(err, country) { | |
if (err) { | |
setAttribute('country', null); | |
return done(null); | |
} | |
countryTemplate(country, function(err, countryTemplate) { | |
if (err) { | |
setAttribute('country', null); | |
} else { | |
setAttribute('country', countryTemplate); | |
} | |
done(null); | |
}); | |
}); | |
} | |
], function() { | |
return callback(null, self.__data); | |
}); | |
return self.__data; | |
} | |
/** | |
* Expose `aname`. | |
*/ | |
module.exports = aname; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Module dependencies. | |
*/ | |
var _ = require('underscore'); | |
var defaultOptions = {}; | |
function aname(models, model, options, callback) { | |
if ( ! _.isObject(model)) { | |
if (typeof callback != 'undefined') { | |
callback(new Error('Model is undefined')); | |
} else { | |
return null; | |
} | |
} | |
if (typeof options == 'function') { | |
callback = options; | |
options = {}; | |
} | |
options = _.extend(options, defaultOptions); | |
var self = this; | |
Object.defineProperty(this, '__data', { | |
writable: true, | |
enumerable: false, | |
configurable: true, | |
value: {} | |
}); | |
function setAttribute(field, value) { | |
if (typeof value == 'undefined') { | |
if ( ! _.has(model.__data, field)) { | |
self.__data[field] = null; | |
} else { | |
self.__data[field] = model.__data[field]; | |
} | |
} else { | |
self.__data[field] = value; | |
} | |
} | |
/* Define each resource attribute */ | |
setAttribute('id', model.uniqueCode); | |
setAttribute('name'); | |
setAttribute('slug'); | |
return self.__data; | |
} | |
/** | |
* Expose `aname`. | |
*/ | |
module.exports = aname; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Module dependencies. | |
*/ | |
var apiTemplates = require('./templates'); | |
/* Use the simple one */ | |
res.send(apiTemplates.aname(model)); | |
/* Use the async one */ | |
res.send(apiTemplates.aname(models, model, function(err, anameTemplate) { | |
res.send(anameTemplate); | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.country = require('./country'); | |
module.exports.city = require('./city'); | |
module.exports.aname = require('./aname'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment