AST Macros: Just what will they be able to do?

kenny funisher at gmail.com
Fri Aug 17 20:36:40 PDT 2007


BCS wrote:
> Reply to Robert,
> 
>> lets' say the
>> coder has two checkboxes -- cb1 and cb2. They are D objects when the
>> website is being created by the servlet, that render as <input
>> type="checkbox"> or whatever. Now say the coder writes this:
>>
>> cb1.onChange = {
>> if(cb1.enabled)
>> cb2.enabled = true;
>> else
>> cb2.enabled = false;
>> }
>> cb2.onChange = {
>> if(cb2.enabled)
>> doSomethingOnTheServer();
>> }
>> Using AST macros, would there be any way for the program to figure out
>> at compile-time cb1s callback could be done entirely on the client
>> side via JavaScript or something while cb2's would require some AJAX
>> calls in some cases (which would then be generated automatically)? In
>> particular, it would need to know that everything within the callback
>> for cb1 refers to variables/state within a particular set those which
>> are available on the client).
> 
> I once wrote a code generate that attempts to build a network interface
> proxy class (given a D interface, generate client/server code to allow a
> client to exercise an object on one computer from another computer) It'
> was a nasty pile before I more or less gave up on it. However with
> __traits, it might be buildable at compile time. To boot, you might also
> be able to build something analogous that would wright the AJAX code to
> let the client side run from JavaScript.
> 
> I'm thinking you would use it like this
> 
> interface I {/*stuff*}
> class C : I {/*more stuff*/}
> 
> class MyPage : WebPage
> {
>     this()
>     {
>        
>         InsertCode(AJAX!(I).ClientAJAX());    // put in needed javascript
>         InsertCode(CustomCodeUsingAJAX);    // put in your  javascript
>         //...
>     }
> 
>     void BuildState() // called for each new page
>     {
>         auto o = AJAX!(I).Registered(new C());
>         InsertCode(o.Binding); // insert code to get right object on
> server side
>     }
> }
> 
> Assuming lots of functionality in the WebPage class, the AJAX template
> would generate:
>     client side AJAX (JavaScript) to call back to the server
>     server side (D) to handle it
>     code to keep track of object all around
> 
> It's a pipe dream for now, but not outside the range of the possible.
> 
> 

IMO, you will want to do it a bit different than that. (replying to the original post and this post).

Let's say I have a bunch of HTML. Now, a common example would be for this sort of thing a form where a textbox has a greyed out text that says "type your name here.." -- then onfocus, that text becomes none. onblur, if the value of field == '', then set it back to the text.

that requires knowing the default text (whether hard coded in the events, or the innerHTML of a hidden div)
the active css class (custom text, and appears selected)
the inactive css class (custom text, but appears deselected)
the default-text css class

also 3 events:
onfocus
onblur
onchange

The only experience I have with your sort of knowledge would be VB 6.0, so bear with me...

frm1.onfocus() {
	this.value = '';
	this.className = 'form-active';
}

frm1.onchange() {
	this.onblur();
}


frm1.onblur() {
	if(this.value == '') {
		this.value = this.default;
		this.className = 'form-default';
	} else if(this.value != this.default) {
		this.className = 'form-inactive';
	}
}

----------------------------

Now, why would I ever want to write all of that more than once? Like in VB, I would have to copy and paste that code to every textbox in the form (UGHHHHH!!!! I HATE VB) Wouldn't it be so much neater, tighter and otherwise more awesome if I had something like this:

.. some html ...
<div id="contain"><%textbox style: "width:198px" name: "fullname" default_text: "your name please..." onchange: "hideElement('contain')'" %></div>
... more html ...

----------------------------

You see, what good is making all that, when a generic template will do the same? It doesn't give you unique examples though... Let's say you wanted it so everything could be clicked on to make an event...

Since to do that, you would essentially have to recreate the DOM, you could just parse the HTML and put it in a dom. The parts that get clicked on turn into templates...

<%div id: "myuser-${User.uid}" onclick: "alert('${User.firstname} is gay')" class: "myclass" style: "background-color: #f43" %>HEy!!!</div>

That's super easy to do. Now, let's say you wanted to reference other javascript ids. Assume you have the $() function like firebug and prototype does...

<div id="showme" style="display:none">show me</div>
<%span onmouseover: "showElement($('showme'))" class: "lala" %>weird</span>

Your template parser could EASILY verify that the id 'showme' exists.

----------------------------

It just seems to me that a VB-like interface would only get you so far (taking into consideration that HTML with wysiwyg is hell), and a MVC-esque design with a smart template scheme could get you a lot further. Writing something like that would take much less time too -- maybe like 2 months or so, instead of a year, lol.

Just thoughts,
Kenny



More information about the Digitalmars-d mailing list