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?
ArangoDB 2.7 replaced the ES5-style constructors-with-prototypes to ES6 classes. This change introduced a number of subtle bugs when using the extend
method, which is why it was reverted in a subsequent bugfix release.
The current version of ArangoDB is 2.7.4. Could you try upgrading and see whether that solves your problem?
EDIT: I can reproduce the error in the latest ArangoDB 2.7 and 2.8. Seems like there was insufficient test coverage and the bug slipped through. Thanks for telling us. The problem will be fixed in the next bugfix releases.
In the meantime you can manually bind the events for each repository instance like so:
repo.on('beforeSave', Model.beforeSave.bind(Model));