Home » Articles » Widget introduction » Hello!

Running the hello widget

So, we end up with a widget like so:

//  Create the namespace
var pax = pax || {};
pax.widget = pax.widget || {};
pax.widget.hello = pax.widget.hello || {};
   
//  "Controller" method
pax.widget.hello.init = function( target, args ) {
    var model = pax.widget.hello.model( target, args );
    pax.widget.init( {
        target: target,
        model: model,
        template: pax.widget.hello.template()
    } ).render();   // This renders the widget
};
   
//  "View" method
pax.widget.hello.template = function() {
    return "Hello [:= name :]";
};
   
//  "Model" method
pax.widget.hello.model = function( target, args ) {
    return pax.defaultArgs( {
        name: 'world'   // Default the name to 'world', if none is specified
    }, args );
};
We will run the widget below, by using some HTML, and javascript.
HTML:
<div id='widget_introduction_example1'></div>
Javascript:
pax.widget.hello.init( pax.$('widget_introduction_example1') );
Result:
We could also run it, with a name parameter, like so:
HTML:
<div id='widget_introduction_example2'></div>
Javascript:
pax.widget.hello.init( pax.$('widget_introduction_example2'), {
    name: 'Bartholemew'
} );
Result:
Read on for an ajax widget example!