2018-03-19 20:52:00 +00:00
|
|
|
// client-side js
|
|
|
|
// run by the browser each time your view template is loaded
|
|
|
|
|
|
|
|
(function(){
|
|
|
|
console.log('hello world :o');
|
|
|
|
|
2018-03-19 21:03:02 +00:00
|
|
|
// our default array of dreams
|
2018-03-19 20:52:00 +00:00
|
|
|
const dreams = [
|
|
|
|
'Find and count some sheep',
|
|
|
|
'Climb a really tall mountain',
|
|
|
|
'Wash the dishes'
|
|
|
|
];
|
|
|
|
|
2018-03-19 21:03:02 +00:00
|
|
|
// define variables that reference elements on our page
|
2018-03-19 20:52:00 +00:00
|
|
|
const dreamsList = document.getElementById('dreams');
|
|
|
|
const dreamsForm = document.forms[0];
|
2018-03-19 21:03:02 +00:00
|
|
|
const dreamInput = dreamsForm.elements['dream'];
|
2018-03-19 20:52:00 +00:00
|
|
|
|
2018-03-19 21:03:02 +00:00
|
|
|
// a helper function that creates a list item for a given dream
|
|
|
|
const appendNewDream = function(dream) {
|
2018-03-19 20:52:00 +00:00
|
|
|
const newListItem = document.createElement('li');
|
|
|
|
newListItem.innerHTML = dream;
|
|
|
|
dreamsList.appendChild(newListItem);
|
2018-03-19 21:03:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// iterate through every dream and add it to our page
|
|
|
|
dreams.forEach( function(dream) {
|
|
|
|
appendNewDream(dream);
|
2018-03-19 20:52:00 +00:00
|
|
|
});
|
|
|
|
|
2018-03-19 21:03:02 +00:00
|
|
|
// listen for the form to be submitted and add a new dream when it is
|
2018-03-19 20:52:00 +00:00
|
|
|
dreamsForm.onsubmit = function(event) {
|
2018-03-19 21:03:02 +00:00
|
|
|
// stop our form submission from refreshing the page
|
2018-03-19 20:52:00 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
2018-03-19 21:03:02 +00:00
|
|
|
// get dream value and add it to the list
|
|
|
|
dreams.push(dreamInput.value);
|
|
|
|
appendNewDream(dreamInput.value);
|
|
|
|
|
|
|
|
// reset form
|
|
|
|
dreamInput.value = '';
|
|
|
|
dreamInput.focus();
|
2018-03-19 20:52:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})()
|
|
|
|
|
|
|
|
/*
|
|
|
|
$(function() {
|
|
|
|
console.log('hello world :o');
|
|
|
|
|
|
|
|
$.get('/dreams', function(dreams) {
|
|
|
|
dreams.forEach(function(dream) {
|
|
|
|
$('<li></li>').text(dream).appendTo('ul#dreams');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$('form').submit(function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
var dream = $('input').val();
|
|
|
|
$.post('/dreams?' + $.param({dream: dream}), function() {
|
|
|
|
$('<li></li>').text(dream).appendTo('ul#dreams');
|
|
|
|
$('input').val('');
|
|
|
|
$('input').focus();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
*/
|