Reputation: 500
I have several loopback models and I want to add shared functions to them.
I'm not really sure if I understand loopback's extend
approach, but I created a Dictionary
model, set other models base
property to Dictionary and got stuck here.
Essentially I need to create an instance of child model, fetch some data and build a hash. Then I plan to call app.models.ChildModel.createHash() several times in the boot script to create dictionary for each of the models.
I'm not sure where to start.. how do I pass the child class name (or instance) to parent? I obviously need it to query the data.
I made a working solution for a concrete Regions
model
module.exports = function(Regions){
var countriesHash={};
Regions.setup=function(next){
Regions.find({where:{type:'Country'}}, function(err, countries){
countries.forEach(function(country){
countriesHash[country.id]=country;
});
console.log('hash built');
next();
});
};
but I want to refactor it to abstract Dictionary
class and just set a few properties like child name and where
in concrete models.
Upvotes: 0
Views: 887
Reputation: 9406
You can use mixin
(here) to put shared functionalities there and use in models.
Also you can send them some options.
Upvotes: 1