TwinSpark was heavily influenced by Intercooler and the main reasons for existing are following:

See at Github.


This document has tests embedded in examples, to run them, press:


Fragment updating

The core feature of TwinSpark is it's ability to update fragments of the page using AJAX requests to server without writing JavaScript.

To simplify inner logic you have one limitation - you can only return single element from the server. If you return more, they will be ignored. How to deal with that and why it is so - read on.


Update me

Triggering requests

Usually requests are triggered on natural interrupts: submit on forms and clicks elsewhere, but sometimes you want more, like triggering on being seen or hovered:


Hover me!

Doing stuff before/after request

If you need to do some custom things around your request, you can use ts-req-before and ts-req-after. Those use same syntax as ts-action, so it's pretty flexible. One additional thing is that if last (or the only) command of ts-req-before returns falsy value (false, or an empty string, or a zero) - this stops that request.


This will work with delay
This is prevented

Collecting data

Sometimes collecting the data you need to send in templates is really annoying: you'll have to teach lower-level components/partials/etc some knowledge about higher levels. That is something TwinSpark can prevent: when you send a request, it iterates through element parents and searches for ts-data attributes, parses their content as query string or JSON and merges it into a single object. Note how values with same keys are aggregated into an array (like normal query string or form data). You can override that by specifying key with null or empty string (see demo source).


So. Much. Data.

Targeting other elements

Often you don't want to replace clicked element, but some other part of the page, like if you hit "Refresh" and need to update big part of the page. This can be done with ts-target attribute, which takes a CSS selector.


I'm waiting... Do it!

Targeting parents

Interestingly, this seems like the most common pattern - when a button needs to update an element around itself. Add modifier parent before selector and element.closest(selector) method will be used to find a parent. This will help a lot to minimize amount of ids.


Wanna read text behind me? Do it!

Changing URLs

URLs are a fundament of the Web. Changing URLs in line with activity makes your app reloadable, browseable with backward/forward button and overall a good citizen of the Web.


Let's change history!

Indicating activity

It's really irritating when you click a link and nothing happens for some time. Luckily TwinSpark makes it really easy: it adds class ts-active to an element, enhanced with ts-req.


Just click me


Sometimes you don't need to go to server to do something. Closing popup or hiding an element can be done without network round trip.

Demo of remove

Hey! I'm here!

Demo of delay

Remove with timeout

Demo of animate (no Safari here)

Triggering on changed visibility

Doing something when element is almost visible makes it possible to implement lazy loading and various analytical events


You'll probably see this text after around 5 seconds or so. Click "Reset" to see loader again.

This sentence will log some message when it becomes invisible (moves out of browser viewport, and, actually, on load as well).


Dealing with multiple elements

TwinSpark usually deals with single element being target (ts-target) and single element being replacement (ts-req-selector) - this is much more controlled behavior than multiple elements. But if you look how endless scrolling is implemented in HTML, it's usually a long list of elements inside some other element - so you have to deal with several elements being appended to a parent. For this and similar use cases there is a modifier children in ts-req-selector.


Element 1
Element 2

Batching requests

This is a thing which is not immediately obvious, but is one of the reasons why TwinSpark appeared. It is an important optimization. Our use case for it was following: page renders for an anonymous user (for efficient caching on CDN) and then status of wishlisted products is checked. It's a pattern we use often and we really wanted it to be a first-class feature.


Span 1
Span 2
Span 3

Joining requests


Update me!

And me!

And me!

Also waiting here.

Doing something when node is removed

It is useful to do something when node is removed (especially if that's some child or even non-related node triggering that removal). It's possible, but not recommended to use often since performance characteristics of the code are not well understood.


When this paragraph is removed by clicking button, it will resurrect itself.

Use cases

Writing autocomplete

Autocomplete is interesting because it executes many things at once. Just look at the source, the interesting part is trigger modifiers - it does something only if user typed something (rather than just navigated field with cursor keys) and then stopped for 200 ms.


Progress bar


Start progress


Filtering on ecommerce sites is a complex task. On one side you want it to be crawlable by Google, on the other if a user selected two filters one by one you'd like to see products, filtered by both. Naïve implementation will filter only one of them if a pause between clicks was short enough. It seems like the best way is form, full of links (so that Google/no-js envs can still use it), which toggle checkboxes when JS is enabled and auto-submit form.


Selected filters: