Add button to update movie informations
parent
76d3d44af4
commit
1736320d81
|
@ -62,6 +62,16 @@ module Librarix
|
||||||
redirect to('/')
|
redirect to('/')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
post '/update' do
|
||||||
|
movie = Librarix::Redis::Movie.new(params[:id]).update
|
||||||
|
|
||||||
|
if request.xhr?
|
||||||
|
slim :'partials/movie', layout: false, locals: {movie: movie}
|
||||||
|
else
|
||||||
|
redirect to('/')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
post '/remove' do
|
post '/remove' do
|
||||||
Librarix::Redis::Movie.new(params[:id]).remove
|
Librarix::Redis::Movie.new(params[:id]).remove
|
||||||
|
|
||||||
|
|
|
@ -8,42 +8,75 @@ function request(method, url, data, loadevent) {
|
||||||
req.send(data)
|
req.send(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
function initButtons(element) {
|
function init() {
|
||||||
var buttons = element.querySelectorAll('button[data-action=remove-movie]');
|
var movies = document.querySelectorAll('.movie');
|
||||||
for (var i = 0; i < buttons.length; i++) {
|
for (var i = 0; i < movies.length; i++) {
|
||||||
buttons[i].addEventListener('click', function(event) {
|
new Movie(movies[i].dataset.id);
|
||||||
event.preventDefault();
|
|
||||||
removeMovie(this.previousElementSibling.value)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
var view_buttons = element.querySelectorAll('button[data-action=view-movie]');
|
|
||||||
for (var i = 0; i < view_buttons.length; i++) {
|
|
||||||
view_buttons[i].addEventListener('click', function(event) {
|
|
||||||
event.preventDefault();
|
|
||||||
viewMovie(this.previousElementSibling.value)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeMovie(id) {
|
var Movie = function(id) {
|
||||||
var data = new FormData();
|
this.id = parseInt(id);
|
||||||
data.append('id', id);
|
this.element = document.querySelector('.movie[data-id="'+id+'"]');
|
||||||
|
|
||||||
request('POST', '/remove', data, function(e) {
|
var remove_button = this.element.querySelector('button[data-action="remove-movie"]');
|
||||||
document.querySelector('.movie[data-id="' + id + '"]').parentNode.remove();
|
var update_button = this.element.querySelector('button[data-action="update-movie"]');
|
||||||
});
|
var view_button = this.element.querySelector('button[data-action="view-movie"]');
|
||||||
}
|
|
||||||
|
|
||||||
function viewMovie(id) {
|
if (remove_button) {
|
||||||
var data = new FormData();
|
remove_button.addEventListener('click', function(e) {
|
||||||
data.append('id', id);
|
e.preventDefault();
|
||||||
|
this.remove();
|
||||||
|
}.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
request('POST', '/view', data, function(e) {
|
if (update_button) {
|
||||||
document.querySelector('.movie[data-id="' + id + '"] button[data-action="view-movie"]').parentNode.remove();
|
update_button.addEventListener('click', function(e) {
|
||||||
});
|
e.preventDefault();
|
||||||
}
|
this.update();
|
||||||
|
}.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (view_button) {
|
||||||
|
view_button.addEventListener('click', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
this.view();
|
||||||
|
}.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.formData = function() {
|
||||||
|
var data = new FormData();
|
||||||
|
data.append('id', this.id);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.update = function() {
|
||||||
|
request('POST', '/update', this.formData(), function(e) {
|
||||||
|
oldelem = this.element;
|
||||||
|
|
||||||
|
tmp_elem = document.createElement('div');
|
||||||
|
tmp_elem.innerHTML = e.target.response;
|
||||||
|
|
||||||
|
this.element = tmp_elem.firstChild;
|
||||||
|
|
||||||
|
oldelem.parentNode.replaceChild(this.element, oldelem);
|
||||||
|
}.bind(this));
|
||||||
|
};
|
||||||
|
|
||||||
|
this.remove = function() {
|
||||||
|
request('POST', '/remove', this.formData(), function(e) {
|
||||||
|
this.element.parentNode.remove();
|
||||||
|
}.bind(this));
|
||||||
|
};
|
||||||
|
|
||||||
|
this.view = function() {
|
||||||
|
request('POST', '/view', this.formData(), function(e) {
|
||||||
|
this.element.querySelector('button[data-action="view-movie"]').parentNode.remove();
|
||||||
|
}.bind(this));
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', function(event) {
|
document.addEventListener('DOMContentLoaded', function(event) {
|
||||||
initButtons(document);
|
init();
|
||||||
});
|
});
|
||||||
|
|
|
@ -15,6 +15,9 @@
|
||||||
form method="post" action="/view"
|
form method="post" action="/view"
|
||||||
input type="hidden" name="id" value="#{movie.id}"
|
input type="hidden" name="id" value="#{movie.id}"
|
||||||
button type="submit" data-action="view-movie" View
|
button type="submit" data-action="view-movie" View
|
||||||
|
form method="post" action="/update"
|
||||||
|
input type="hidden" name="id" value="#{movie.id}"
|
||||||
|
button type="submit" data-action="update-movie" Update
|
||||||
form method="post" action="/remove"
|
form method="post" action="/remove"
|
||||||
input type="hidden" name="id" value="#{movie.id}"
|
input type="hidden" name="id" value="#{movie.id}"
|
||||||
button type="submit" data-action="remove-movie" Remove
|
button type="submit" data-action="remove-movie" Remove
|
||||||
|
|
Loading…
Reference in New Issue