Skip to content

Instantly share code, notes, and snippets.

@wiesson

wiesson/frontend.html

Last active Aug 29, 2015
Embed
What would you like to do?
XMLHttpRequest "DELETE" call to the flask backend. No jquery is required.
<button id="deleteDevice" class="btn btn-danger btn-xs"
data-link="{{ url_for('monitor.delete_device', id_device=device.id_device) }}">
<span class="glyphicon glyphicon-remove-circle"></span>
Delete Device
</button>
<script type="text/javascript">
var t = document.getElementById('deleteDevice');
t.addEventListener('click', function () {
deleteDevice(t.dataset.link);
});
function deleteDevice(url) {
var xhr = new XMLHttpRequest();
xhr.open("DELETE", url, true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
window.location.assign('{{ url_for('monitor.index') }}');
console.log(xhr.responseText);
} else {
console.error(xhr.statusText);
}
}
};
xhr.onerror = function (e) {
console.error(xhr.statusText);
};
xhr.send(null);
}
</script>
# Flask backend
@mod.route('/<string:id_device>')
@mod.route('/<string:id_device>', methods=['DELETE'])
@login_required
def view_device(id_device):
"""
View a single device and delete a single device
"""
if request.method == 'DELETE':
Devices.query.filter_by(id_device=id_device).delete()
db.session.commit()
# if id_device not in Device.query.filter_by(id_device=id_device).first():
flash('Device %s has successfully been deleted.' % id_device, 'success')
return ""
# else ...
return render_template(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.