Developing with KendoUI we try to formalize tasks. With this in mind we would like to have user controls.
We define user control as following:
It is a javascript class that extends Widget. It offers a way to reuse UI. It allows to define a model and a template with UI and data binding.
Unfortunately, KendoUI does not have such API, though one can easily define it; so we have defined our version.
Here we review our solution. We have taken a grid KendoUI example and converted it into a user control.
See index.html
<!DOCTYPE html> <html> <head> <title>Test</title> <!-- (1) Include templates for controls. --> <script src="scripts/templates.js"></script> <script src="scripts/jquery/jquery.js"></script> <script src="scripts/kendo/kendo.web.min.js"></script> <!-- (2) UserControl definition. --> <script src="scripts/controls.js"></script> <!-- (3) Confirm dialog user control. --> <script src="scripts/controls/confirm.js"></script> <!-- (4) Products user control. --> <script src="scripts/controls/products.js"></script> <link href="styles/kendo.common.min.css" rel="stylesheet" /> <link href="styles/kendo.default.min.css" rel="stylesheet" /> <script> $(function () { // (5) Bind the page. kendo.bind( document.body, // (6) Model as a datasource. { source: [new nesterovskyBros.data.ProductsModel] }); }); </script> </head> <body> <!-- (7) User control and its binding. --> <div data-role="products" data-bind="source: source"></div> </body> </html>
That's what we see here:
Declaration consists of a view and a model.
View is html with data binding. See products.tmpl.html
We build our project using Visual Studio, so templates packaging is done with templates.tt. This transformation converts products template into a tag:
<script id="products-template" type="text/x-kendo-template">
thus template can be referred by a utility function: nesterovskyBros.template("products-template").
nesterovskyBros.template("products-template")
Model inherits kedo.data.Model. Here how it looks:
// (1) Define a ProducsModel class. nesterovskyBros.data.ProductsModel = kendo.data.Model.define( { // (2) Model properties. fields: { productName: { type: "string", defaultValue: "Product Name" }, productPrice: { type: "number", defaultValue: 10 }, productUnitsInStock: { type: "number", defaultValue: 10 }, products: { type: "default", defaultValue: [] } }, // (3) Model methods. addProduct: function () { ... }, deleteProduct: function (e) { ... }, ... }); // (4) Register user control. nesterovskyBros.ui.Products = nesterovskyBros.defineControl( { name: "Products", model: nesterovskyBros.data.ProductsModel });
That's what we have here:
nesterovskyBros.defineControl(proto)
proto.name
proto.model
proto.template
$("#" + proto.name.toLowerCase() + "-template").html()
Now, what's remained is API for the UserControl. See controls.js.
change
dataBound
dataBinding
save
autoBind (default false)
template (default $.noop)
dataSource
setDataSource()
rebind()
model.refresh
options.name
options.model
options.windowOptions
kendo.ui.Window
model()
dialog()
result()
deleteProduct: function(e) { var that = this; return nesterovskyBros.dialog.confirm( { title: "Please confirm", message: "Do you want to delete the record?", confirm: "Yes", cancel: "No" }). open(). center(). result(). then( function(confirmed) { if (!confirmed) { return; } ... }); }
User controls along with technique to manage and cache templates allow us to build robust web applications. As the added value it's became a trivial task to build SPA.
See also: Compile KendoUI templates.