0

Following this link I have created a namespace for my object with:

var Namespace =
{
  Register : function(_Name)
  {
    var chk = false;
    var cob = "";
    var spc = _Name.split(".");
    for(var i = 0; i<spc.length; i++)
    {
      if(cob!=""){cob+=".";}
      cob+=spc[i];
      chk = this.Exists(cob);
      if(!chk){this.Create(cob);}
    }
    if(chk){ throw "Namespace: " + _Name + " is already defined."; }
  },

  Create : function(_Src)
  {
    eval("window." + _Src + " = new Object();");
  },

  Exists : function(_Src)
  {
    eval("var NE = false; try{if(" + _Src + "){NE = true;}else{NE = false;}}catch(err){NE=false;}");
    return NE;
  }
}

define the object:

Namespace.Register("System.Classes.HelloWorld"); 

System.Classes.HelloWorld = function(aa){ return {

    Message : "Hello World!",

    Hello : function(a,b)
    {
        alert(this.Message + a + b);
    }

};}

How can I pass in jQuery NoConflict to this object and allow me to use the $ sign?

For example this article creates a similar object, only passes in jQuery NoConflict in a way I am unable to apply to the above code.

This code wraps jQuery NoConflict and allows dollar sign which I'd like to replicate:

(function( skillet, $, undefined ) {
    //Private Property
    var isHot = true;

    //Public Property
    skillet.ingredient = "Bacon Strips";

    //Public Method
    skillet.fry = function() {
        var oliveOil;

        addItem( "\t\n Butter \n\t" );
        addItem( oliveOil );
        console.log( "Frying " + skillet.ingredient );
    };

    //Private Method
    function addItem( item ) {
        if ( item !== undefined ) {
            console.log( "Adding " + $.trim(item) );
        }
    }    
}( window.skillet = window.skillet || {}, jQuery ));
Gga
  • 4,311
  • 14
  • 39
  • 74
  • You can do it the same way... what exactly is the problem? Maybe it helps if you explain *where* exactly you want to access jQuery. – Felix Kling Mar 26 '13 at 21:34
  • Inside the System.Classes.HelloWorld object. The idea being that the object automatically has access to jQuery $, so preferably we would pass in the No Conflict argument in the Namespace function itself. How would I do it the same way, by the way? I have tried adapting the lower code into the lower portion code into the middle potion code to no avail. – Gga Mar 27 '13 at 11:03

1 Answers1

0

You might like to try this pattern :

var Namespace = (function($) {
    //Private members
    var register = function(_Name) {
        //...
    };
    var create = function(_Src) {
        //...
    };
    var exists = function(_Src) {
        //...
    };

    //Expose public members
    return {
        register : register,
        create : create,
        exists : exists
    };
})(jQuery);

This not only allows all sorts of private members to be defined but also avoids the need to prefix internal function calls with Namespace. or this..

Beetroot-Beetroot
  • 18,022
  • 3
  • 37
  • 44
  • Thanks, I have taken the above and inserted the lines from the original Namespace function where you have commented //... but the code then throws - TypeError: Namespace.Register is not a function [Break On This Error] var a = System.Classes.HelloWorld(1); – Gga Mar 27 '13 at 11:25
  • It's `Namespace.register`. By convention, methods are not capitalized. – Beetroot-Beetroot Mar 27 '13 at 13:07
  • Nice, thanks. Just while your hat's in the ring so to speak, what do you make of my follow up question on this, if you have the time to read of course? http://stackoverflow.com/questions/15659283/most-elegent-way-to-create-a-namespace-class-type-structure-in-javascript cheers – Gga Mar 27 '13 at 13:14