TAILIEUCHUNG - JQuery: Novice to Ninja- P15

JQuery: Novice to Ninja- P15:No matter what kind of ninja you are—a cooking ninja, a corporate lawyer ninja, or an actual ninja ninja—virtuosity lies in first mastering the basic tools of the trade. Once conquered, it’s then up to the full-fledged ninja to apply that knowledge in creative and inventive ways. | Construction Ajax and Interactivity 187 Likewise a variable that you declare inside a construct such as a function or an object is said to be local to that construct. This seems simple but it can become messy when we start defining callback methods for our Ajax requests because the callback will often be run in a different context than the one where it was defined. So if you try to refer to this in a callback expecting it to point to your widget namespace you ll be unpleasantly surprised it might be undefined or it might refer to something else entirely. For example var WIDGET 1000 function alert 1000 . good p .click function alert undefined bad When a p tag is clicked our event handler runs in a different context than the widget object itself. So will most likely not exist and if it does it s a different variable to what we wanted anyway . There are a few ways we can deal with this but without being too JavaScripty the easiest way is to store the widget s scope in a variable var WIDGET 1000 function alert 1000 . good var _widget this p .click function alert 1000 . yes By setting _widget to point to this in the context of the widget we ll always be able to refer back to it wherever we are in the code. In JavaScript this is called a closure. The general convention is to name it _this though some people also use self . If it s used in a namespacing object it s best to name it with an underscore _ followed by the widget name. This helps to clarify the scope we re operating in. 188 jQuery Novice to Ninja Client-side Templating Most of the menus and effects we ve seen so far have contained static content. Of course most menu text is unlikely to change but as we explore Ajax-enabled widgets the need to inject and replace text dynamically becomes more of an issue. This brings us to the problem of templating where do you put the markup that will structure the content .

TÀI LIỆU MỚI ĐĂNG
29    7    0    14-08-2020
4    9    0    14-08-2020
108    5    0    14-08-2020
10    4    0    14-08-2020
TÀI LIỆU XEM NHIỀU
TÀI LIỆU HOT
2    7    0
TÀI LIỆU LIÊN QUAN
21    4    0