Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic api query example, update readme #1

Merged
merged 3 commits into from
Oct 31, 2017
Merged

basic api query example, update readme #1

merged 3 commits into from
Oct 31, 2017

Conversation

j-andrews7
Copy link
Owner

No description provided.

@melalawi melalawi removed their request for review October 31, 2017 03:53
@melalawi melalawi self-assigned this Oct 31, 2017
<p id="basics"></p>
<br>
<p id="summary"></p>
<script>
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's nothing wrong with including JavaScript within an HTML file. What I said to avoid was stuff like this:

<a href="#" onclick="console.log('This is some JavaScript');">Click Me</a>

Copy link
Owner Author

Choose a reason for hiding this comment

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

Oh, yeah, inline stuff is awful.

index.html Outdated
<br>
<p id="summary"></p>
<script>
(function() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your code will be executed as soon as it is encountered. Notice the document.getElementById() call. It relies on the fact that the DOM is ready, so by immediately making this call, there's a change that it returns null if the page hasn't be parsed yet. This is fine for your current setup (placing the script at the end of the body), as all the html will be parsed before the JavaScript is reached. However, you'll likely segregate your JS code into their own files in the future, and in doing so, you'll encounter this issue.

Look into this: https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded

index.html Outdated
document.getElementById("searchButton").addEventListener('click', makeQuery);

function makeQuery() {
httpRequest = new XMLHttpRequest();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider replacing XMLHttpRequest with fetch. On top of being newer, Fetch's syntax is superior and it is generally less of a hassle to work with.

Here's where I use fetch in one of my projects.

index.html Outdated
var term = document.getElementById("query").value;

if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please don't use alert(). It provides a poor user experience. Use console.error(string) for now. In the future, we can incorporate errors directly into the page.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Oh, I think that was a holdover from an example I was looking at. Will do as you suggest.

index.html Outdated
var response = JSON.parse(httpRequest.responseText);
var symbol = response.hits[0].symbol
var geneId = response.hits[0]._id
document.getElementById("basics").innerHTML = 'Gene Symbol: ' + symbol + "; Gene ID: " + geneId;
Copy link
Collaborator

@melalawi melalawi Oct 31, 2017

Choose a reason for hiding this comment

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

Directly modifying an element's innerHTML is very expensive. On top of that, parsing external data as HTML makes you vulnerable to Cross-site Scripting (XSS).

Here's an example.

Use textContent instead.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Ah, good to know. Yet again, poor examples abound.

index.html Outdated
if (sumRequest.status === 200) {
var response = JSON.parse(sumRequest.responseText);
var summary = response.summary
document.getElementById("summary").innerHTML = 'Gene Summary: ' + response.summary;
Copy link
Collaborator

Choose a reason for hiding this comment

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

See above.

@j-andrews7
Copy link
Owner Author

All expected changes (and a few others) made. fetch is nice.

I'm going to go ahead and merge, as I don't think it needs another review. Next up will be adding the species filter and fleshing out the information displayed.

@j-andrews7 j-andrews7 merged commit 1dba5f8 into master Oct 31, 2017
@j-andrews7 j-andrews7 deleted the query_api branch October 31, 2017 14:01
@melalawi
Copy link
Collaborator

http://markdalgleish.com/2011/03/self-executing-anonymous-functions/

More about self-executing functions. I don't think you need to have one here, but it's good practice to properly scope stuff.

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.

None yet

2 participants