3.2.0

Javascript Novice flailing helplessly

I've been running into issues lately when trying to build custom cell templates that attempting to pass functions as parameters. I suspect this betrays my fundamental lack of understanding of javascript scopes and the this keyword. I have not had any luck on the net, as the stuff in this project is far more advanced than any JS tutorial i've seen on the net. I suppose I could risk my limited cash on a book, but in this day and age???

What I am trying to do here in the attached code is to create a template that contains a checkbox input, a hidden input, and a displayed label. and have the label default to the grid cells item text property from the corresponding model element.
fn(){return this.getItemProperty("text")}

I run into this problem all the time, I presumee b/c I just don't understand what is going on. My code fails from the line

label.setContent("text", function() { return this.getItemProperty("text"); });


If anyone can enlighten me (JS links, advice, recommended carreer changes;) I would really appreciate it. This same scenario seems to work sometimes, and not others, and I don't see the difference.

Thanks,
Here is the full code

Active.Templates.Checkbox = Active.System.Template.subclass();

Active.Templates.Checkbox.create = function(){

/****************************************************************
    Checkbox Cell template.
*****************************************************************/
    var obj = this.prototype;
    var _super = this.superclass.prototype;

    obj.setTag("div");
    obj.setClass("templates","input");

    obj.defineTemplate("checkbox", new Active.HTML.INPUT);
    obj.defineTemplate("hidden", new Active.HTML.INPUT);
    obj.defineTemplate("label", new Active.HTML.SPAN);


    var checkbox = obj.getCheckboxTemplate();
    checkbox.setClass("input","checkbox");
    checkbox.setClass("checkbox",function(){return this.getColumnProperty("index");});
    checkbox.setAttribute("type","checkbox");

    var hidden = obj.getHiddenTemplate();
    hidden.setAttribute("type","hidden");

    var label = obj.getLabelTemplate();
    label.setClass("checkbox","label");
    label.setContent("text", function() { return this.getItemProperty("text"); });
    obj.setLabel = function( l ) {
        var lbl = this.getLabelTemplate();
        lbl.setContent( "text", l );
    }

    obj.setContent("checkbox", checkbox );
    obj.setContent("hidden", function() { return this.getHiddenTemplate(); } );
    obj.setContent("label", function() { return this.getLabelTemplate() });

};

Active.Templates.Checkbox.create();
gbegley
May 6,
I would rather use the static content here instead of templates:
var My = new Object;

My.Templates = new Object;

My.Templates.Checkbox = Active.System.Template.subclass();

My.Templates.Checkbox.create = function(){

    var obj = this.prototype;

    obj.setClass("templates","input");

    var checkbox = new Active.HTML.INPUT;
    checkbox.setClass("input","checkbox");
    checkbox.setClass("checkbox",function(){return this.getColumnProperty("index");});
    checkbox.setAttribute("type","checkbox");

    var hidden = new Active.HTML.INPUT;
    hidden.setAttribute("type","hidden");

    var label = new Active.HTML.SPAN;
    label.setClass("checkbox","label");
    label.setContent("text", function() { return this.getItemProperty("text"); });

    obj.setContent("checkbox", checkbox );
    obj.setContent("hidden", hidden);
    obj.setContent("label", label);

};

My.Templates.Checkbox.create();

var obj = new Active.Controls.Grid;
...
obj.setColumnTemplate(new My.Templates.Checkbox, 1);
...


The issue 'static content' vs 'dynamic templates' probably requires more explanations - I will try to write more on this in a few days.

There is one important difference in behavior which you already noticed - keyword 'this' always refers to template (or control) even when used in a function attached to inner static content of this template (control). This is not feature of JS - this is how I decided to run those functions (i.e. as method of template).

I use call() method of the Function object to execute the function as a method of an arbitrary object:
myFunction.call(myObject, arg1, arg2 ...);


In this case keyword 'this' inside the myFunction will refer to myObject regardless where myFunction was originally defined or stored.
Alex (ActiveWidgets)
May 6,
Recommended reading ;-)

http://w3future.com/html/stories/hop.xml
http://w3future.com/html/stories/callbacks.xml
http://www.crockford.com/#javascript
http://www.crockford.com/javascript/jslint.html (read source code!)
http://www.jibbering.com/faq/faq_notes/closures.html
Alex (ActiveWidgets)
May 6,
Thank you very much. Your way not only works but seems a much cleaner implementation. I look forward to a static versus dynamic post, and will occupy myself with the referenced articles until then.

Thanks again for the very well written and easily extensible library.
gbegley
May 7,

This topic is archived.

See also:


Back to support forum