1

Hi I am hoping someone can help me here :) I am trying to setup a library similar in fashion to jQuery. Here is what I have so far.

/*!
 * Sentinal Javascript Library V1.0.0
 * http://sentinal.com
 *
 * Depends on jQuery v1.9.1
 *
 * Copyright 2013 Sentinal Team and other contributors
 * Released under license
 * http://sentinal.com/license
 *
 * Date 25-3-2013
 */ 
(function ( window, location, $, undefined ){

// Can't do this because several apps including ASP.NET trace
// the stack via arguments.caller.callee and Firefox dies if
// you try to trace through "use strict" call chains. (#13335)
// Support: Firefox 18+
//"use strict";
var 
    // A central reference to the root Sentinal(document)
    rootSentinal,

    // Use the correct document accordingly with window argument (sandbox)
    document = window.document,
    location = window.location,

    // Map over Sentinal in case of overwrite   
    _Sentinal = window.Sentinal,

    // Map over the $S alias in case of overwrite (Namespace alias for Sentinal)
    _$S = window.$S,

    // Version of this library 
    core_version = "1.0.0",

    // Define a local copy of Sentinal
    Sentinal = function() {
        // The Sentinal object is actually just the init constructor 'enhanced'
        return new Sentinal.fn.init();
    };

    Sentinal.fn = Sentinal.prototype = {
        // The current version of Sentinal being used
        Sentinal: core_version,

        constructor: Sentinal,

        init: function (){
                $(document).ready(function(){
                $('body').append('<h1>It Works!</h1>');
                console.log( "Sentinal Version is reporting : " + $S.fn.version() );
            });
        },

        version: function (){
            return core_version;
        },

        render: function (){

        },

        send: function () {

        },

        recieve: function () {

        }

    };



    // Give the init function the jQuery prototype for later instantiation
    Sentinal.fn.init.prototype = Sentinal.fn;

    window.Sentinal = window.$S = Sentinal;

})( window, location, jQuery );

The problem I have at the minute is either to do with my library not being aware of $(document).ready. Or a scoping issue but the init method that has some jQuery in it will not append the html to the body of the document and also the console.log is not working either.

Any information will be greatly received :)

Robert
  • 13
  • 2

1 Answers1

0

Anonymous functions do not always execute themselves. They are given a name by the function they are passed to (or accessed through the arguments object).

Your problem is you never call the init function, which means the $(document).ready() function is never called, and thus your anonymous function is never attached to the ready event of the document.

Try to remove the init function altogether and put your call to $(document).ready() at the end of your code like this:

. . .  

window.Sentinal = window.$S = Sentinal;

$(document).ready(function(){
            $('body').append('<h1>It Works!</h1>');
            console.log( "Sentinal Version is reporting : " + $S.fn.version() );
        });

// Give the init function the jQuery prototype for later instantiation
Sentinal.fn.init.prototype = Sentinal.fn;

})( window, location, jQuery );

Or, you can call your init function immediately like this:

        init: function (){
            $(document).ready(function(){
            $('body').append('<h1>It Works!</h1>');
            console.log( "Sentinal Version is reporting : " + $S.fn.version() );
        });
    }(); // call the function immediately,

Also, you don't need to pass the window object into your plugin, since it is the global object and therefore accessible anywhere and everywhere.

orb
  • 1,263
  • 1
  • 10
  • 16
  • OK I have added the code suggested waiting for document ready from jQuery and it works great :) – Robert Mar 26 '13 at 10:18
  • FYI I just found out that passing the window object optimizes minification: http://stackoverflow.com/questions/4665277/why-does-jquery-pass-the-window-object-into-their-scope – orb Apr 08 '13 at 14:40