Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

It's in context of PureMVC Javascript port (www.puremvc.org) but it can be a general question as well.

Under Inheritance, when a child object extends the parent, the parent needs to be constructed first before the child can be created.

However, one of the properties of parent constructor is async (webSQL database transaction). I want to share the database transaction object to all of it's children.

But then the problem is children can't use the transaction object right away because it gets created some time later asynchronously after the parent creation.

Parent:

    if(this.database = common.model.connections.Sync.getConnection()) {
        this.database.transaction(function(transaction){
            self.transaction = transaction;
        });
    }

Please advise or ask me for more details. Will appreciate any design patterns.

share|improve this question
    
Not sure if I entirely understand your question, but I think you're looking for Promises: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… –  Nick Dugger Sep 3 at 16:37
    
thanks @NickDugger for the attempt, it's new for me, but won't be able to use since it won't work across browsers, but it seems similar to what I'm thinking. i.e. Pass a callback method (let's say startup) of the child object to the parent object in the constructor, and then parent will call the callback once the async property (database transaction) gets created. –  user2727195 Sep 3 at 16:43
    
There are polyfills for Promises. I highly suggest you give it a try –  Nick Dugger Sep 3 at 17:42
    
What I understood from my brief research that it's to reduce nesting of callbacks and make the code more organized, but I feel my question is more architecture related, even if you forget for a minute that we are in javascript, let's say a parent's property get's created asynchronously, so basically child is not fully complete right away, it also means the parent has to know the child. –  user2727195 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.