Skip to content

Instantly share code, notes, and snippets.

@ntamvl
Forked from soltrinox/restaurants.js
Created October 13, 2015 05:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ntamvl/32549f39d110c40cefb9 to your computer and use it in GitHub Desktop.
Save ntamvl/32549f39d110c40cefb9 to your computer and use it in GitHub Desktop.
Loopback : Model access non-related model via App object
'use strict';
var app = require('../../server/server');
module.exports = function(Restaurant, app) {
// Restaurant.validatesUniquenessOf('UUID', {message: 'UUID EXISTS'});
Restaurant.observe('before save', function setDefaultUsername(ctx, next) {
if (ctx.instance) {
if(!ctx.instance.created){
ctx.instance.created = Date.now();
}
ctx.instance.updated = Date.now();
}
next();
});
Restaurant.observe('loaded', function logQuery(ctx, next) {
console.log('RETURNING %j', ctx.instance);
next();
});
Restaurant.getChildren = function(id,cb) {
var filter = { include : [ 'DETAILS'] };
// get the restaurant
Restaurant.findById(id, filter, function(err, restaurant) {
if(err) {
console.log(err);
} else {
// get the Generics
var Rapp = Restaurant.app;
var GenericModel = Rapp.models.Generic;
GenericModel.find({ where: { UUID : id } }, function(err, generics) {
if(err) {
console.log(err);
} else {
restaurant.children = generics;
cb(null, restaurant);
}
});
}
});
};
Restaurant.remoteMethod('getChildren', {
accepts: [{arg: 'id', type: 'string'}],
returns: {arg: 'FULLOBJECT', type: 'object'},
http: {path:'/withChildren/:id', verb: 'get'}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment