Arbitrary abbreviations in phobos considered ridiculous

Adam D. Ruppe destructionator at gmail.com
Mon Mar 12 17:50:29 PDT 2012


On Tuesday, 13 March 2012 at 00:25:15 UTC, Jonathan M Davis wrote:
> But that's a decision based on your needs as a website 
> developer. If JS best suits whatever the needs of a particular 
> website developer are, then they are completely justified in 
> using it,
> because 99% of the people out there have it enabled in their 
> browsers.

If it takes ten seconds to support 100% of the people out there,
why not?

Though, in the majority of cases I've done, it actually
takes no extra time at all: use things like event delegation
and standard attributes.

For example, I was making a site and the client wanted a side
navigation added. Easy:

<div id="left-navigation">
   <a href="location">Name</a>
   <a href="location">Name</a>
</div>

That's the easiest way to do navigation... and it also happens
to work for everyone!

Then, he asked for a partial ajax load thing. Turns out that's
trivially easy too. On the client:

document.getElementById("left-navigation").addEventListener(
    "click", function(ev) {
         var element = getParent(ev.target, "a");
         if(!element) return;
         MySite.getPageContent(element.href).
             useToReplaceElement("page-content");
         ev.preventDefault();
    }, false);


And on the server:

Element getPageContent(string link) {
    auto document = run(link, "document");
    return document.requireElementById("page-content");
}



Boom. Now, I have the javascript he asked for, the standard
links for people who don't like that crap... and it takes no
extra work to add new links. It is just another standard link
tag.



So, then I have a comment thing kinda like reddit. I have
a "Comment" link that is supposed to show the form right
under.

<a class="comment-link">Comment</a>


On the server, I do most the work:

foreach(element; document.querySelectorAll(".comment"))
       element.href = "comment-form?parentId=" ~ 
encodeComponent(parentId);

Element commentForm(string parentId) {
    auto document = getDocument!"comment-form";
    auto form = document.requireElementById!Form("comment-form");
    form.setValue("parentId", parentId);
    return form;
}


And, on the client:

function commentSubmitHandler() {
     var placement = this.parentNode;
     backgroundSubmit(this, function(response) {
         placement.innerHTML += response;
     });
     return false;
}

var ch = document.getElementById("comments-holder");
ch.addEventListener("click", function(ev) {
     if(!hasClass(ev, "comment-link"))
          return;

     MySite.getPageContent(ev.href).get(function(html) {
         ev.parentNode.innerHTML += html;
         var forms = ev.parentNode.getElementsByTagName("form");
         for(var a = 0; a < forms; a++) {
              forms[a].onsubmit = commentSubmitHandler;
         }
     });
}, false);



Boom, a standard link for people without JS, and the inline
comment function for people who want that.

The standard link form won't look /great/, but hey it
cost nothing extra (you have to implement a form and
form handler anyway) to implement and it works.




Now, there *are* cases where you can't do this so easily.
If you're stuck on poor PHP I'm sure this is harder than
in D too... but really, do you have one of those cases?


More information about the Digitalmars-d mailing list