Opened 9 years ago

Closed 9 years ago

#12089 closed enhancement (fixed)

Add option to parser to provide the parser with the scope in which to evaluate JSON attributes

Reported by: mgottli Owned by: bill
Priority: high Milestone: 1.6
Component: Parser Version: 1.5
Keywords: Cc:
Blocked By: Blocking:

Description (last modified by bill)

When using widgets within a templated widget, there is no way to pass the child widget an object or function from the parent templated widget object. The object or function must be in the global scope. For example, the dojox.Grid can take a getter for columns. It would be nice to pass in a function in the parent templated widget. Another example is for widgets that take a store. It would be nice to be able to define a store within the constructor of the class as a class field but then assign the store to a child widget within the template HTML using an attribute.

The reason this doesn't work is that the JSON, function, or variable passed in an attribute is evaluated by calling eval() in the global scope. If it was possible to pass in the scope to use to the parser, the _Templated class could assign this scope to itself in buildRendering.

Attached are modified parser.js and _Templated.js (from version 1.5.0) files that make this work. The changes are minimal.

  • parser.js: Line 27 - Added scope param to str2obj
  • parser.js: Line 67 - Added .call(scope,... to d.fromJson call
  • parser.js: Line 212 - Added scope to call to str2obj
  • parser.js: Line 312 - Added scope to parser args comment
  • _Templated.js: Line 124 - Added scope to parser call

Attachments (2)

parser.js (15.3 KB) - added by mgottli 9 years ago.
_Templated.js (10.9 KB) - added by mgottli 9 years ago.

Download all attachments as: .zip

Change History (7)

Changed 9 years ago by mgottli

Attachment: parser.js added

Changed 9 years ago by mgottli

Attachment: _Templated.js added

comment:1 Changed 9 years ago by bill

Hi, are you talking about having a template like:

<div>
     <button dojoType=dijit.form.Button dojoAttachPoint=myButton dojoAttachEvent="onClick: onButtonClick">
            click m
    </button>
</div>

and then your main widget defines the onButtonClick handler like this:

onButtonClick: function(){
    console.log(this.id + ": button was clicked");
}

comment:2 Changed 9 years ago by mgottli

No, I understand that can be done already. I'm referring to having a template like:

<div>
    <input dojoType="dijit.form.FilteringSelect" store="this.myStore">
</div>

and then the widget class defines:

    constructor: function () {
         this.myStore = new dojo.data.ItemFileReadStore({ ... });
    }

Here is another example. This one has a function in a template:

<div>
    <input dojoType="dijit.form.ValidationTextBox" regExpGen="this.myExpGen">
</div>

and then the widget class defines:

    myExpGen: function (/*dijit.form.ValidationTextBox.__Constraints*/constraints) {
         return this.requiredPrefix + ".*";
    }

comment:3 Changed 9 years ago by bill

Description: modified (diff)
Milestone: tbd1.6

I see. It would probably make more sense to support that with the new parser syntax:

<div>
    <input data-dojo-type="dijit.form.FilteringSelect"
                 data-dojo-props="store: this.myStore">
</div>

Unfortunately I recently added another parameter to the parser called "scope", for a different function, maybe I'll have to change that name. OTOH "scope" is a bit ambiguous anyway, seems like "scope" could refer either to what "this" points to, or to somewhere to look for dijit.form.FilteringSelect? before/instead of the global scope.

On a side note, the other useful thing would be for attributeMap to work when connected to widgets in templates.

For the future, please file a CLA before submitting patches and then send them as a unified patch file.

comment:4 Changed 9 years ago by bill

Status: newassigned

comment:5 Changed 9 years ago by bill

Resolution: fixed
Status: assignedclosed

(In [23490]) For widgets in templates, allow syntax like data-dojo-props="foo: this.bar" so that sub widgets can reference objects in the parent. Fixes #12089 !strict.

Obviously the dojo.toJson() call in the parser should be changed to dojo.eval() once we have that,

Note: See TracTickets for help on using tickets.