I'm exporting a model as in the below:
var Foxx = require("org/arangodb/foxx");
var myNewModel = Foxx.extend(
{ schema:{...} },
{
beforeSave: function() {
throw new Error('reached before save');
}
});
And using it in a controller as in:
var FoxxRepo = require("org/arangodb/foxx").Repository;
...
app.POST(function(req, resp) {
var instance = new myNewModel({...schemadata...});
var repo = new FoxxRepo(collection, { model: myNewModel });
repo.save(instance);
}
...
The only way I can get the beforeSave model event to respond to the repository event is by registering the function with the model instance via instance.on(...) before passing the instance to the repo.
There are some threads on this discussion, but they appear to date from when adding event registration was just getting underway. The documention, here, has an example showing event registration as I've shown here. My server version is 2.7.1.
Is there a way to add event handlers to a foxx model in the definition file and have the handlers included in the instance such that they listen for repository events or must I manually add all the handlers via model.on() each time I create a new data model instance?