1

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?

gph
  • 1,045
  • 8
  • 25
  • Issued moved to github per Alan's answer to be addressed as a bug instead of a question. – gph Jan 22 '16 at 00:46

1 Answers1

1

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));
Alan Plum
  • 10,814
  • 4
  • 40
  • 57
  • Upgraded to 2.7.3 via brew which is the most recent version available there. Upgrading didn't resolve this issue (at least within my context) I don't see anything past 2.7.3 on the downloads page. – gph Jan 15 '16 at 02:35
  • @gph I've confirmed the bug and created an issue on GitHub: https://github.com/arangodb/arangodb/issues/1665 – Alan Plum Jan 19 '16 at 17:14