I'm using node.js, RailwayJS and JugglingDB.
I have a model:
Model.afterInitialize = function() {
var me = this;
Sequence.getSequence('forModel', function(sequence) {
me.serialId = sequence;
});
me.title = 'Hello';
}
Once it's all done only the attribute title
is set. This isn't surprising but I cannot get it to work. I tried using the async
module with no luck.
added a callback parameter to be able to chain.
Model.afterInitialize = function(then)
{
then = then || function(me) { };
var me = this;
Sequence.getSequence('forModel', function(sequence)
{
me.serialId = sequence;
me.title = 'Hello';
then.call(null, me);
});
};
If Sequence.getSequence
is asynchronous, the serialId
will be set from within the callback, when it is invoked. Since it's asynchronous, that's probably after Model.afterInitialize
returns. If you have to perform operations that depend on the serialId
being set, fire them from the Sequence.getSequence
callback.
Model.afterInitialize
is part of an third-party API that I cannot change, so unfortunately there is no way to do this. I was hoping some sort of infinite loop checking for a flag would work but it doesn't and it would be a bodge even if it did work.
I ended up hooking into another method Model.beforeSave = function(callback) {};
, which requires a callback.
FYI I was using https://github.com/1602/jugglingdb