This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bookListItem = $('<li book-id=' + id + '><a href="book.html"><h2>' + title + '</h2><p>' + realText + '</p></a></li>'); | |
bookListItem.click(function(){ | |
book_id = $(this).attr("book-id"); | |
localStorage.setItem("id", book_id); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// xml document | |
var xml = ('<section> ' + | |
' <enclosure url="a"/> ' + | |
' <other_tag> ' + | |
' <enclosure url="b"/>' + | |
' </other_tag> ' + | |
'</section> ' ) | |
$(xml).find("enclosure").each(function(i) { | |
console.log( i.attr('url') ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<section> | |
<enclosure url="a" /> | |
<other_tag> | |
<enclosure url="b" /> | |
</other_tag> | |
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(xml).find("enclosure").each(function(i) { | |
console.log( i.attr('url') ); // -> TypeError: i.attr is not a function | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(xml).find("enclosure").each(function(i) { | |
console.log( $(this).attr('url') ); // note that I'm using $(this) and not i | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $enclosure_search = $(xml).find("enclosure") | |
// different values for similar calls | |
$enclosure_search // -> Object { 0: <enclosure>, 1: <enclosure>, length: 2, prevObject: Object, context: undefined, selector: "enclosure" } | |
$enclosure_search[0] // -> <enclosure url="a"> | |
$($enclosure_search[0]) // -> Object { 0: <enclosure>, context: <enclosure>, length: 1 } | |
// Get the first enclosure tag explicitly - #attr() can no longer be called | |
$enclosure_search[0].attr('url') // -> TypeError: $enclosure_search[0].attr is not a function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TODO: needs more jQuery | |
$( $(1)[0] + $(1 + 1)[0] + $(100).length ).first()[0] // -> 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pre.code { | |
border: 1px solid #919191; | |
color: rgb(153, 58, 23); | |
padding: 2px; | |
padding-left: 10px; | |
border-radius: 2px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initial code: | |
var xml = xhr.response; | |
var xmlDoc = $.parseXML( xml ); // xml variable is never referenced after this line | |
var newXML = $( xmlDoc ); // redundant form of above line, does nothing | |
var title = newXML.find( "title" ); // misleading, should be plural 'titles' (#find returns all title tags in the document) | |
var enclosure = newXML.find("enclosure"); // never used | |
var currTitle; // unnecessary | |
var currEnclosure; // unnecessary, never used | |
// Refactored into: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// This is an excerpt from the app's search result generation code | |
/// entry is a book's json data | |
var book = new Book({'json': entry}); // -> Book object with keys: id, title, description, and json | |
bookCache[book.id] = book; // add book to cache with its id as a key | |
// Book id is in the link href: for example href="chapters.html?9396" | |
bookListItem = $('<li><a href="chapters.html?'+ book.id + '"><h2>' + book.title + '</h2><p>' + book.description + '</p></a></li>'); |