recipes/app/js/app.js
2016-02-24 11:48:09 +00:00

129 lines
3.8 KiB
JavaScript

/**
* Created by Martin on 24/02/2016.
*/
(function () {
"use strict";
console.log('GO!');
var $list = $('#listContainer');
var displayList = function (obj) {
var html = new EJS({url: '/partials/list.ejs'}).render(obj);
console.log(html);
$list.empty();
$list.append(html);
$("#listContainer .entry").not('.emptyMessage').click(function () {
console.log('Clicked list. ' + this.id);
getRecipe(this.id);
});
}, displayPage = function (obj) {
var $bodyContents = $('#bodyContents');
if (obj.list[0].body.length > 0) {
$bodyContents.empty();
$bodyContents.append(obj.list[0].body);
}
}, getRecipe = function (id) {
console.log('get recipe');
var url = '/recipes/entry/' + id;
var data = '';
$.ajax({
type: 'GET',
url: url,
data: data,
dataType: 'json',
timeout: 10000,
//contentType: ('application/json'),
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
},
success: function (data) {
console.log(data);
displayPage(data);
},
error: function (xhr, type) {
console.log("ajax error");
console.log(xhr);
console.log(type);
}
});
},
getList = function () {
var url = '/recipes/list';
$.ajax({
type: 'GET',
url: url,
data: '',
dataType: 'json',
timeout: 10000,
//contentType: ('application/json'),
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
},
success: function (data) {
console.log(data);
displayList(data);
},
error: function (xhr, type) {
console.log("ajax error");
console.log(xhr);
console.log(type);
}
});
}, addNew = function (newUrl) {
var url = '/recipes/add';
var data = {url: JSON.stringify(newUrl)};
$.ajax({
type: 'POST',
url: url,
data: data,
dataType: 'json',
timeout: 10000,
//contentType: ('application/json'),
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
},
success: function (data) {
console.log(data);
// displayList(data);
},
error: function (xhr, type) {
console.log("ajax error");
console.log(xhr);
console.log(type);
}
});
},
start = function () {
getList();
};
$('#addNew').click(function () {
var url = prompt("Please enter a new url", "");
if (url != null) {
console.log('Adding: ' + url);
addNew(url);
}
});
start();
})();