Lately, I was involved in a few Windows 8 projects. One of the questions that I am being asked from time to time was how to write your own Windows Store app custom control with WinJS library. This post will try to give you a head start for your custom control library. During the post you will build an autocomplete control step by step and learn a few WinJS concepts on the way. So let's get started.
The WinJS.Namespace Object
WinJS includes a Namespace object that can help you define namespaces. You will use the WinJS.Namespace.define function which receives two parameters: the namespace name and an object populated with the namespace’s identifiers. The following example defines a new namespace called MyApp.UI:
WinJS.Namespace object also include a defineWithParent function to define namespace hierarchies but in this example I don’t use it.
Defining the Control Class
So now we have a namespace, what's next? creating a control class.
In order to define a class, you will use the WinJS.Class.define function. The function gets three parameters: a constructor function to initialize objects, an instance members object to add to every created instance and a static members object which will be added to the prototype of the class. Lets define the Autocomplete class inside the previously created namespace:
As you can see the define function gets all the three parameters. In the constructor function you will get two parameters which are the element for the control and an options object to configure the control. The two parameters are mandatory for building a WinJS control since they are mapped to the data-win-control and data-win-options HTML attributes of the element when the processAll function is called.
Adding Control functionality
Now that we have our template for the control class, lets create its functionality. Before we write any code, lets understand what we expect from the control. The control needs to create a datalist element on the fly and attach it to an input type (if you want to read about the new HTML5 datalist element, you can go to the following link). The control also needs to get as input an option list that will be shown as the autocomplete list. So lets write the code:
As you can see, the constructor function first checks whether the element is an input type. It also sets the element and the option list which is supposed to be supplied in the option object. The last thing it does is to create the datalist element. In the instance member object you divide the functions and properties to a private and public member section. This division is only logical (and marked with comments) and as you can see private members gets the underscore character before their name. The main functionality here is the _createDataList function which handle the creation of the datalist and sets the input type list attribute to the list id. All the other functions are straight forward.
Using the Control
Now that we have the control, lets use it. In the example app I added a data.js file under the js folder and wrote the following code inside of it:
I could get any data from any other source (cloud, service, storage) but I wanted the example to be simple.
In the home.html file located in pages/home folder I added a text input type which is configured using the data-win-control and data-win-options attributes:
Don’t forget to add the script tags for the autocomplete.js file and data.js file in order for the example to work.
Now everything is set and you can run the app and see the result:
The full example can be downloaded from here.
WinJS comes with a lot of built-in controls such as the FlipView, ListView ToggleSwitch and many more. On the other hand, sometimes it is necessary to build your own custom controls. In this post you learned the basics of creating your own WinJS custom control.