Womenes
Evet, popups.ejs
dosyasını oluşturarak farklı pop-up'ları tek bir EJS dosyasında toplamak ve bu sayede kod tekrarını azaltır ve yönetilebilirliği artırırsınız. İşte bunu nasıl yapabileceğinizin adım adım açıklaması:
Adım 1: popups.ejs
Dosyasını Oluşturma
views/popups.ejs
dosyasını oluşturun ve aşağıdaki içeriği ekleyin:
<% if (popupType === 'addNote') { %>
<div id="addNotePopup" class="popup">
<textarea id="noteContent"></textarea>
<button id="saveNoteBtn">Kaydet</button>
<button class="closePopup">Kapat</button>
</div>
<% } else if (popupType === 'deleteNote') { %>
<div id="deleteNotePopup" class="popup">
<p id="deleteNoteMessage"><%= message %></p>
<button id="confirmDeleteBtn">Onayla</button>
<button class="closePopup">Reddet</button>
</div>
<% } %>
Adım 2: index.ejs
Dosyasını Güncelleme
index.ejs
dosyanızda pop-up'ları include
komutu ile çağırın. Bu sayede hangi pop-up'ın yükleneceğini dinamik olarak belirleyebilirsiniz. Örneğin:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Not Yönetimi</title>
<link rel="stylesheet" href="/styles.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>Not Yönetimi</h1>
<button id="addNoteBtn">Not Ekle</button>
<div id="notes">
<% notes.forEach(function(note) { %>
<div class="note">
<h2><%= note.title %></h2>
<p><%= note.content %></p>
<button class="editNoteBtn" data-id="<%= note.id %>">Düzenle</button>
<button class="deleteNoteBtn" data-id="<%= note.id %>" data-name="<%= note.name %>">Sil</button>
</div>
<% }); %>
</div>
<!-- Pop-up'ları Burada Dahil Et -->
<% include popups %>
<script src="/scripts.js"></script>
</body>
</html>
Adım 3: Sunucu Tarafını Güncelleme
Sunucu tarafında hangi pop-up'ın gösterileceğini belirlemek için res.render
işlevine popupType
ve diğer gerekli bilgileri gönderin:
app.get('/', (req, res) => {
res.render('index', { notes: notes, popupType: null, message: null });
});
app.post('/add-note', (req, res) => {
const note = {
id: Date.now().toString(),
content: req.body.content,
name: 'User' // Sabit isim, gerçekte kullanıcı oturumundan alınmalı
};
notes.push(note);
res.redirect('/');
});
app.post('/delete-note', (req, res) => {
const noteId = req.body.id;
const noteIndex = notes.findIndex(note => note.id === noteId);
if (noteIndex !== -1) {
notes.splice(noteIndex, 1);
}
res.redirect('/');
});
Adım 4: jQuery Kodunu Güncelleme
jQuery kodunu pop-up'ların gösterilmesi için güncelleyin. Örneğin, data-popup-type
ile hangi pop-up'ın gösterileceğini belirleyin:
$(document).ready(function() {
// Not Ekleme Pop-up'ını Aç
$('#addNoteBtn').on('click', function() {
$.get('/get-popup', { popupType: 'addNote' }, function(response) {
$('body').append(response);
$('#addNotePopup').show();
});
});
// Pop-up'ı Kapat
$('body').on('click', '.closePopup', function() {
$(this).parent('.popup').hide().remove();
});
// Not Kaydetme
$('body').on('click', '#saveNoteBtn', function() {
var noteContent = $('#noteContent').val();
$.post('/add-note', { content: noteContent }, function(response) {
// Yeni notu ekrana ekle veya sayfayı yeniden yükle
location.reload();
});
});
// Not Silme Pop-up'ını Aç
$('body').on('click', '.deleteNoteBtn', function() {
var noteId = $(this).data('id');
var noteName = $(this).data('name');
$.get('/get-popup', { popupType: 'deleteNote', message: 'Merhaba ' + noteName + ', bu notu silmek istediğinizden emin misiniz? Bu işlemin geri dönüşü olmayacaktır.' }, function(response) {
$('body').append(response);
$('#deleteNotePopup').show();
$('#confirmDeleteBtn').data('id', noteId);
});
});
// Not Silme Onayı
$('body').on('click', '#confirmDeleteBtn', function() {
var noteId = $(this).data('id');
$.post('/delete-note', { id: noteId }, function(response) {
// Notu ekrandan kaldır veya sayfayı yeniden yükle
location.reload();
});
});
});
Adım 5: Yeni Bir GET Endpoint Oluşturma
Pop-up'ları dinamik olarak çekmek için yeni bir GET endpoint oluşturun:
app.get('/get-popup', (req, res) => {
const { popupType, message } = req.query;
res.render('popups', { popupType: popupType, message: message });
});
Bu yapılandırma ile pop-up'ları dinamik olarak yükleyebilir ve yönetebilirsiniz. Tek bir EJS dosyasında farklı pop-up'ları tanımlayarak, kod tekrarını azaltır ve bakımını kolaylaştırmış olursunuz.