Build an image gallery with Knockout

This article first appeared in issue 228 of .net magazine – the world's best-selling magazine for web designers and developers.

If you’re working with a fairly simple, content-based website then your JavaScript needn’t get too complicated; perhaps a lightbox effect for an image gallery and some form validation. But as soon as a reasonable amount of data or the need to keep track of UI state in your application is added to the mix then it can start to cause a bit of a problem.

For interfaces where the user can page through data, change the appearance or position of components on the page or make selections or filters that need to persist, trying to rely on DOM inspection to understand where things are is likely to end in frustration. Perhaps a better way to approach things is to have a clean separation of presentation and logic, and that’s where frameworks such as Knockout come in. You might already use event handler bindings in jQuery or other JavaScript libraries to connect user actions with parts of a page, but with Knockout we can go one step further and use JavaScript to automatically populate the interface – so whenever the data or state changes so does the UI.

In rich UI development this can greatly simplify the process of loading and updating data. It’s also more robust and testable, so whether you’re working on your own or in a team it’s a great way to make life easier for everyone.

Thank you for reading 5 articles this month* Join now for unlimited access

Enjoy your first month for just £1 / $1 / €1

*Read 5 free articles per month without a subscription

Join now for unlimited access

Try first month for just £1 / $1 / €1

TOPICS

The Creative Bloq team is made up of a group of design fans, and has changed and evolved since Creative Bloq began back in 2012. The current website team consists of eight full-time members of staff: Editor Georgia Coggan, Deputy Editor Rosie Hilder, Ecommerce Editor Beren Neale, Senior News Editor Daniel Piper, Editor, Digital Art and 3D Ian Dean, Tech Reviews Editor Erlingur Einarsson and Ecommerce Writer Beth Nicholls and Staff Writer Natalie Fear, as well as a roster of freelancers from around the world. The 3D World and ImagineFX magazine teams also pitch in, ensuring that content from 3D World and ImagineFX is represented on Creative Bloq.