Jquery widget development tutorial

Source: Internet
Author: User

Title: jquery widget development tutorial
-Roamman
2010-02-02 read: 35

-Comment: 0 | Add Comment
| Return
Bytes

Original article: http://bililite.com/blog/understanding-jquery-ui-widgets-a-tutorial/

Since HTML and script cannot be used after translation, the demo cannot be seen. Refer to the original article.

The purpose of this article is to help me understand and develop my own Widgets. At the same time, I hope this article will be helpful to others. Widget is a kind of user interface element.
It is similar to a button or a complicated datepicker (date Selection control ). But this concept is a class for jquery. The members of this class are similar to some HTML elements.
Off, such as draggable and sortable. Of course, not all widgets use $. Widgets. For example, datepicker is not used.

 

Plugin: modify elements

Let's use a P element with CSS class as the target for example.

  1. 1
  1. <P class = "target"> This is a paragraph </P>

Let's turn it green:

  1. 1
  1. $ (". Target" ).css ({"background": 'green '});

Then we need to make this function more universal:

  1. 1
  1. $. FN. Green = function () {return this.css ({Background: 'green '})}

The code above allows us to produce results on any selected element, but it does not allow us to maintain the association with an element. For example, if we want to create a background restoration function, we must determine its initial color. To design a darker (), we must know the color of the element.

Maintain the agent status

We can create an object to manage a ticket with an element:

  1.  
  1. Element. myobject = new myobject ({'target': Element })
  2.  
  3. $. FN. green2 = function (){
  4. Return this. Each (function (){
  5. If (! This. Green) This. Green = new green (this); // associate our State-keeping object with the element
  6. This. Green. setlevel (15 );
  7. });
  8. };
  9. $. FN. Off = function (){
  10. Return this. Each (function (){
  11. If (this. Green) This. Green. setlevel (16 );
  12. Delete this. Green; // recover the memory
  13. });
  14. };
  15. $. FN. Darker = function (){
  16. Return this. Each (function (){
  17. If (this. Green) This. Green. setlevel (this. Green. getlevel ()-1 );
  18. });
  19. };
  20. $. FN. Lighter = function (){
  21. Return this. Each (function (){
  22. If (this. Green) This. Green. setlevel (this. Green. getlevel () + 1 );
  23. });
  24. };
  25.  
  26. Function Green (target ){
  27. Greenlevels =
    ['# 000',' #010 ',' # 020', '# 030',' # 040', '# 050',' #060 ', '# 070',' # 080', '# 090',' #0a0 ',' # 0b0', '#0c0', '#0d0', '#0e0 ', '#0f0', '# fff'];
  28. This.tar get = target; // associate the element with the object
  29. This. Level = 0;
  30. This. getlevel = function () {return this. level ;}
  31. This. setlevel = function (x ){
  32. This. Level = math. Floor (math. Min (greenlevels. Length-1, math. Max (0, x )));
  33. This.target.css ({Background: greenlevels [This. level]});
  34. }
  35. };

However, the above method makes a lot of functions appear in the $. FN namespace. We can also create another namespace in The namespace, but the general design idea is to use a string to call the corresponding function, that is, we can use element. green2 ("darker") to call the darker () method:

  1.  
  1. $. FN. green2 = function (which ){
  2. Return this. Each (function (){
  3. If (which = undefined) {// initial call
  4. If (! This. Green) This. Green = new green ($ (this); // associate our State-keeping object with the element
  5. This. Green. setlevel (15 );
  6. } Else if (which = 'off '){
  7. If (this. Green) This. Green. setlevel (16 );
  8. Delete this. Green
  9. } Else if (which = 'darker '){
  10. If (this. Green) This. Green. setlevel (this. Green. getlevel ()-1 );
  11. } Else if (which = 'lighter '){
  12. If (this. Green) This. Green. setlevel (this. Green. getlevel () + 1 );
  13. }
  14. });
  15. };
  16.  
  17. Function Green (target ){
  18. Greenlevels =
    ['# 000',' #010 ',' # 020', '# 030',' # 040', '# 050',' #060 ', '# 070',' # 080', '# 090',' #0a0 ',' # 0b0', '#0c0', '#0d0', '#0e0 ', '#0f0 ',
    '# Fff'];
  19. This.tar get = target; // associate the element with the object
  20. This. Level = 0;
  21. This. getlevel = function () {return this. level ;}
  22. This. setlevel = function (x ){
  23. This. Level = math. Floor (math. Min (greenlevels. Length-1, math. Max (0, x )));
  24. This.target.css ({Background: greenlevels [This. level]});
  25. }
  26. };

Problems arising from the Association of plug-ins and objects
The above method can easily write a program, but it also brings loop reference and possible memory vulnerabilities. The browser uses different garbage collection methods for JavaScript and DOM elements. The reference method above may cause problems, making the collection unable to proceed normally.
At the same time, we should also pay attention to the memory reclaim when developing plug-ins.
Jquery solves this problem with $. FN. Data:

  1. 1
  1. $ (Element). Data ('myobject', new myobject ({'target': Element }))

However, this operation also brings about other problems. We need to do a lot of "extra" work, which is unrelated to the Program Logic and repetitive and useless. Therefore, repeated implementation of this function requires abstraction to reduce
Less work.
Solution: $. widget
This is why $. widget appears. It associates a single instance of the Javascript class in the plug-in with each element, so that we do not include
In-memory embroidery.
Same as above, you still need to create class constructor. The difference is that we need a prototype that contains the relevant methods ". There are also some changes, such:
The destroy () method is called in the destructor. Both functions are pre-defined, but you can override them. Element and our target above are jquery related to the class
Object.
All methods starting with "_" in the widget belong to a predefined type of UDF and cannot be called externally.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  1. VaR green3 = {
  2. _ Init: function () {This. setlevel (15 );},
  3. Greenlevels:
    ['# 000',' #010 ',' # 020', '# 030',' # 040', '# 050',' #060 ', '# 070',' # 080', '# 090',' #0a0 ',' # 0b0', '#0c0', '#0d0', '#0e0 ', '#0f0 ',
    '# Fff'],
  4. Level: 0,
  5. Getlevel: function () {return this. level ;},
  6. Setlevel: function (x ){
  7. This. Level = math. Floor (math. Min (this. greenlevels. Length-1, math. Max (0, x )));
  8. This.element.css ({Background: This. greenlevels [This. level]});
  9. },
  10. Darker: function () {This. setlevel (this. getlevel ()-1 );},
  11. Lighter: function () {This. setlevel (this. getlevel () + 1 );},
  12. Off: function (){
  13. This.element.css ({Background: 'none '});
  14. This. Destroy (); // use the predefined Function
  15. }
  16. };

The above code is the entire program logic. Use the following code to generate the plug-in:

  1. 1
  1. $. Widget ("UI. green3", green3); // create the widget

This is the translation. It's almost my understanding. Maybe the translation is not good. please correct me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.