What is the right way to wire together 2 javascript objects?

前端 未结 6 1902
梦如初夏
梦如初夏 2020-12-28 19:34

I\'m currently facing a conundrum: What is the right way to wire together 2 javascript objects?

Imagine an application like a text editor with several different fi

6条回答
  •  失恋的感觉
    2020-12-28 20:01

    Thanks for the insight. I ended up writing a simple JavaScript dependency injection utility. After debating for a while and your comments, it occured to me that DI was really the right answer because:

    1. It totally separated the concerns of wiring from the business logic while keeping the wiring logic close to the things being wired.
    2. It allowed me to generically provide a "you're all wired up" callback on my objects so that I could do a 3 phase initialization: instantiate everything, wire it all up, call everyone's callbacks and tell them they're wired.
    3. It was easy to check for dependency missing problems.

    So here's the DI utility:

    var Dependency = function(_name, _instance, _dependencyMap) {
        this.name = _name;
        this.instance = _instance;
        this.dependencyMap = _dependencyMap;
    }
    
    Dependency.prototype.toString = function() {
        return this.name;
    }
    
    CONCORD.dependencyinjection = {};
    
    CONCORD.dependencyinjection.Context = function() {
        this.registry = {};
    }
    
    CONCORD.dependencyinjection.Context.prototype = {
        register : function(name, instance, dependencyMap) {
            this.registry[name] = new Dependency(name, instance, dependencyMap);
        }, 
        get : function(name) {
            var dependency = this.registry[name];
            return dependency != null ? dependency.instance : null;
        },
    
        init : function() {
            YAHOO.log("Initializing Dependency Injection","info","CONCORD.dependencyinjection.Context");
            var registryKey;
            var dependencyKey;
            var dependency;
            var afterDependenciesSet = [];
            for (registryKey in this.registry) {
                dependency = this.registry[registryKey];
                YAHOO.log("Initializing " + dependency.name,"debug","CONCORD.dependencyinjection.Context");
    
                for(dependencyKey in dependency.dependencyMap) {
                    var name = dependency.dependencyMap[dependencyKey];
                    var instance = this.get(name);
                    if(instance == null) {
                        throw "Unsatisfied Dependency: "+dependency+"."+dependencyKey+" could not find instance for "+name;
                    }
                    dependency.instance[dependencyKey] = instance; 
                }
    
                if(typeof dependency.instance['afterDependenciesSet'] != 'undefined') {
                    afterDependenciesSet.push(dependency);
                }
            }
    
            var i;
            for(i = 0; i < afterDependenciesSet.length; i++) {
                afterDependenciesSet[i].instance.afterDependenciesSet();
            }
        }
    
    }
    

提交回复
热议问题