d4fseeker
d4fseeker

Reputation: 23

Strongloop add properties during boot

I am trying to add properties to a (previously created) model through a bootscript by using

model.getDataSource().definePropery(model_name , property , property_value)

While it seems that the model correctly accepts the property (I can see it via model.definition.properties[property] ) the API-Explorer and propably the full remote API does not update accordingly. Is there a method to force a refresh on those?

My goal is to have a plugin system which extends existing basic models with additional properties and methods, with the methods-part working flawlessly. I don't want to have to change the JSON file before boot so a dynamic approach would be very much preferred.

[EDIT] I did figure out how to add the method, but it looks so dirty-hacky that I'm afraid it will cause issues. After adding all properties, I overwrite the app's model with the modified model:

app.model(model , { dataSource: model.getDataSource() } )

Any thoughts on this hack?

Upvotes: 2

Views: 48

Answers (1)

Farid Nouri Neshat
Farid Nouri Neshat

Reputation: 30420

I believe without your workaround it should be fine, as it seems to be just the API explorer not refreshing. Other functionality should be untouched.

Looking at the loopback-component-explorer code, it refreshes on the modelRemoted and remoteMethodDisabled application events.

So it's not surprising that your workaround refreshes it:

app.model(model , { dataSource: model.getDataSource() } )

Because that causes a modelRemoted event being emitted.

Probably a safer way would be to just emit yourself:

app.emit('modelRemoted', model.sharedClss);

This won't involve all those machinery involved with using app.model. I searched on Github for usages of this event, and didn't find anything that would have problems with it. So I believe it should fix your problem for now till loopback fixes this.

In the mean time, I have filed a bug report for this at here, I suggest keep an eye on it.

Upvotes: 1

Related Questions