Writing jQuery Plugins Jon Hartmann Most content shamelessly stolen from and

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Python Objects and Classes
Written by: Dr. JJ Shepherd
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
High Performance Faceted Interfaces Using S2S Eric Rozell, Tetherless World Constellation.
ICS 665 Jesse Abdul. jQuery UI Overview  jQuery UI javascript library Includes all UI component functionality  jQuery UI CSS framework Includes standard.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
IT PUTS THE ++ IN C++ Object Oriented Programming.
NextGen Technology upgrade – Synerizip - Sandeep Kamble.
Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Ajax Runtime Toolkits IBM Emerging Technologies. What is an AJAX Toolkit/Framework? An AJAX Toolkit/Runtime is more than just XMLHTTPRequest Should includes:
Chapter 8 More Object Concepts
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
London April 2005 London April 2005 Creating Eyeblaster Ads The Rich Media Platform The Rich Media Platform Eyeblaster.
London April 2005 London April 2005 Creating Eyeblaster Ads The Rich Media Platform The Rich Media Platform Eyeblaster.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Intro to jQuery. What is jQuery? A JavaScript library Lightweight (about 31KB for the minified version) Simplifies HTML document traversing (DOM), event.
The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.
4. Events are where it happens! Bear Bibeault Yehuda Katz.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Classes, Interfaces and Packages
Written by: Dr. JJ Shepherd
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
Chapter 2 - OOP Maciej Mensfeld Presented by: Maciej Mensfeld More about OOP dev.mensfeld.pl github.com/mensfeld.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Advance ExtJS concepts. Javascript Object var obj = {}; obj.x = 10; obj.y = 20; var obj = new Object; obj.x = 10; obj.y = 20; function Foo() {this.x =
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Written by: Dr. JJ Shepherd
Delegates and Events 14: Delegates and Events
jQuery – Form Validation
This pointer, Dynamic memory allocation, Constructors and Destructor
Object Oriented Analysis and Design
Inheritance, Polymorphism, and Interfaces. Oh My
Java Programming Language
CSE 1030: Implementing GUI Mark Shtern.
..
Object-Oriented Programming: Inheritance and Polymorphism
Presentation transcript:

Writing jQuery Plugins Jon Hartmann Most content shamelessly stolen from and

What is a Widget? Plugin Stateless Implemented by assigning to $.fn No extensibility – Need to modify it? Make a new plugin Manual Setup – Options – Methods – Callbacks Widget Stateful Special implementation code Extensibility pattern Automatic Setup – Options – Methods – Callbacks

Simple Widget $.widget('thd.progressbar', { _create: function() { var progress = this.options.value + '%'; this.element.addClass('progressbar').text(progress); } }); $(' ').appendTo('body').progressbar({ value: 20 }); Its called like this:

Widget Dissected $.widget('thd.progressbar', { _create: function() { var progress = this.options.value + '%'; this.element.addClass('progressbar').text(progress); } }); $.widget Constructor Widget name with namespace Widget configuration object this also has an options object for all options passed in through when the widget is invoked _create is standard invoked on widget call. this has access to the target element, but IS NOT the target element

Options Default Widget $.widget('thd.progressbar', { // Default Values options: { value: 0 }, _create: function() { var progress = this.options.value + '%'; this.element.addClass('progressbar').text(progress); } });

Adding methods $.widget('thd.progressbar', { //... // Create a public method. Calld as $('*').progressbar('value') value: function( value ) { //... }, // Create a private method. Called as this._constrain() _constrain: function( value ) { //... } }); Adding public methods easy as adding them to configuration object Private methods easy as using an underscore

Adding Callbacks $.widget('thd.progressbar', { //... _update: function() { var progress = this.options.value + '%'; this.element.text( progress ); if ( this.options.value == 100 ) { this._trigger('complete', null, { value: 100 } ); } }); Callbacks passed and set like options, code can trigger them even if not specified. Easily add callbacks where necessary. Always emits "custom" events!!

Widgets Bind Directly to DOM Useful because you can access the widget methods by the DOM – No need to call the $() selector engine again – Accessed via the.data() Also means you have a hook to override a specific instance's methods var bar = $( " ").appendTo( "body" ).progressbar().data( "progressbar" ); bar.option( "value", 50 ); alert( bar.options.value ); // Override bar.open = function () { console.log('Overridden!'); }; bar.open();

Widgets can be… DESTROY'ED! Automatically called when DOM element removed Helps clean up memory by removing references Cleans up HTML if you just want to remove the widget from the DOM element (no dangling classes) $.widget('thd.progressbar', { //... destroy: function() { this.element.removeClass( "progressbar" ).text( "" ); // Call the base destroy function. $.Widget.prototype.destroy.call( this ); } });

Widgets Have a Prototype You can extend the widget dynamically You can extend the object like any other object. – Is that the best way? – No. There is another Skywalker… err method. $.thd.progressbar.prototype.reset = function() { this._setOption( "value", 0 ); };

Widget can Extend Widgets BOOM! – Did you catch that? Built in inheritance $.widget('thd.awesomebar', $.thd.progressbar, { dazzle: function(onOff) { this.options.dazzleOn = onOff; this._update(); } }); $(' ').appendTo('body').awesomebar({ value: 20 });

Widgets can Override or Extend Methods Widgets implement "_super" to access parent class. $.widget('thd.awesomebar', $.thd.progressbar, { open: function() { console.log( "open" ); } }); $.widget('thd.awesomebar', $.thd.progressbar, { open: function () { console.log( "open" ); // Invoke the parent widget's open(). return this._super(); } });

Access to _super _super() accepts argument list like.call() _supperApply() accepts an array, like.apply() $.widget('thd.awesomebar', $.thd.progressbar, { _someFunction : function () { this._super( key, value ); // like.call() this._superApply( arguments ); // like.apply() } });

Cool Trick: Redefining Widget jQuery 1.9 lets you redefine a widget, which means this is valid: $.widget('thd.progressbar', $.thd.progressbar, { someMethod : function () { // Do something } }); Note that the name matches the original object definition.