Backbonejs-view-dollar(jquery)

提供:Dev Guides
移動先:案内検索

BackboneJS-View $(jQuery)

説明

jQueryがページに含まれている場合、各ビューには、ビューの要素内でスコープされたクエリを実行する$関数があります。 このスコープ付きjQuery関数を使用する場合、リスト内の特定の要素を引き出すためにクエリの一部としてモデルIDを使用する必要はなく、HTMLクラス属性にもっと頼ることができます。 実行と同等です:view。$ el.find(selector)

構文

view.$(selector)

パラメーター:

*_selector:_ idやclassなどのさまざまなタイプのセレクターを使用します。

<!DOCTYPE html>
   <head>
      <title>View Example</title>
         <script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
         <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
         <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
   </head>
   <body>
   <div id="myVal">
        <button id="button" data-test="">Click Here</button>
    </div>
   <span id="myLog"></span>
      <script type="text/javascript">

       //The variable contains id selector as 'mydata'
         var myLog = $('#mydata');

        //The variable 'data' is used to display the values
         var data = function(val) {
            document.write(val);
         };

        //'ViewDemo' is a name of the view class
         var ViewDemo = Backbone.View.extend({

        //When click event occurs it activates the defined functions 'myFunc1' and 'myFunc2'
            events: {
               'click [data-test]' : 'myFunc1',
               'click* [data-test]': 'myFunc2',
            },

           //'el' uses '#myVal' as the view reference
            el: $('#myVal'),

           //When user clicks the button, it refers to the button defined within the 'div' tag and
           //it will display the below statements
            myFunc1: function () {
               data('Hello...');
            },
            myFunc2: function () {
               data('Welcome to finddevguides...');
            }
        });

       //'myview' is an instance of the 'ViewDemo' class
        var myview = new ViewDemo();
      </script>
   </body>
</html>

出力

上記のコードがどのように機能するかを確認するために、次の手順を実行しましょう。

  • 上記のコードを dollar-jquery ファイルに保存します
  • このHTMLファイルをブラウザーで開きます。