w6d4 Flashcards

1
Q

Explain how DOM elements bubble

A

DOM events typically bubble: if an event fires on element X which lives inside element Y, then after being fired on X the event will bubble up and be fired on Y.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between the currentTarget and the target?

A

The currentTarget is the element that the event handler corresponds to (i.e. a ul), and the target is what actually activates the event handler (i.e. a li)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is event delegation?

A

A pattern of installing a single event handler on a parent to catch events on its children.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the 3 element version of jQuery#on ?

A

It sets up an event delegation; the first is the event, then the child element to target, then the callback.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the method to fire an event from within code?

A

jQuery#trigger

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the object that we must define a new jQuery plugin on?

A

$.fn

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an example of a simple jQuery plugin?

A

$.fn.greenify = function() {
this.css( “color”, “green” );
return this;
}

$( “a” ).greenify().addClass( “greenified” );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can we prevent collisions with other JS libraries which use $?

A

Use an IIFE:

(function ( $ ) {

$.fn.greenify = function() {
    this.css( "color", "green" );
    return this;
};

}( jQuery ));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the best practice when defining methods on .fn?

A

Define one function on it and overload it to take different numbers/types of arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do we deal with #each?

A

Return it’s RESULTS (i.e. NOT Ruby!):

$.fn.myNewPlugin = function() {

    return this.each(function() {
        // Do something to each element here.
    });

};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can we set up a function to take what is (essentially) an options hash?

A

(function ( $ ) {

$.fn.greenify = function( options ) {
        // This is the easiest way to have default options.
        var settings = $.extend({
            // These are the defaults.
            color: "#556b2f",
            backgroundColor: "white"
        }, options );
    // Greenify the collection based on the settings variable.
    return this.css({
        color: settings.color,
        backgroundColor: settings.backgroundColor
    });

};

}( jQuery ));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a simple jQuery plugin that can show the location of a link?

A

(function( $ ) {

$.fn.showLinkLocation = function() {

    this.filter( "a" ).append(function() {
        return " (" + this.href + ")";
    });

    return this;

};

}( jQuery ));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

See an example of a full widget (plugin) file

A

(function () {
if (typeof NameApp === “undefined”) {
window.NameApp = {};
}

$.NameSearch = function (el) {

this. $el = $(el);
this. $input = this.$el.find("input[name=query]");
this. $ul = this.$el.find("ul.names");

this.$input.on("input", this.render.bind(this));
this.render();   };

$.NameSearch.prototype.searchResults = function () {
return NameApp.Name.search(this.$input.val());
};

$.NameSearch.prototype.render = function () {
this.$ul.empty();

    this.searchResults().forEach((function (name) {
      var $li = $("<li>");
      $li.text(name.fullName());
      this.$ul.append($li);
    }).bind(this));
  };
  $.fn.nameSearch = function () {
    return this.each(function () {
      new $.NameSearch(this);
    });
  }
})();

$(function () {
$(‘.name-search’).nameSearch();
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly