0

I have a single global object I use. I know the cons of using global objects but in this case I want to use it.

I call this global object the object pipe b.c. it branches my models to my controller and vice versa...maybe should have called it object branch...anyways...

The mistake I made is that I thought I only had one model running at any given time...but I don't, there are multiple.

Hence I can't use a single static implementation, I need and instance based one, one global object pipe per each model running.

Here is the static versions. MC stands for Model/Controller.

/********************************************************************************************
 *
 * MC - Model/Controller Types
 *
 *******************************************************************************************/

var MC = {};

/**
 **  Object Pipe
 */

MC.o_p = {
    model  :  'default',
    result :  'continue',
    page   :  {},
    args   :  {},
    server :  {},
    hash   :  localStorage.hash
};

I thought about doing something like this:

MC.o_p1 = function() {
    return {
        model  :  'default',
        result :  'continue',
        page   :  {},
        args   :  {},
        server :  {},
        hash   :  localStorage.hash
        }
}

but now the return object is in local scope to whatever called it.

I need global instance based objects.

I'm not sure if I'm over thinking this or what I'm asking is possible?

3 Answers3

3

Hold your package privately and just have some access functions:

var myModel = (function() {

    var model_vars = {
        model: 'default',
        result: 'continue',
        page: {},
        args: {},
        server: {},
        hash: localStorage.hash
    };

    return function() {
        this.get = function(ele) {
            if (model_vars.hasOwnProperty(ele)) {
                return model_vars[ele];
            }

        };

        this.set = function(ele, value) {
            if (model_vars.hasOwnProperty(ele)) {
                return model_vars[ele] = value;
            }

        };

    };

})();

Then you can just do:

Model = new myModel();

DEMO: http://jsfiddle.net/maniator/PSsQ3/

Naftali
  • 144,921
  • 39
  • 244
  • 303
1

You can pass in the global scope and use it when you need it, something like this:

MC.o_p1 = function(global) {
    return {
        model  :  'default',
        result :  'continue',
        page   :  {},
        args   :  {},
        server :  {},
        hash   :  global.localStorage.hash
        }
}(window);
Mitya
  • 33,629
  • 9
  • 60
  • 107
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • How does that effect the one line return statement? –  Jul 30 '12 at 21:44
  • window is global in browsers -- oh wait is this node.js? – Hogan Jul 30 '12 at 21:44
  • @HiroProtagonist - it does not -- the function is called before it is assigned. – Hogan Jul 30 '12 at 21:45
  • so the return statement will still go to what called it....how do I pop these objects out into the global space? –  Jul 30 '12 at 21:49
  • is it possible to do such a thing? –  Jul 30 '12 at 21:52
  • This works exactly like your "static" version. Except every time you want to access the global space inside the creation of `o_p1` you prefix with `global.` You could also modify this to cache the object and then if it has already been created return the cached item instead of the new one. – Hogan Jul 30 '12 at 21:53
  • MC.o_p1 should already have access to the window object...you just optimized it using global import. –  Jul 30 '12 at 21:59
  • What exactly is your question? You complained about not having the global namespace available -- I showed you how to pass it in to your creation and make global access explicit (all good things). Now you say I was optimizing - which I wasn't. What exactly are you trying to do that my answer or Neal's does not solve? – Hogan Jul 30 '12 at 22:16
1
var msg = 'window is global in browsers';

window.alert(msg);

alert('or we can just use alert without accessing from window because, '+msg);

function isWindowReallyGlobalInBrowsers(){
    window.newMsg = 'Did you see var newMsg get declared anywhere?';
    alert(newMsg + ' It works because ' +msg);
}

isWindowReallyGlobalInBrowsers();

Try it in a browser console. Ask questions as needed.

Erik Reppen
  • 4,605
  • 1
  • 22
  • 26
  • Assigning as a property of window makes it global in any context. And yes, msg was defined in the global context so it's accessible from anywhere. You could alert window.msg and get msg. – Erik Reppen Jul 31 '12 at 00:08
  • JS is not only not block-scoped but it's function/constructor scoped and everything from parent context is accessible to an inner function. You can alter stuff in an outer-scope. – Erik Reppen Jul 31 '12 at 00:12