Skip to content

Conversation

@zyahav
Copy link
Contributor

@zyahav zyahav commented Dec 22, 2016

No description provided.

@zyahav
Copy link
Contributor Author

zyahav commented Dec 22, 2016

new

@@ -0,0 +1,18 @@
function ajax_get(url, callback) {
xmlhttp = new XMLHttpRequest();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing var statement.

function ajax_get(url, callback) {
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen is readyState!==4 or status!==200. Right now, nothing (your callback isn't called), which is probably not good.
You can simulate this by calling ajax_get in the console, or by a click of a button, after you have killed your local HTTP server.

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log('responseText:' + xmlhttp.responseText);
try {
var data = JSON.parse(xmlhttp.responseText);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: while this is fine in javascript, it's customary (and mandatory in other languages) to move the var data outside of the try, because if the JSON.parse throws an error, data will remain undefined.
So you can do this instead:
var data = null; // or undefined; try{ data = JSON.parse(...) } . . . callback(data);

console.log(err.message + " in " + xmlhttp.responseText);
return;
}
callback(data);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you fine with the callback being called with undefined (if the responseText cannot be JSON-parsed? Maybe throw an error instead (or signal something else to the callback (two standard options: 1) have two callbacks - one for success - with data, one for failure - with some error message) 2) have a first param to the callback be error - if it's null, the next param is the parsed json)

requirejs.config({
baseUrl: 'js',
paths: {
ajax: 'ajax',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use requirejs' paths for other people's code, not yours.
This just alias updatePhoneBook to update. If you want it to be called update, rename the file; if you want it to be called updatePhoneBook, change the require call for it.

element.setAttributeNode(attribute);
element.innerHTML = info;
return element;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should put these functions in the exports or module.exports. Right now they are globally available and used, only if you require them. This is called a side-effect and it's bad.
if you put these in exports, the caller can then be:
require('updatePhoneBook', function(updatePhoneBook) { updatePhoneBook.insertJson()); }

<html>
<head>
<link rel="stylesheet" href="style/style.css">
<script data-main="js/config" src="js/lib/require/require.js"></script>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data-main should be a javascript file that actually does the work (the several sines below: the calls for ajax_get, insertJson). Please move these calls into config and rename it to be main.js.

window.onload = function() {
require(['config'], function() {
require(['ajax','update'], function() {
ajax_get('json/data.json', function(data) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style error: insertJson (camelCase) vs. ajax_get (all lower case with underscore). Please use camelCase (standard for javascript).

@zyahav
Copy link
Contributor Author

zyahav commented Jan 2, 2017

OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants