Welcome to the last post in this series about REST in SharePoint 2013. In this post it’s time to take a look into Knockout, a JS library that let’s you associate DOM elements with ‘model data’, instead of pushing or appending data by JS you can separate markup from the JS. You can think of Knockout as a general way to make UIs for editing JSON data.
Please read the previous posts in this series before you go on to this post:
- Getting started with REST in SharePoint 2013 – Part I
- Getting started with REST in SharePoint 2013 – Part II
- Getting started with REST in SharePoint 2013 – Part III
- Getting started with REST in SharePoint 2013 – Part IV
- Getting started with REST in SharePoint 2013 – Part V
- Getting started with REST in SharePoint 2013 – Part VI
- Getting started with REST in SharePoint 2013 – Part VII
Hey what’s Knockout?
Knockout js (shortly called KO) is a popular JS library that helps to create rich & interactive web applications. It works directly with the web application’s underlying data model. Using KO with any web application is simple, clean and straightforward. Its powerful in the context of dynamic UI creation.
In previous example I’ve used jQuery for the Ajax stuff, but what should you use? Well KO doesn’t compete with jQuery or similar low-level DOM APIs. KO provides a complementary, high-level way to link a data model to a UI. KO itself doesn’t depend on jQuery, but you can certainly use jQuery at the same time, and indeed that’s often useful if you want things like animated transitions. I would try to use KO in the first hand if it’s about a more complex structure of data you are going do display in let’s say an app, otherwise if its just about to roll up some stuff from a list in SharePoint I would just do as described in the previous posts in this series. Please note that KO is not the only player here, there are as always a bunch of alternative techniques and framworks out there, but that’s maybe a subject for a another post in the future.
KO in short
- Declarative bindings
- Pure JS, minimal and works cross browser
- A framework that let’s you build interactive CRUD applications in SharePoint with help of REST
Let’s try Knockout!
For this example I have created a custom list called ‘Knockout’ in a subsite called ‘knockout’ below the root site collection. I have added two custom columns (multiple text) to the list called ‘Introduction’ and ‘Content’ and added a few example items. The idea is to display the content from the list in a app or web part on the start page of the root site. How to create an app or web part is out of scoope for this post, if you want to try this you can just add this stuff into a textfile in a document library in the root site and use a content editor and link it to the textfile including the JS and the markup you’ll find down below.
It’s a standard SharePoint list at the top, and below is an example of you can create your own custom look and feel with help of KnockOut
Thats the last post in this series about REST, hope you liked it!