On Tuesday, 13 March 2012 at 04:07:08 UTC, Ary Manzana wrote:
The implementation is straightforward (much more if I use something like knockoutjs): I post the comment to the server via javascript and on the callback, turn that "editing comment" into a definitive comment.

It is *equally* straightforward to do this without
javascript, though it will have a page refresh.

Note that only the comment contents were transfered between the client and the server.

That's not relevant. Profile a web app's speed, and
you'll see there is no significant difference between
some json and a page on most internet connections.

The majority of the time is spent in javascript, then
latency between server and client. Data transfer time
is often not important.

To implement that I have to check for disabled javascript, and post the comment to a different url that will save the comment and redirect to the same page.

You're doing this wrong. Your comment form is a form, yes?
Just set the action to the same thing you'd call in
javascript.

Zero extra work.

Now, add your javascript code to the onsubmit handler.

Using my web.d:

Element addComment(int contentId, Text commentText) {
   ensureGoodPost(); // xsrf check
   auto obj = new DataObject(db, "comments");
   obj.id = std.random.uniform(1, int.max);
   obj.content_id = contentId;
   obj.message = commentText.content;
   obj.commitChanges();
   // redirect redirects users, not api calls so this just works
redirect("view-content?contentId=" ~ to!string(contentId) ~ "#c" ~ obj.id; return renderComment(obj); // you need to be able to render comments on the server for full page load anyway... so just reuse that
}


And you can render that in javascript or html without
hassle. You can simply let the automatically created
form

To render the form in javascript:

YourSite.getAutomaticForm("addComment").appendTo(this);

to call it:

YourSite.addComment(cid, "comment").appendTo(this);



Or, you can link to it:

site/add-comment?contentId=xxx

that gives the form, and POST to it will save the comment.
No JS needed, it uses standard form encoding.

Submit the form, and the redirect() sends the user
to the right place. Do the javascript submit, and the
redirect is not needed - the return value there
gets sent down.

(don't even have to write html btw!)



I make it scroll without javascript to let the user see the comment just created?

use an anchor in a http redirect. I don't recall if
that is quite right in IE8... but worst case, it just
ignores the anchor so the user scrolls manually. That's
graceful degredation.


In fact, if the page has a static html which invokes javascript that makes callbacks, that's the most efficient thing to do.

No. The more processing you do on the server, the
faster the page will be.

Paying an extra couple milliseconds on the server
is much smaller than paying for double latency
in more server round trips.


It is just like how people combine their css, js,
and images into big single files to cut down on
http requests. Do the same thing with ajax.

Now, you could cache the ajax requests too,
and save that... but there's no need when
you can just do it on the server. (and perhaps
cache the finished product)

Reply via email to