Created
May 16, 2016 04:14
-
-
Save brunops/acade902983242d39ffa44dff34b4a00 to your computer and use it in GitHub Desktop.
avoid anon callbacks
This file contains hidden or 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
// don't do this | |
$(document).ready(function () { | |
$.get('/foo', function (response) { | |
var list = $('<ul />') | |
response.list.forEach(function (item) { | |
list.append($('<li />').text(item)) | |
}) | |
$('#content').append(list) | |
}) | |
}) | |
// SEPARATE YOUR CODE INSTEAD | |
// do this: | |
function getList(list) { | |
var listEl = $('<ul />') | |
list.forEach(function (item) { | |
listEl.append($('<li />').text(item)) | |
}) | |
return listEl | |
} | |
function render(element) { | |
$('#content').html(element) | |
} | |
function ajaxSuccess(response) { | |
var list = getList(response.list) | |
render(list) | |
} | |
$(document).ready(function () { | |
$.get('/foo', ajaxSuccess) | |
}) | |
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Document</title> | |
</head> | |
<body> | |
<div id="content"></div> | |
<script src="app.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment