0.0
No release in over 3 years
Low commit activity in last 3 years
Prepares the SHA values of CDN assets in a JS hash. Knockout templates can then refer to them without being rendered by erb or haml every time.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

> 5.0
 Project Readme

Knockout Assets

Knockout Assets is a Rails gem that generates full asset URL paths for assets in the asset pipeline. It also supplies simple bindings for using assets in Knockout templates. This makes it very useful for applications that use Knockout and CDNs.

Usage

Install:

gem 'knockout-assets'

Add the loader in yout application.html.erb:

<%= knockout_assets %>

Then use in a template:

<img data-bind="img: 'person.gif'">

Why?

Asset URLs change depending on how they are served up. In production, assets coming from a CDN have a URL generated at compile time. The standard Rails way of solving this is to use an image_tag, which renders the right URL. However, since Knockout templates are rendered client side, they don't have access to this method, unless the templates are generated in the first place using erb or haml.

Using Knockout Assets has three advantages over using a standard image_tag in knockout templates.

  • The knockout templates files need not be processed on the server side at all. This means that testing them (for example with Jasmine) becomes possible.
  • The templates may be served up from a CDN using Knockout.js-External-Template-Engine.
  • The images are preloaded by default so templates render with the correct layout instantly (but this can be optionally disabled).

Options

Include/Exclude some images using a regex

<%= knockout_assets(include: /.*\.(png|gif|jpg|jpeg|bmp|svg)/) %>

or

<%= knockout_assets(exclude: /im.*\//) %>

Or use both (exclude wins).

Disable preloading images

<%= knockout_assets(preload: false) %>

License

MIT License.