javascript 功能/ Noteful App V1 POST和DELETE客户端更新
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 功能/ Noteful App V1 POST和DELETE客户端更新相关的知识,希望对你有一定的参考价值。
/* global $ */
'use strict';
const api = {
search: function (query, callback) {
$.ajax({
type: 'GET',
url: '/api/notes/',
dataType: 'json',
data: query,
success: callback
});
},
details: function (id, callback) {
$.ajax({
type: 'GET',
dataType: 'json',
url: `/api/notes/${id}`,
success: callback
});
},
update: function (id, obj, callback) {
$.ajax({
type: 'PUT',
url: `/api/notes/${id}`,
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(obj),
success: callback
});
},
create: function (obj, callback) {
$.ajax({
type: 'POST',
url: '/api/notes',
contentType: 'application/json',
dataType: 'json',
processData: false,
data: JSON.stringify(obj),
success: callback
});
},
remove: function (id, callback) {
return $.ajax({
type: 'DELETE',
url: `/api/notes/${id}`,
dataType: 'json',
success: callback
});
}
};
/* global $ store api */
'use strict';
const noteful = (function () {
function render() {
const notesList = generateNotesList(store.notes, store.currentNote);
$('.js-notes-list').html(notesList);
const editForm = $('.js-note-edit-form');
editForm.find('.js-note-title-entry').val(store.currentNote.title);
editForm.find('.js-note-content-entry').val(store.currentNote.content);
}
/**
* GENERATE HTML FUNCTIONS
*/
function generateNotesList(list, currentNote) {
const listItems = list.map(item => `
<li data-id="${item.id}" class="js-note-element ${currentNote.id === item.id ? 'active' : ''}">
<a href="#" class="name js-note-show-link">${item.title}</a>
<button class="removeBtn js-note-delete-button">X</button>
</li>`);
return listItems.join('');
}
/**
* HELPERS
*/
function getNoteIdFromElement(item) {
const id = $(item).closest('.js-note-element').data('id');
return id;
}
/**
* EVENT LISTENERS AND HANDLERS
*/
function handleNoteItemClick() {
$('.js-notes-list').on('click', '.js-note-show-link', event => {
event.preventDefault();
const noteId = getNoteIdFromElement(event.currentTarget);
api.details(noteId, detailsResponse => {
store.currentNote = detailsResponse;
render();
});
});
}
function handleNoteSearchSubmit() {
$('.js-notes-search-form').on('submit', event => {
event.preventDefault();
const searchTerm = $('.js-note-search-entry').val();
store.currentSearchTerm = searchTerm ? { searchTerm } : {};
api.search(store.currentSearchTerm, searchResponse => {
store.notes = searchResponse;
render();
});
});
}
function handleNoteFormSubmit() {
$('.js-note-edit-form').on('submit', function (event) {
event.preventDefault();
const editForm = $(event.currentTarget);
const noteObj = {
id: store.currentNote.id,
title: editForm.find('.js-note-title-entry').val(),
content: editForm.find('.js-note-content-entry').val()
};
if (noteObj.id) {
api.update(store.currentNote.id, noteObj, updateResponse => {
store.currentNote = updateResponse;
api.search(store.currentSearchTerm, searchResponse => {
store.notes = searchResponse;
render();
});
});
} else {
api.create(noteObj, createResponse => {
store.currentNote = createResponse;
api.search(store.currentSearchTerm, searchResponse => {
store.notes = searchResponse;
render();
});
});
}
});
}
function handleNoteStartNewSubmit() {
$('.js-start-new-note-form').on('submit', event => {
event.preventDefault();
store.currentNote = {};
render();
});
}
function handleNoteDeleteClick() {
$('.js-notes-list').on('click', '.js-note-delete-button', event => {
event.preventDefault();
const noteId = getNoteIdFromElement(event.currentTarget);
api.remove(noteId, () => {
api.search(store.currentSearchTerm, searchResponse => {
store.notes = searchResponse;
if (noteId === store.currentNote.id) {
store.currentNote = {};
}
render();
});
});
});
}
function bindEventListeners() {
handleNoteItemClick();
handleNoteSearchSubmit();
handleNoteFormSubmit();
handleNoteStartNewSubmit();
handleNoteDeleteClick();
}
// This object contains the only exposed methods from this module:
return {
render: render,
bindEventListeners: bindEventListeners,
};
}());
以上是关于javascript 功能/ Noteful App V1 POST和DELETE客户端更新的主要内容,如果未能解决你的问题,请参考以下文章