-1

できるだけシンプルにしようと思います:

jqueryuiの助けを借りて jquery ウィジェットを作成しました(themeroller に移動し、テーマをダウンロードして解凍すると、そのフォルダーに というdevelopment-bundleフォルダーが見つかります。次に、この例を実行するためuiの のみを含めます)。jquery.ui.widget.js

<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>jQuery widget construction & Events</title>
<style>
  h1 {
     color: #808080;
  }
  h1:hover {
     color: #000000;
     text-decoration: none;
     cursor: pointer;
  }
  a {
     color: #808080;
     background-color: #CCFF33;
  }
</style>
</head>
<body>

<h1>Click Me</h1>

<div class=content>
<p>
<a href="http://speckyboy.com/2010/01/20/25-tutorials-and-resources-for-learning-jquery-ui/">spekyboy</a>
</p>
<p>
<a href="http://www.learningjquery.com/">learning jquery</a>
</p>    
</div>

<script src="../jquery-1.7.1.js"></script>
<script src="../jquery.ui.widget.js"></script>

<script>
////////////////////////Plugin Creation (Widget Factory)////////////////////////
///////////////////////////mycompany.linkLocation///////////////////////////////
(function( $ ) {
//1. use jQuery.widget() method, the Widget factory, to create plugins
//--------------------------------------------------------------------
$.widget( "mycompany.linkLocation", {
  //2. widget properties "foreground" and "background" (see docs)
  //-------------------------------------------------------------
  options: {
     color: "black",
     "background-color": "transparent",
     done: false,
     //3. a callback from the relevant widget event: linkLocation_onapply (see docs)
     //-----------------------------------------------------------------------------
     //where does this refers to (???)
     //evt refers to the custom jquery event object
     _onapply: function(evt, options){
        var attrs='';
        for(prop in this)
           attrs+=prop+', ';
        alert('from _onapply, this:\n'+attrs);
        attrs='';
        for(prop in evt)
           attrs+=prop+'='+evt[prop]+'\n';
        alert('from _onapply 1st arg, evt:\n'+attrs);
        //4. where is the reference to widget linkLocation for evt.target?
        //----------------------------------------------------------------
        $(evt.target).css(options);
     }
  },
  //5. used during construction by the Widget Factory (see docs)
  //------------------------------------------------------------
  //this refers to the object built by the Widget Factory
  _create: function() {
  },
  //6. used after initialization (see docs)
  //---------------------------------------
  //called as $("selector").widgetname("option", option_object)
  //or,
  //$("selector").widgetname("option", "key", "value")
  //this refers to the object built by the Widget Factory
   _setOption: function(key, value){
     this.options[ key ] = value;
  },
  //7. public method apply()
  //------------------------
  //this refers to the object built by the Widget Factory
  apply: function(evt){
     if(!this.options.done){
        //create a jquery object that wraps a specific dom element accessed
        //through the this.element provided by the Widget Factory
        var $this = $(this.element);
        this._css($this);
        ////////////////////////////////////////////////////////////////////////////
        //8. maybe one reason we done all these: add memory and execute only once!//
        ////////////////////////////////////////////////////////////////////////////
        //--------------------------------------------------------------------------
        this.options.done = true;
        //9. if apply() is called once then, _onapply() is called once
        //------------------------------------------------------------
        //event linkLocation_onapply will trigger method _onapply(), see docs
        this._trigger("_onapply", evt, {color: "white", "background-color": "purple"});
     }
  },
  //10. a private method _css()
  //--------------------------
  //this refers to the object built by the Widget Factory
  _css: function($elem){
     $elem.css({
        "background-color": this.options.background,
        "color": this.options.foreground
     });
  }
});
}( jQuery ));
//11. user overwites public property: this.options
//------------------------------------------------
//for the objects created by the Widget Factory
var options2 = {
   color: "blue",
   "background-color": "yellow"
};
//12. construct an object for every <a> element using the Widget Factory
$( "a" ).linkLocation(options2);
//13. call widget apply() methods
//-------------------------------
$('h1').on('click', function(evt) {
   //14. the jquery $(h1) object
   var attrs='';
   for(prop in this)
      attrs+=prop+', ';
   alert('From h1 tag onclick event, this:\n'+attrs);
   $( "a" ).linkLocation("apply").on("linklocation_onapply", 
      //15. never executed???
      function(evt, data){
         attrs='';
         for(prop in data)
            attrs+=prop+': '+data[prop]+'\n';
         alert('From linklocation_onapply event function, data:\n'+attrs);
      }
   );
});
</script>

</body>
</html>

(コードのコメントで私の番号を参照してください) 今、私たちがしているのは(12)何もしない(5)いくつかのオプション(2)を持つウィジェットを作成することだけです。ユーザーは構築中にデフォルトのオプションを変更できます(11)。ユーザーはパブリック メソッド(7)を呼び出して、ウィジェット オプションに基づいてアンカーの前景と背景の色を変更できますが、カスタム jquery イベント(9)でトリガーされるコールバックも定義したいと考えていました。apply()

ユーザーが h1 タグ(13)をクリックするたびに、呼び出されてアンカー要素(12)にアタッチされたすべてのウィジェットからメソッドapply() (7)が実行されますが、 1 回だけです (if.​​.else 内にコードを配置します)。前述したように、メソッドはアンカーの色を変更し、コールバック関数をトリガーします。linkLocationapply()

私の質問:

a)thisコールバック_onapply() (3)は、コメント(14)からのメッセージでわかるように、ウィジェットの public メソッドを実際に呼び出す jquery オブジェクトですか? メッセージは次のとおりです。

From h1 tag onclick event, this:
jQuery17102845835909852654, align, click, focus, blur, title, lang, dir, dataset, 
itemScope, itemType, itemId, itemRef, itemProp, properties, itemValue, hidden, 
tabIndex, accessKey, ...

ウィジェット メッセージが続きます。

from _onapply, this:
jQuery17102845835909852654, toString, href, target, download, ping, rel, hreflang, 
type, text, coords, charset, name, rev, shape, protocol, host, hostname ...

from _onapply 1st arg, evt:
type=linklocation_onapply
timeStamp=1382545296011
jQuery17102845835909852654=true
...

_onapply比較すると、追加のプロパティがあることがわかります-なぜそうなのですか?

toString, href, target, download, ping, rel, hreflang, type, text, coords, charset,
name, rev, shape, protocol, host, hostname, port, pathname, search, hash

b) コメント(15)のイベント ハンドラーが実行されないのはなぜですか?

c) jQuerynjquery オブジェクトのプロパティで n=number となるこの常に変化するプロパティは何ですか? jquery はシングルトン オブジェクトではないようで、ウィジェット オブジェクトの場合のように dom 要素のステータスをメモリに保持したい場合は、帯域幅が広いようです。それらはjqueryオブジェクトに保存されていますが、常に自分自身を複製しているときはどれですか?

d) コメント(4)でわかるようにlinkLocation、カスタム イベントをトリガーする特定の要素のウィジェットへの参照はどこにありますか?

本当に、ライブラリが約束する抽象化を見つけることができません....ソースコードを検索して答えを探し続けてください...ありがとう。

4

1 に答える 1

0

答えを探した後、私は次のように結論付けました。

for Qa) ウィジェットの関数_onapply()options、ウィジェットの接続された要素に割り当てられているため、実行コンテキストを内部的に変更するため、要素の参照です。

Qb) の場合、イベント システムは観察可能なパターンを実装します。誰かが他の人に観察される役割を果たし、それがウィジェットです。オブザーバブルはそれ自身のシグナルに応答できないため (コメント(15)を参照)、このジョブにはオブザーバーが必要です。つまり、ウィジェットによって通知される特定のイベントの他の要素にイベント ハンドラーをアタッチすると、オブザーバブルによる応答が得られます (つまり、コード付きの新しいコメント(16) )。

言われたり書かれたりしていないことは、ウィジェットの構築に 2 種類のオブザーバブルを含めることができるということです。関数を使用する形式的なものthis.element.trigger(eventtype, data)(一般的なトリガー) と、関数として呼び出されthis._trigger(eventtype, event, data)、関数を呼び出すために使用される特定のものです。ウィジェットに接続され、同時にオブザーバブルを通知します-オプションのトリガー!

コメント(9)(11)の下で、両方のタイプを実装しました。の奇妙な点は、接続ドットなどを使用せずoptions triggerに、イベントの名前が付けられている場所を呼び出して通知できることです。namespacewidgetname

呼び出す関数のoptions trigger位置に応じて、次の 2 つのサブカテゴリに分類されます: -construction options trigger構築中にuser options trigger定義された関数を呼び出す a と - ユーザーによって定義された関数を呼び出し、開発者が理解できるように使いやすさを理解するのが難しい場合でも、ウィジェットのセマンティクスを拡張するaユーザーの内線番号を予測しません。

Qc) と Qd) については、答えるのが不安です...

jquery オブジェクトとウィジェットの間の接続に関する私のタイトルの質問の答えは、ウィジェットはイベント システムなどの要素にタイトであり、jquery は要素を見つけてプロパティを添付する中間として機能するため、何もないということです...

テストしてみましょう:

<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>Custom Effect Methods</title>
<style>
  h1 {
     color: #808080;
  }
  h1:hover {
     color: #000000;
     text-decoration: none;
     cursor: pointer;
  }
  a {
     color: #808080;
     background-color: #CCFF33;
  }
</style>
</head>
<body>

<h1>Click Me</h1>

<div class=content>
<p>
<a href="http://speckyboy.com/2010/01/20/25-tutorials-and-resources-for-learning-jquery-ui/">spekyboy</a>
</p>
<p>
<a href="http://www.learningjquery.com/">learning jquery</a>
</p>    
</div>

<script src="../jquery-1.7.1.js"></script>
<script src="../jquery.ui.widget.js"></script>

<script>
////////////////////////Plugin Creation (Widget Factory)////////////////////////
///////////////////////////mycompany.linkLocation///////////////////////////////
(function($, window, document, undefined){
//1. use jQuery.widget() method, the Widget factory, to create plugins
//--------------------------------------------------------------------
$.widget( "mycompany.linkLocation", {
  //2. widget properties "foreground" and "background" (see docs)
  //-------------------------------------------------------------
  options: {
     that: this,
     color: "black",
     "background-color": "transparent",
     done: false,
     //3. a callback from the relevant widget event: linkLocation_onapply (see docs)
     //-----------------------------------------------------------------------------
     //this refers to the element referencing the plugin (element.data property?)
     //evt refers to the custom jquery event object
     _onapply: function(evt, options){
        //4. where is the reference to widget linkLocation for evt.target?
        //----------------------------------------------------------------
        $(evt.target).css(options);
     }
  },
  //5. used during construction by the Widget Factory (see docs)
  //------------------------------------------------------------
  //this refers to the object built by the Widget Factory
  _create: function() {
  },
  //6. used after initialization (see docs)
  //---------------------------------------
  //called as $("selector").linkLocation("option", option_object)
  //or,
  //$("selector").linkLocation("option", "key", "value")
  //this refers to the object built by the Widget Factory
   _setOption: function(key, value){
     this.options[ key ] = value;
  },
  //7. public method apply()
  //------------------------
  //this refers to the object built by the Widget Factory
  apply: function(evt){
     if(!this.options.done){
        //create a jquery object that wraps a specific dom element accessed
        //through the this.element provided by the Widget Factory
        var $this = $(this.element);
        this._css($this);
        ////////////////////////////////////////////////////////////////////////////
        //8. maybe one reason we done all these: add memory and execute only once!//
        ////////////////////////////////////////////////////////////////////////////
        //--------------------------------------------------------------------------
        this.options.done = true;
        //9. if apply() main code is executed once then, 
        //onapply() & trigger are executed only once
        //------------------------------------------
        //(!!!construction options trigger!!!) register observers for event linkLocation_onapply
        //--------------------------------------------------------------------------------------
        this._trigger("_onapply", evt, {color: "white", "background-color": "purple"});
        //(!!!user options trigger!!!) register observers for event linkLocation_onuserapply
        //----------------------------------------------------------------------------------
        this._trigger("_onuserapply", evt, {color: "white", "background-color": "orange"});
        //(!!!general trigger!!!) register observers for the event shown
        //---------------------------------------------------------------
        this.element.trigger("mycompany.linkLocation.customevent", this.options);
     }
  },
  //10. a private method _css()
  //--------------------------
  //this refers to the object built by the Widget Factory
  _css: function($elem){
     $elem.css({
        "background-color": this.options.background,
        "color": this.options.foreground
     });
  },
  _destroy: function() {
  }
});
}(jQuery, window, document));
//11. user overwites public property: this.options
//------------------------------------------------
//for the objects created by the Widget Factory
var options2 = {
   color: "blue",
   "background-color": "yellow",
   //a user supplied function that will trigger a custom event
   _onuserapply: function(evt, options){
      $(evt.target).css(options);
   }
};
//12. construct an object for every <a> element using the Widget Factory
var widgets = $( "a" ).linkLocation(options2);
//13. call widget apply() methods
//-------------------------------
$('h1').on('click', function(evt){
   //14. at this point this refers to h1 element
   //...
   $( "a" ).linkLocation("apply").on("linklocation_onapply", 
      //15. an observable cannot catch it's own signals!!!
      function(evt, data){
         attrs='';
         for(prop in data)
            attrs+=prop+': '+data[prop]+'\n';
         alert('From linklocation_onapply on observable\'s event function, data:\n'+attrs);
      }
   );
});
//16. register observers on widget mycompany.linkLocation
$("div.content").on({
   //an observer to a custom event mycompany.linkLocation.customevent
   "mycompany.linkLocation.customevent": function(evt, data){
      var attrs='';
      for(prop in data)
         attrs+=prop+': '+data[prop]+'\n';
      alert('From mycompany.linkLocation.customevent event on div.content event function, data:\n'+attrs);
   },
   //an observer to a custom event linklocation_onapply
   "linklocation_onapply": function(evt, data){
      var attrs='';
      for(prop in data)
         attrs+=prop+': '+data[prop]+'\n';
      alert('From linklocation_onapply event on div.content event function, data:\n'+attrs);
   },
   //an observer to a custom event linklocation_onuserapply
   "linklocation_onuserapply": function(evt, data){
      var attrs='';
      for(prop in data)
         attrs+=prop+': '+data[prop]+'\n';
      alert('From linklocation_onuserapply event on div.content event function, data:\n'+attrs);
   }
});
</script>

</body>
</html>
于 2013-10-25T18:10:12.500 に答える