2

I've got a javascript file as follows

(function (rpc) {

    rpc.loadHomeBanner = rpc_loadHomeBanner;
    function rpc_loadHomeBanner(){

        // AN is used by Sencha Animator as an Animation variable
        rpc.AN ={};

        rpc.AN.Controller = {

            setConfig: function(configData) {
                // update config crap
            }
        };

        var configData = {
           // config crap
        };

        rpc.AN.Controller.setConfig(configData);
    }
})(rpc);

Now on the very first load of the page, I call

rpc.loadHomeBanner(); 

and it fires up just as I need it to.

The problem is that I have a handleOrientationChange method that needs to update the config from outside the namespace (I don't want to fire the loadHomeBanner method because of overhead).

 handleOrientationChange: function(){
        // Updating the config for the animation to ensure appropriate width.
        var configData = {
            // config crap
        };
        rpc.AN.Controller.setConfig(configData);
    }

How can I call rpc.AN.controller.setConfig from outside the scope of the closure?

Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • 1
    You are passing `rpc` from the outside, so you should be able to just call `rpc.AN.controller.setConfig` from anywhere where `rpc` is visible. I don't see any advantage of the closure here actually. – Felix Kling Feb 13 '12 at 17:42
  • 1
    @FelixKling: I would hope this is not all the code in that module, Chase is showing just what's necessary? – Ruan Mendes Feb 13 '12 at 18:07

2 Answers2

1

It should already be accessible considering you've passed the rpc object into that code module.

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • hrm... I feel a little stupid. The error I was getting was being thrown inside the `setConfig` method. I should do a little more troubleshooting before asking stupid questions. – Chase Florell Feb 13 '12 at 17:47
0

You should create a factory.

var createRPC = function( rpc ) {
    rpc.AN = {
        Controller: {
            setConfig: function( configData ) {
            }
        }
    };

    return {
        updateConfig: function( configData ) {
            rpc.AN.Controller.setConfig( configData );
        };
    };
};

and then use it like

var rpc = createRPC({});

//handle orientation change
rpc.updateConfig({
    some: 'data'
});
BenMorel
  • 34,448
  • 50
  • 182
  • 322
jAndy
  • 231,737
  • 57
  • 305
  • 359
  • note his other question: http://stackoverflow.com/questions/9264349/whats-the-difference-between-these-two-approaches-to-namespacing/9264443#9264443 for the object form he is usings context. – Mark Schultheiss Feb 13 '12 at 23:00