I have seen a variety of questions centered around creating custom MVVM widgets on this forum, and have not yet seen a good answer on how exactly to create a custom widget that is MVVM aware. Mr. Holland did a nice job with a data-source aware widget that also binds to MVVM, but it only covers a datasource aware widget and doesn't extend into other bindings that you might want to support in your own custom widgets. I wanted to post a series of tips I've discovered with creating custom widgets that are fully MVVM aware and that function in the same manner as the Kendo widgets themselves.
In this first post, I will show how to create widget that uses simple options that are defined either from data-someoption attributes, or within the { someoption: 'myvalue'} javascript declarations. We'll create a widget that uses markup like:
In order to accomplish this, let's start with the boiler plate code for a simple widget extension.
With the boiler plate code, we can start adding in the functionality we'll need to support this widget within the KendoUI framework. First, we should create an init function. Kendo will call init during the creation of the widget.
The init function is the entry point for KendoUI framework to initialize our widget. We'll need to use this skeleton of a function and add in our DOM elements creation, event bindings to handle events within the widgets, and setup of any settings or options that we need to handle during creation. The options variable is supplied to us by kendo and contains all of the options that are set as data-option value pairs or passed in via the options attribute in a javascript declaration. If we had an option in markup such as data-someoption, we could reference it here as options.someoption. This value could also be passed during javascript initialization as { someoption: 'somevalue' }. In order for options to be read and available in the options collection, we have to declare them in a property called options within our widget.
By exposing this options collection, now have access to these options in the options collection of our widget. Options will automatically be set by the framework within our options collection without any action on our part. Each option that is declared using a data-someoption="somevalue" attribute or in the javascript as an options: { someoption: "myvalue" } that also has a corresponding options: {} declaration in our widget will be available in the options collection in the init method, and the provided value will also automatically be updated within this.options in our widget.
We can show the use of the options with a little bit of simple jQuery to set the source elements value to equal the option's value.
We can see that both the options parameter as well as our widget's options property have been set to the value specified in the markup's data-someoption attribute. We would also see this value from a javascript options attribute if our widget was declared from code.
This fiddle provides a working example of widget options: http://jsfiddle.net/cn172/EuNLh/
The full source code for a very basic widget with simple options support:
In the next post, I'll cover a basic MVVM binding, the value binding. We'll see how MVVM sets the value of widgets with a data-bind="value: someViewModelProperty" markup and how to reflect that value on our UI.
In this first post, I will show how to create widget that uses simple options that are defined either from data-someoption attributes, or within the { someoption: 'myvalue'} javascript declarations. We'll create a widget that uses markup like:
<
input
data-role
=
"SimpleWidget"
data-someoption="myoption" />
(
function
($) {
var
kendo = window.kendo,
ui = kendo.ui,
Widget = ui.Widget;
var
SimpleWidget= Widget.extend({
// custom widget stuff
});
})(jQuery);
// Kendo calls this method when a new widget is created
init:
function
(element, options) {
var
that =
this
;
Widget.fn.init.call(
this
, element, options);
}
//List of all options supported and default values
options: {
name:
"SimpleWidget"
,
value:
null
,
width:
"150"
,
someoption:
""
}
We can show the use of the options with a little bit of simple jQuery to set the source elements value to equal the option's value.
init:
function
(element, options) {
...
//Options are accessible via the options
$(that.element).val(options.someoption);
//Our internal widget's options property has also been updated for us
$(that.element).val(that.options.someoption);
...
}
This fiddle provides a working example of widget options: http://jsfiddle.net/cn172/EuNLh/
The full source code for a very basic widget with simple options support:
(
function
($) {
var
kendo = window.kendo,
ui = kendo.ui,
Widget = ui.Widget;
var
SimpleWidget = Widget.extend({
// Kendo calls this method when a new widget is created
init:
function
(element, options) {
var
that =
this
;
Widget.fn.init.call(
this
, element, options);
//Our internal widget's options property has been updated for us
$(that.element).val(that.options.someoption);
},
//List of all options supported and default values
options: {
name:
"SimpleWidget"
,
value:
null
,
width:
"150"
,
someoption:
""
,
},
});
ui.plugin(SimpleWidget);
})(jQuery);
In the next post, I'll cover a basic MVVM binding, the value binding. We'll see how MVVM sets the value of widgets with a data-bind="value: someViewModelProperty" markup and how to reflect that value on our UI.