loopback icon indicating copy to clipboard operation
loopback copied to clipboard

apply mixin to models dynamically

Open lokeshjain20 opened this issue 9 years ago • 11 comments

I want to apply a mixin to my models including built in models dynamically , i.e in some boot script , right now I am using modelname.mixin('') . This method only works if I have applied mixin property in any one of my model , but doesn't work if no model is given mixin property in model.json file.

How to resolve it?

lokeshjain20 avatar Jun 27 '17 06:06 lokeshjain20

Initialize mixin in server.js file

require(mixin)(app)

padelagani avatar Jun 27 '17 07:06 padelagani

hi @padelagani I did not get you should I give path to mixin where you have mentioned mixin and whattshould i give in app, and should i require it in a variable? Can you please be more clear

lokeshjain20 avatar Jun 27 '17 08:06 lokeshjain20

@lokeshjain20 Keep that line in server.js after - var app = loopback()

var app = loopback()
require(mixin)(app)

padelagani avatar Jun 27 '17 09:06 padelagani

@padelagani doing this gives me error - "mixin is not defined".

lokeshjain20 avatar Jun 27 '17 12:06 lokeshjain20

In mixin define your mixin name if it is from npm module else give path of that mixin

ganipadela avatar Jun 27 '17 13:06 ganipadela

giving path name , does not give any error , but still mixin does not get applied to my models , when I am doing model.mixin('mixinName') , in my boot script.

lokeshjain20 avatar Jun 28 '17 06:06 lokeshjain20

i got it working finally , instead of model.mixin , var mixin = "/path of mixin"; and then require(mixin)();

@padelagani thank you for help , can you please explain what this line :require(mixin)(app/model) actualy does? Till now I have only used require with file path or module name , but never gave two brackets after require , what does it do?

lokeshjain20 avatar Jun 29 '17 09:06 lokeshjain20

@lokeshjain20 It passes loopback app instance to that mixin.

() because that module might be function module so to initialize that we need to call in that way.

When module.exports, exports a function then we call in that way.

padelagani avatar Jun 29 '17 09:06 padelagani

@lokeshjain20 Can you close this issue or mark is at done

padelagani avatar Jun 29 '17 09:06 padelagani

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 28 '17 10:08 stale[bot]

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

stale[bot] avatar Sep 11 '17 10:09 stale[bot]