TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Intercooler.js – Making AJAX as easy as anchor tags

455 pointsby cx1000over 8 years ago

31 comments

DSteinmannover 8 years ago
This has been reposted so many times by the author and by others that I can&#x27;t help but finally ask.<p>What&#x27;s the point? This would lead to your API being comprised of blocks of HTML which are probably only useable for one product. Why not just use REST + JSON? It would take no more than five minutes to set up client-side rendering, and you could even make it attribute-based like this with barely any more effort. Is it really not worth spending the extra five minutes it takes to set things up in a way that is reusable and standard? All I see is piles of legacy code being generated where it hurts most - in the backend.<p>This took me 10 minutes to cook up. It would have taken about three if I hadn&#x27;t forgotten the jQuery and Handlebars APIs. This allows you to POST to a JSON API using two attributes. Untested of course, but you get the idea:<p><pre><code> Example: &lt;button ic-post-to=&quot;&#x2F;api&#x2F;resource&quot; ic-template=&quot;#a-handlebars-template&quot; &#x2F;&gt; $(&#x27;[ic-post-to]&#x27;).click((button) =&gt; { fetch($(button).attr(&#x27;ic-post-to&#x27;)), { method: &#x27;post&#x27; }) .then((result) =&gt; { let templateText = $($(button).attr(&quot;ic-template&quot;)).html(); let template = Handlebars.compile(templateText); let resultHtml = template(result); $(button).replaceWith(resultHtml); }); });</code></pre>
评论 #12887484 未加载
评论 #12886871 未加载
评论 #12886924 未加载
评论 #12887360 未加载
评论 #12886853 未加载
评论 #12887033 未加载
评论 #12892858 未加载
评论 #12892988 未加载
评论 #12887280 未加载
评论 #12886947 未加载
评论 #12886891 未加载
评论 #12887745 未加载
评论 #12889251 未加载
评论 #12887230 未加载
评论 #12886908 未加载
cx1000over 8 years ago
Honestly it feels like intercooler.js is building in functionality that should exist in HTML in the first place. For example, the unintuitive &quot;href&quot; tag sends a GET request, and POST requests are only sent with forms and buttons. What about PUT, PATCH, OPTIONS, or DELETE? According to <a href="http:&#x2F;&#x2F;softwareengineering.stackexchange.com&#x2F;a&#x2F;211790" rel="nofollow">http:&#x2F;&#x2F;softwareengineering.stackexchange.com&#x2F;a&#x2F;211790</a>, &quot;At this point, it seems that the main reason why there is no support for these methods is simply that nobody has taken the time to write a comprehensive specification for it.&quot;<p>Intercooler.js makes them seem a little more &quot;built in&quot; to html, which I like.
评论 #12886517 未加载
评论 #12889151 未加载
评论 #12887915 未加载
评论 #12886874 未加载
nzjrsover 8 years ago
I said this on the other discussion, but I&#x27;m compelled to post it again.<p>Intercooler.js is so logically designed it basically requires no documentation - I read the introduction and a handful of examples and thought, &quot;shit of course it works this way&quot; and could basically derive how every other feature mapped to its implementation in that moment.<p>Congratulations!
评论 #12886680 未加载
scwoodalover 8 years ago
I was a long time pjax&#x2F;turbolinks user but always felt like I was pushing the boundaries of what these technologies were doing and always wished for more functionality.<p>I tried out several client side frameworks but always felt like it was way overkill for the apps I built.<p>I gave intercooler.js a try a few months ago and was extremely pleased. There&#x27;s very little server side that&#x27;s required and the extra functionality I had wanted from pjax was there.<p>If you&#x27;re wanting the simplicity of server side rendering plus the feel of an SPA without the frontend complexity give this library a try.
评论 #12890931 未加载
Kequcover 8 years ago
I&#x27;m surprised this needs jQuery. What this seems to be is a simple script that fetches a resource and places it into an element. I really feel opposed to adding more dependencies where they aren&#x27;t required. That could be written without jQuery or this library fairly easily.
评论 #12886622 未加载
评论 #12886305 未加载
评论 #12886663 未加载
评论 #12886267 未加载
xg15over 8 years ago
This seems to make things simpler at first glance, but I fear in the end you end up with the worst of both worlds: You have the API inflexibility and UX restrictions of a pure-HTML approach combined with the overhead and need for graceful degradation of a full-ajax approach.
评论 #12887527 未加载
carsongrossover 8 years ago
Main intercooler.js author here, glad to answer any questions.<p>Happy to see people are enjoying it!
评论 #12886623 未加载
评论 #12887115 未加载
评论 #12890546 未加载
评论 #12886978 未加载
评论 #12886606 未加载
评论 #12890911 未加载
评论 #12890106 未加载
Toucheover 8 years ago
It would be cool if this could some how use DOM diffing (I assume it just uses innerHTML now), so you&#x27;d get minimal dom updates with the advantages of doing everything server-side that this already provides you. Throw in some smart service worker caching and you get pretty close to the responsiveness of a fully client-side approach.
评论 #12886794 未加载
20yearsover 8 years ago
I am happy to see this featured on the front page. I am using this for a current project after coming off an Angular project. I am so glad I chose this. It is simple to use and a pleasure to work with.
评论 #12887799 未加载
stdgyover 8 years ago
Very neat! This matches up closely with what we have evolved for my group&#x27;s legacy codebase to simplify handling AJAX requests. I suspect we&#x27;re not alone in arriving at this sort of declarative abstraction.<p>Unfortunately, our implementation is rather scatter-brained and non-uniform. That&#x27;s partly due to its gradual evolution and partly due to lack of free employee time to clean up bit-rot. I&#x27;m going to investigate this a bit more and mock out some examples for our product. I definitely think it&#x27;d help us organize our unruly mass of code. Good job!
dec0dedab0deover 8 years ago
I recently used intercooler to implement a small feature in a django app. It was an absolute pleasure to use.
oliv__over 8 years ago
Thanks HN! This is one of those just-in-time situations: I was going to need something to do some AJAX in the next few days and this is one of the most elegant solutions I&#x27;ve seen so far. Didn&#x27;t even know this existed!
astrospectiveover 8 years ago
I&#x27;ve been using this on .net projects, have pulled off some fairly intricate UIs by returning server rendered partials. The polling support is nice and robust for dashboards.
cx1000over 8 years ago
I love that you can use this without having to build anything with babel&#x2F;webpack. Given the scope of my web apps, anything that transpiles or mutates my sourcecode is a non starter because it makes debugging it weird since I&#x27;m not looking at my own code anymore.
评论 #12886255 未加载
评论 #12886373 未加载
ing33kover 8 years ago
this is one of those libraries, which should be posted on HN once in a while .
评论 #12887080 未加载
rhabarbaover 8 years ago
&quot;Small&quot;. As in &quot;just add the giant jquery library as a dependency&quot;.
brianzelipover 8 years ago
fyi, the timing of this post is likely related to the HN discussion <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12882816" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12882816</a>
bobwaycottover 8 years ago
I have, over the last few years, taken a similar approach and built my own reusable, yet rudimentary, version of this. Happy to see such a well-thought out and elegant approach that matches my own preferences. Going to be using Intercooler in the future (and might even switch my old stuff to it). Nice project.
unethical_banover 8 years ago
I see someone read the front-end discussion. I am reading the guide to IC.js and it&#x27;s a neat piece of tooling.
ape4over 8 years ago
What happens when there&#x27;s an error? eg cannot contact host.
评论 #12887061 未加载
chandmkhnover 8 years ago
Webforms version of asp.net always sypported this idea through soething called UpdatePanel<p><a href="https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;bb399001.aspx" rel="nofollow">https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;bb399001.aspx</a><p>Commercial control providers in .Net world support these scenario with something called &quot;CallbackPanel&quot;.<p><a href="https:&#x2F;&#x2F;demos.devexpress.com&#x2F;MVCxMultiUseExtensionsDemos&#x2F;CallbackPanel&#x2F;Example" rel="nofollow">https:&#x2F;&#x2F;demos.devexpress.com&#x2F;MVCxMultiUseExtensionsDemos&#x2F;Cal...</a><p>Real conufsion starts when you have nested HTML controls that automagcally making ajax calls. Nice idea as long as you can get away with minimal work.<p>The moment you want to use any moden SPA framework, you are up for a big rewrite.
评论 #12904434 未加载
chunkiestbaconover 8 years ago
I used this to make my own webshop software for a client. Lots of ajax features but only 80 lines of javascript in total. Intercooler is great to update the shopping cart in the sidebar when pressing the add to cart button. This makes the shop feel a lot smoother.
wichertover 8 years ago
If you like this sort of thing I can recommend to look at Patternslib (<a href="http:&#x2F;&#x2F;patternslib.com" rel="nofollow">http:&#x2F;&#x2F;patternslib.com</a> ), which has many tools to add interactive behaviour to a webpage without having to write and javascript, making it a great toolset for designers. The injection pattern (see <a href="http:&#x2F;&#x2F;patternslib.com&#x2F;inject&#x2F;" rel="nofollow">http:&#x2F;&#x2F;patternslib.com&#x2F;inject&#x2F;</a> ) does everything intercooler does, but also a lot more.<p>Disclaimer: I&#x27;m one of the original authors of Patternslib.
asciihackerover 8 years ago
<p><pre><code> &lt;!-- When this button is clicked an AJAX POST request is sent to &#x2F;example and the response content is swapped in to the body of the button --&gt; &lt;button ic-post-to=&quot;&#x2F;example&quot;&gt; Click Me! &lt;&#x2F;button&gt; </code></pre> But what if I want the response to populate the content of another HTML element? E.g. if I wanted an accordion-style FAQ where when you click on the question (or on a plus-sign before the question), the div below the question is loaded with the answer from the server via AJAX.
评论 #12892836 未加载
grimmdudeover 8 years ago
Cool, this has some great functionality. A while back I wrote something very similar called &quot;jQuery Ajax Markup&quot;. It was much simpler though: <a href="https:&#x2F;&#x2F;github.com&#x2F;grimmdude&#x2F;jquery-ajax-markup" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;grimmdude&#x2F;jquery-ajax-markup</a>
sleepyheadover 8 years ago
&quot;Attribute ic-get-from not allowed on element div at this point.&quot;<p><a href="https:&#x2F;&#x2F;validator.w3.org&#x2F;nu&#x2F;?doc=http%3A%2F%2Fintercoolerjs.org%2Fexamples%2Flazyload.html" rel="nofollow">https:&#x2F;&#x2F;validator.w3.org&#x2F;nu&#x2F;?doc=http%3A%2F%2Fintercoolerjs....</a>
评论 #12890136 未加载
Yokohiiiover 8 years ago
Not sure yet if I like it, but for the &quot;on churn&quot; parts I will leave some respect here.
bedrosover 8 years ago
the best part is the examples, there are so many practical cases.
评论 #12886796 未加载
smrtinsertover 8 years ago
How many times will something like this be attempted? It ends up being awful for debugging. Works great when there are no bugs of course.
评论 #12893567 未加载
jramzover 8 years ago
reminds me a bit to: <a href="https:&#x2F;&#x2F;github.com&#x2F;eldarion&#x2F;eldarion-ajax" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;eldarion&#x2F;eldarion-ajax</a>
bitforgerover 8 years ago
+1 for official theme music