Lazy-load Rails partials via CableReady
BREAKING CHANGE: With v1.0, futurism has been transferred to the stimulusreflex organization. Please update your npm package to @stimulus_reflex/futurism accordingly
Caniuse
with a helper in your template
custom
With that method, you could lazy load every class that has to_partial_path defined (ActiveModel has by default).
You can pass the placeholder as a block:
You can also omit the placeholder, which falls back to eager loading.
Currently there are two ways to call futurize, designed to wrap render's behavior:
You can pass a single ActiveRecord or an ActiveRecord::Relation to futurize, just as you would call render:
Remember that you can override the partial path in you models, like so:
That way you get maximal flexibility when just specifying a single resource.
Call futurize with a partial keyword:
You can also use the shorthand syntax:
Collection rendering is also possible:
You can also pass in the controller that will be used to render the partial.
By default (i.e. not passing in a value), futurize will use ApplicationController, but you may override by setting the Futurism default controller in an initializer, for example config/initializers/futurism.rb.
You can pass a hash of attribute/value pairs which will be mixed into the HTML markup for the placeholder element. This is important for layouts that require elements to have dimensionality. For example, many scripts calculate size based on element height and width. This option ensures that your elements have integrity, even if they are gone before you see them.
This will output the following:
It may sound surprising to support eager loading in a lazy loading library , but there's a quite simple use case:
Suppose you have some hidden interactive portion of your page, like a tab or dropdown. You don't want its content to block the initial page load, but once that is done, you occasionally don't want to wait for the element to become visible and trigger the IntersectionObserver, you want to lazy load its contents right after it's added to the DOM.
Futurism makes that dead simple:
Futurism's default behavior is to broadcast partials as they are generated in batches:
On the client side, IntersectionObserver events are triggered in a debounced fashion, so several renders are performed on the server for each of those events. By default, futurism will group those to a single broadcast call (to save server CPU time).
For collections, however, you can opt into individual broadcasts by specifying broadcast_each: true in your helper usage:
For individual models or arbitrary collections, you can pass record and index to the placeholder block as arguments:
Once your futurize element has been rendered, the futurize:appeared custom event will be called.
Add this line to your application's Gemfile:
And then execute:
To copy over the javascript files to your application, run
! Note that the installer will run yarn add @stimulus_reflex/futurism for you !
After bundle, install the Javascript library:
In your app/javascript/channels/index.js, add the following
For authentication, you can rely on ActionCable identifiers, for example, if you use Devise:
The Stimulus Reflex Docs have an excellent section about all sorts of authentication.
In Rails system tests there is a chance that flaky errors will occur due to Capybara not waiting for the placeholder elements to be replaced. To overcome this, add the flag
to an initializer, for example config/initializers/futurism.rb.
Out of the box, Rails will prefix generated urls with http://example.org rather than http://localhost, much like ActionMailer. To amend this, add
to your environments.
Below are a set of instructions that may help you get a local development environment working
The gem is available as open source under the terms of the MIT License.
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Read more from the original source:
GitHub - stimulusreflex/futurism: Lazy-load Rails partials ...
- Futurism Vs. Art of the Future - Video [Last Updated On: September 7th, 2012] [Originally Added On: September 7th, 2012]
- Muse - Futurism [HQ] - Video [Last Updated On: September 7th, 2012] [Originally Added On: September 7th, 2012]
- Futurism- Muse Guitar Cover - Video [Last Updated On: September 7th, 2012] [Originally Added On: September 7th, 2012]
- The Tomorrow Project and Futurism at Intel - Video [Last Updated On: September 7th, 2012] [Originally Added On: September 7th, 2012]
- Muse - Futurism (Guitar Cover) - Video [Last Updated On: September 7th, 2012] [Originally Added On: September 7th, 2012]
- Painiting Movements- Part XII Futurism.wmv - Video [Last Updated On: September 7th, 2012] [Originally Added On: September 7th, 2012]
- Futurism - Muse (Pendulum Remix) - Video [Last Updated On: September 7th, 2012] [Originally Added On: September 7th, 2012]
- 1960s futuristic homes and kitchens, retro futurism part1 - Video [Last Updated On: September 7th, 2012] [Originally Added On: September 7th, 2012]
- Italian Futurism Tribute - Video [Last Updated On: September 7th, 2012] [Originally Added On: September 7th, 2012]
- Optimistic futurism - Video [Last Updated On: September 7th, 2012] [Originally Added On: September 7th, 2012]
- Blonde Redhead - Futurism Vs. Passeism Part 2 - Video [Last Updated On: September 7th, 2012] [Originally Added On: September 7th, 2012]
- Muse - Futurism - Video [Last Updated On: September 7th, 2012] [Originally Added On: September 7th, 2012]
- Futurism - The Dead Movement That Lives - Video [Last Updated On: September 7th, 2012] [Originally Added On: September 7th, 2012]
- What I'd like to see in the iPhone 5 [Last Updated On: September 8th, 2012] [Originally Added On: September 8th, 2012]
- Dion Lee: sleek, futuristic, leathery [Last Updated On: September 16th, 2012] [Originally Added On: September 16th, 2012]
- ‘Shock of the News’ at National Gallery of Art a fascinating cross section of art, news [Last Updated On: September 21st, 2012] [Originally Added On: September 21st, 2012]
- The Best and Worst of Star Trek: The Next Generation’s Sci-Fi Optimism [Last Updated On: September 30th, 2012] [Originally Added On: September 30th, 2012]
- Text Messaging Still Thriving Despite Smartphones, Twitter and WhatsApp [Last Updated On: September 30th, 2012] [Originally Added On: September 30th, 2012]
- The Mind-Bending Charm of 'Looper' [Last Updated On: September 30th, 2012] [Originally Added On: September 30th, 2012]
- The Most Beautiful '50s And '60s American Car Paintings [Car Art] [Last Updated On: October 2nd, 2012] [Originally Added On: October 2nd, 2012]
- LV: Brit Dance-Music Production Crew Conspire With South African MCs [Last Updated On: October 3rd, 2012] [Originally Added On: October 3rd, 2012]
- Infographic: Mapping The 70-Year Gestation Of Street Art [Last Updated On: October 3rd, 2012] [Originally Added On: October 3rd, 2012]
- Review: Prague's weeks of fashion [Last Updated On: October 4th, 2012] [Originally Added On: October 4th, 2012]
- Chattanoogan.com - Chattanooga's source for breaking local news [Last Updated On: October 5th, 2012] [Originally Added On: October 5th, 2012]
- Exhibition at the Grand Palais seeks to shed light on Edward Hopper's works of art [Last Updated On: October 10th, 2012] [Originally Added On: October 10th, 2012]
- About the Illustrators [Last Updated On: October 11th, 2012] [Originally Added On: October 11th, 2012]
- Live review: David Byrne & St. Vincent spellbinding at Segerstrom Center stunner [Last Updated On: October 14th, 2012] [Originally Added On: October 14th, 2012]
- The future according to Rush runs like clockwork [Last Updated On: October 15th, 2012] [Originally Added On: October 15th, 2012]
- Review: A$AP Rocky at the Egyptian Room at Old National Centre [Last Updated On: October 15th, 2012] [Originally Added On: October 15th, 2012]
- Singularity Summit 2012: the lion doesn’t sleep tonight | Gene Expression [Last Updated On: October 16th, 2012] [Originally Added On: October 16th, 2012]
- Enlightening talk on art and design [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- SoHo Museum Hopes Late Graffiti Artist's Creations Can Inspire Kids [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- Computing's low-cost, Cloud-centric future is not Science Fiction [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- Back to the future: Why The Jetsons is the most influential TV show of the 20th century [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- DJ RASP @ FUTURISM WIGAN 2010 - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- sm1701_presentation:funnism - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- Futurism Biweekly Journal Presents - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- Millennialism - Wiki Article - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- Barzakh - Wiki Article - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- FUTURISM WIGAN 2010 - DJ RASP SHOWCASE - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- Rayonism - Wiki Article - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- AH 438 Midterm Review - German Expressionism, Italian expressionism, Futurism - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- AH 438 Midterm Review - Fauvism, Matisse - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- AH 438 Midterm Review - Picasso - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- AH 438 Midterm Review - Art Nouveau - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- AH 438 Midterm Review - Arts and Crafts - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- AH 438 Midterm Review - Redon - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- AH 438 Midterm Review - Munch - Video [Last Updated On: October 31st, 2012] [Originally Added On: October 31st, 2012]
- The Neo-Future is here [Last Updated On: November 2nd, 2012] [Originally Added On: November 2nd, 2012]
- Futurism by Muse Voice Cover Attempt - Video [Last Updated On: November 2nd, 2012] [Originally Added On: November 2nd, 2012]
- DJ RASP TURNTABLIST SHOWCASE - Video [Last Updated On: November 2nd, 2012] [Originally Added On: November 2nd, 2012]
- "MASKS" Alexander McQueen 1997 by FashionChannel - Video [Last Updated On: November 2nd, 2012] [Originally Added On: November 2nd, 2012]
- Muse - Futurism (140% Speed) - Video [Last Updated On: November 2nd, 2012] [Originally Added On: November 2nd, 2012]
- Muse - Futurism (125% Speed) - Video [Last Updated On: November 2nd, 2012] [Originally Added On: November 2nd, 2012]
- Muse - Futurism (90% Speed) - Video [Last Updated On: November 2nd, 2012] [Originally Added On: November 2nd, 2012]
- Futurism - Ever Since (Stoner) - Video [Last Updated On: November 2nd, 2012] [Originally Added On: November 2nd, 2012]
- Futurism bass cover- Muse - Video [Last Updated On: November 2nd, 2012] [Originally Added On: November 2nd, 2012]
- Futurism FTW - Video [Last Updated On: November 2nd, 2012] [Originally Added On: November 2nd, 2012]
- Hi-Fi Rotary Control Panel Sci-Fi Design - Video [Last Updated On: November 4th, 2012] [Originally Added On: November 4th, 2012]
- Apocalypse - Wiki Article - Video [Last Updated On: November 4th, 2012] [Originally Added On: November 4th, 2012]
- : : Access to Arasaka : : xt10.20zp9 [ music video ] - Video [Last Updated On: November 4th, 2012] [Originally Added On: November 4th, 2012]
- Futurism - Wiki Article - Video [Last Updated On: November 4th, 2012] [Originally Added On: November 4th, 2012]
- Futurism online - Romanian Brand.AVI - Video [Last Updated On: November 4th, 2012] [Originally Added On: November 4th, 2012]
- Let's Roleplay Fallout 3. Episode 7 - Video [Last Updated On: November 5th, 2012] [Originally Added On: November 5th, 2012]
- dreDDup - Live in Subotica (MLADOST CLUB 2-11-2012) - Video [Last Updated On: November 5th, 2012] [Originally Added On: November 5th, 2012]
- SOUFSON Trance Immersion #09 - Video [Last Updated On: November 5th, 2012] [Originally Added On: November 5th, 2012]
- Let's Roleplay Fallout 3. Episode 6 - Video [Last Updated On: November 5th, 2012] [Originally Added On: November 5th, 2012]
- Having a coke with you Frank O' Hara English/Italian - Video [Last Updated On: November 7th, 2012] [Originally Added On: November 7th, 2012]
- Freak Me Out Vs Amber-Shaper Un'sok - Video [Last Updated On: November 7th, 2012] [Originally Added On: November 7th, 2012]
- Muse: Endlessly Voice Cover - Video [Last Updated On: November 7th, 2012] [Originally Added On: November 7th, 2012]
- Let's Roleplay Fallout 3. Episode 8 - Video [Last Updated On: November 7th, 2012] [Originally Added On: November 7th, 2012]
- Futurism 2.0 - Exhibition Video - Gamma Proforma - Video [Last Updated On: November 7th, 2012] [Originally Added On: November 7th, 2012]
- Resurrection of The Dead - Wiki Article - Video [Last Updated On: November 9th, 2012] [Originally Added On: November 9th, 2012]
- Let's Roleplay Fallout 3. Episode 10 - Video [Last Updated On: November 9th, 2012] [Originally Added On: November 9th, 2012]
- Buckyball in Madison Square Park - Video [Last Updated On: November 9th, 2012] [Originally Added On: November 9th, 2012]
- Let's Roleplay Fallout 3. Episode 9 - Video [Last Updated On: November 9th, 2012] [Originally Added On: November 9th, 2012]
- Twilight of the Elites and the Rise of the Culture [Uncertain Principles] [Last Updated On: November 11th, 2012] [Originally Added On: November 11th, 2012]
- Let's Roleplay Fallout 3. Episode 12 - Video [Last Updated On: November 11th, 2012] [Originally Added On: November 11th, 2012]
- Let's Roleplay Fallout 3. Episode 11 - Video [Last Updated On: November 11th, 2012] [Originally Added On: November 11th, 2012]
- Let's Roleplay Fallout 3. Episode 14 - Video [Last Updated On: November 12th, 2012] [Originally Added On: November 12th, 2012]