• .NET Developer Tools DevTools

    UI controls for ASP.NET AJAX, MVC, WPF,
    Silverlight, Windows 8 and Windows Phone

  • Hybrid Mobile Development Icenium

    Cross-platform Mobile Development Tool
    with cloud-based architecture

  • HTML5 / JavaScript Development Kendo UI

    Everything you need to build sites and
    mobile apps with JavaScript and HTML5

  • Testing Tools TestStudio

    One easy tool for Functional, Performance,
    Load and Mobile software testing

  • Web Presence Platform Sitefinity CMS

    Everything for your online business - content
    management, ecommerce, emarketing

  • Agile Project Management TeamPulse

    Simple and intuitive project management
    and collaboration software

Contact us

We are here for you.
  • usa+1‒888‒365‒2779
  • uk+44‒20‒7291‒0580
  • bg+359‒2‒8099850
  • de+49‒89‒2441642‒70
  • au+61‒2‒8090‒1465
  • emailsales@telerik.com
Your account Access to your products, updates and support
Telerik Product Families
  • Your Account
    Your Account
    Log in
  • ABOUT US

    About Telerik

    • Company
    • Press Center
    • Customers
    • Community
    • Careers
    • Contacts
Kendo UI - The way of HTML5
Products ▼
Kendo UI Web Kendo UI Mobile Kendo UI DataViz Server Side Wrappers
Demos Purchase Download
Blogs Documentation
Support ▼
Premium Forums StackOverflow Forums
Resources ▼

Featured Resource

Kendo UI Dojo


Blogs Code Library Demos Documentation FAQ Testing
Premium Forums Roadmap User Voice Videos Webinars More Resources
Contact Us Search
 

Blogs

Creating Custom Kendo UI Plugins

Tuesday, April 03, 2012 by Kendo UI Team Blog | Comments 16

One of the questions that I see with some frequency on the Twitters, Emails and general InterWebs is:

“Hey yo! How can I extend Kendo UI widgets or create custom Kendo UI plugins?”.

Of course the fabulous thing about JavaScript frameworks is that you can in fact extend them or create your own plugins for them.  jQuery’s ability to add plugins so easily is one of the major contributing factors in its raging success and rampant implementation. There was an absolute explosion of available plugins online that did everything from the very useful Masked Input, to the completely ridiculous yet awesome UnicornBlast.

Kendo UI is built on a plugin architecture and exposes that same interface for you to either extend existing widgets, create your own plugins that are completely new widgets, or composite of exsting Kendo UI controls.

In this post, I’ll show you how to create a custom Kendo UI plugin that uses the AutoComplete, ListView, Pager and Kendo UI DataSource to harness the power of Google Suggest and the YouTube search API.

Disclaimer

While Kendo UI provides awesome ways for building custom widgets, remember that the Kendo UI team can only support features that ship out of the box! If you want improvements to existing Kendo UI widgets, it's much better to share the idea on UserVoice and/or submit a feature request and/or ask for the improvement in the forums. If the improvement makes sense, we're very likely to implement it in an upcoming release (we have 3 major releases + service packs every year), and then you get support for it! Same goes for new widgets. If you want it, ask for it!  

Of course, we'd love to see your work and help you out if you run into any issues, so you can always hit us up on Twitter or by posting a question on StackOverflow.

Kendo YouTube Search Widget

You can grab all the code used in this project from the kendo-plugins GitHub repo.  First, lets have a look at the finished product.  You create the YouTube search widget in the normal way that you would initialize any Kendo UI widget.

jQuery initialization

<div id="youtube"></div> <script>

    $("#youtube").kendoYouTube();

</script>

 

Declarative Initialization

<div class="container"> <div id="youtube" data-role="kendoYouTube"></div> </div> <script> // bind all the widgets
    kendo.bind($(".container"));

</script>

And here is what you get…

Of course you can dive right in and start looking at the source if that’s how your boat is best floated.  I’m going to dissect it a bit here and go over some of the practices that we use when building Widgets that you can use as well.  First let me go over the basics of creating a new widget and provide you with some boilerplate code.

The Boilerplate

Extending Kendo UI is pretty darn easy and straightforward.

Step 1 is to extend the base Widget class in the kendo.ui namespace.  I also create some variables to hold values which helps with minification down the road.

(function($) {
    
    // shorten references to variables. this is better for uglification var kendo = window.kendo,
        ui = kendo.ui,
        Widget = ui.Widget

    var YouTube = Widget.extend({

        // awesome code lies herein.

    });

})(jQuery);

Notice a couple of things here.

1. The entire thing is wrapped in a self executing anonymous function so as to protect the global namespace as if it were the rebel base.  jQuery is passed in as a reference to make sure $ is jQuery.

2. The widget itself extends the base Widget class so its given the Upper Case name YouTube because this is how Crockford wants it so that’s how you WILL DO IT.  This is generally considered best practice when naming classes in JavaScript as opposed to regular objects.

Number 2. Engage. Or Init.

Obscure Star Trek TNG reference out the way, we need to provide the init method.  This method will be called by the framework when the Widget is initialized.  This init function takes 2 parameters.  The first one is the element on which you are initializing the widget.  The second is a set of options that we are going to specify shortly.  These will be configuration values.

var YouTube = Widget.extend({

    init: function(element, options) {
        
        // base call to initialize widget
        Widget.fn.init.call(this, element, options);
    }

});

  

The call to the base is what translates your widget in from declarative initialization or jQuery Initialization; and merges all the base options (if you are extending a widget) and custom options.

Speaking of options, we’re going to need to declare those right under the init.  Anything that you declare in the options object will be available for the user to pass as either a configuration value, or a data attribute.

var YouTube = Widget.extend({

    init: function(element, options) {
        
        // base call to initialize widget
        Widget.fn.init.call(this, element, options);
    },
    
    options: {
        // the name is what it will appear as off the kendo namespace(i.e. kendo.ui.YouTube). 
        // The jQuery plugin would be jQuery.fn.kendoYouTube.
        name: "YouTube",
        // other options go here
        ...
    }

});
 

Sweet.  Lastly but not leastly we add the widget to Kendo UI.  Here is the full boilerplate for creating your own Kendo UI widget and making it available like all other Kendo UI widgets are.

 

(function($) {
    
    // shorten references to variables. this is better for uglification var kendo = window.kendo,
        ui = kendo.ui,
        Widget = ui.Widget

    var YouTube = Widget.extend({

        init: function(element, options) {

            // base call to widget initialization
            Widget.fn.init.call(this, element, options);

        },

        options: {    
             // the name is what it will appear as off the kendo namespace(i.e. kendo.ui.YouTube). 
             // The jQuery plugin would be jQuery.fn.kendoYouTube.
             name: "YouTube",
            // other options go here
            ....
        }

    });

    ui.plugin(YouTube);

})(jQuery);
 

Some Best Practices

It’s nice to adhere to some conventions when creating a plugin for Kendo UI and we kindly ask that you do a few of these small things when creating plugins so that your plugin looks just like our plugins under the covers.

  1. Class names are caps.
  2. If you are going to be using this < 3 times, stick with this. Otherwise, make it var that = this. This is for maximum minification.
  3. private variables and methods being with _.  This is typical and you can find it everywhere in jQuery.  This just denotes a method or property that you should not touch because it’s not meant to be manipulated outside of the Widget.
  4. Try to get all methods out of the init.  It’s tempting to put all your code there, but we try and keep those methods as skinny as possible.

A Widget's Widget

Now that we have gone over the basics, lets look at this Kendo UI YouTube widget.

This is a composite widget.  That simply means that it’s a widget that contains other widgets.  Or a widget inside of a widget. Queue the “Yo Dawg” meme.

This widget contains the AutoComplete which is wired up to Google Suggest API via the Kendo UI DataSource.  When an item is selected in that AutoComplete, a ListView which is bound to the YouTube Search API is refreshed and a pager is attached to the same DataSource.  In the event that a no text is entered in the search box, the ListView and Pager are not displayed.

The widget additionally takes in some configuration attributes:

  • placeholder(string, default: “Search YouTube”) – The watermark text to display when the AutoComplete is empty.
  • template(string, default: internal template) – The template to be used to display the results in the ListView. If not template is provided, a default internal one will be used instead.

We could add more, but this is what it takes for the time being.  In the example above, I am specifying a custom template that returns a video in a Kendo UI Window whenever an item thumbnail is clicked.

In the init method of my widget, I am creating the elements that make up the widget and adding them to the DOM.  I also create the DataSources that are used to connect to Google Suggest and YouTube JSONP API’s.

All methods are moved outside of the init to keep that method as lean as possible.  In my case, its already much larger than what you would find in the standard Kendo UI Widget.

One of the more interesting things about this widget, is that is uses the lesser known parse method on the DataSource to manipulate the results into more valid JSON before the DataSource actually consumes and internally maps them.  For instance, the Google Suggest API returns results as an array of arrays instead of objects.  Which is less than desirable.  Using the parse method on the DataSource, I can enumerate through the array and compose an object of arrays then return that new object as the data.  This is quite a powerful piece of the Kendo UI DataSource as it allows you to have complete control over the structure of the returning data.

// parse the google suggest results before the dataSource gets them
_suggestParse: function(data) {
     return $.map(data[1], function(item) {
          return { value: item[0] }
     });
},

 

The other thing I use on the DataSource is the parameterMap on the transport which allows me to add parameters to the outgoing request before it gets sent.  This is how I can grab the value off of the AutoComplete before I head off to Google Suggest for some, well, suggestions.

The neat thing about the Google Suggest API, is that is auto corrects you as well.  Try searching for “Taylro Swift”, and notice that it fixes it for you in the returned result.  Pretty nifty.  Here is the widget with a custom template and a Kendo UI Window to boot for showcasing the video.

What Will You Build?

I’m very interested to see what sort of plugins and composites people will build with Kendo UI.  This YouTube Widget can be placed on any site running Kendo UI and you will have embedded YouTube search right on your site.  In fact, I added it to my own site earlier this week.

Furthermore, you can use your custom widgets with declarative initialization or the standard jQuery initialization and you don’t have to do any extra work.  It’s all handled for you.

Kendo UI is designed to be not only a complete end-to-end HTML5 framework, but one that you can easily extend and composite should you find the need to.  If you haven’t already, download Kendo UI today and get started building custom plugins and widgets.  Maybe you will build the next Raptorize…

About the Author
Burke Holland is a web developer living in Nashville, TN. He enjoys working with and meeting developers who are building mobile apps with jQuery / HTML5 and loves to hack on social API's. Burke works for Telerik as a Developer Evangelist focusing on Kendo UI. Burke is @burkeholland on Twitter.

16 Comments

  1. 1 Giuseppe Modarelli 03 Apr 2012
    Figo!! (it means cool here in Italy :D )
  2. 2 Jacques 04 Apr 2012
    Would you recommand:

    (function
    ($) {
    var kendo = window.kendo
    ...
    })(jQuery);

    rather than:
    (function($, kendo, undefined) {
        ...
    })(jQuery, window.kendo);
  3. 3 Brandon 04 Apr 2012
    Burke, you have the best blog posts man! I lol'd at the Yo Dawg reference!
  4. 4 Burke 04 Apr 2012
    @Jacques

    It's really just a matter of taste.  You could do it either way, but that's they we it's done internally, so that's the way I posted it here.

    Great question though.
  5. 5 Burke 04 Apr 2012
    @Brandon

    Thanks man!  I appreciate that.  John Bristowe has given me a respect for the meme. And animated GIF.
  6. 6 Jacques 05 Apr 2012
    Could you expand in another blog post on how we can make our custom widgets MVVM and dataSource aware?

    My impression from the Kendo source code is that there is not much to do regarding MVVM, on the other hand things are more complicated with dataSource. Am I correct?

    Also, what if I do not want to clutter or avoid collisions with the kendo namespace, i.e. I do not want my widget to be called kendoYouTube but myProductYouTube.

    Finally, composition/aggregation of widgets is a common scenario, but what about inheritence?  Let's say I like your numericTextbox but I want to add a watermark for example. Would you recommend a composite widget as developed here above?
  7. 7 Burke 10 Apr 2012
    @Jacques

    Ask and you shall receive...

    http://www.kendoui.com/blogs/teamblog/posts/12-04-10/creating_a_datasource_aware_kendo_ui_widget.aspx
  8. 8 mkidder 28 Apr 2012
    @Burke  your blog posts have been exceptional to say the least! Keep up the awesome work.
  9. 9 mkidder 28 Apr 2012
    @Burke  your blog posts have been exceptional to say the least! Keep up the awesome work.
  10. 10 numan 11 Aug 2012
    How do you destroy a custom widget?

    If a create a composite widget and want to destroy it, is there an equivalent of a "destroy" method that frees up the dom and event handlers?
  11. 11 Pascal Martin 09 Jan 2013
    Where can you get the source code of this Widget?

    I can't find a download link in the article and the link in the comments above does not work either.

    Thanks
  12. 12 Burke 14 Jan 2013
    @Pascal

    The code is located at https://github.com/burkeholland/kendo-plugins/tree/master/YouTube.

    I'll fix the links in this article.  Thanks!
  13. 13 Ankur Jindal 07 Jan 2013
    hi

    could you pls explain the options part in bit detail.
    moreover could you pls tell from where can i get the information about the functions available like _create(),_refresh and other functions. Is there any documentation for this.

    Thanks
  14. 14 Kevin Pirkl 19 Dec 2012
    Getting errors with this sample...
    
    "NetworkError: 404 Not Found - https://raw.github.com/burkeholland/kendo-plugins/master/kendo.youtube.js"
  15. 15 zzhang 21 Mar 2013
    The code is located at https://github.com/burkeholland/kendo-plugins/tree/master/YouTube.
    above link does not work. can you give me a working link?
  16. 16 Kuljit Saini 27 Mar 2013
    The current link is 
    https://github.com/kendo-labs/kendo-plugins

Comment

  1. Click to add

  2. Click to add

  3. Click to add

  4.    
     
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
     
      
       
Blogs feed
Categories

  • Tutorials (26)
  • Release (33)
  • Browsers (7)
  • Extensions (3)
  • Tip of the Week (10)
  • Videos (5)
  • Concepts and Theory (13)
  • Misc. (25)
  • Framework Constructs (6)
  • Mobile (6)
  • UI Widgets (5)
  • Blogs (1)
Archive
  • 2013 May (8)
  • 2013 April (10)
  • 2013 March (9)
  • 2013 February (12)
  • 2013 January (10)
  • 2012 December (9)
  • 2012 November (11)
  • 2012 October (6)
  • 2012 September (7)
  • 2012 August (8)
  • 2012 July (10)
  • 2012 June (8)
  • 2012 May (10)
  • 2012 April (7)
  • 2012 March (13)
  • 2012 February (10)
  • 2012 January (6)
  • 2011 December (10)
  • 2011 November (4)
  • 2011 October (6)
  • 2011 September (5)
  • 2011 August (9)
Home Web Mobile DataViz Server Wrappers Whitepapers Surveys Chrome Icenium Contact Us

Kendo UI framework is developed by Telerik - a leading provider of UI components for web, desktop and mobile applications. Trusted by over 100,000 customers worldwide for our devotion to quality and industry-best technical support, Telerik helps professionals maximize their productivity and "deliver more than expected" every day.

kendoui - powered by html5, css3 & jquery
get social
  • Twitter
  • Facebook
  • Google plus
  • RSS
Privacy Policy | Branding Guidelines
Powered by Sitefinity CMS

Copyright © 2011 - 2013 Telerik Inc. All rights reserved.