Özellik | MVP | MVC |
---|---|---|
Olay Dinleme | View içinde Presenter’a verilir | View içinde Controller’a verilir |
View-Controller ilişkisi | Pasif View → Presenter yönlendirir | View → Controller direkt tetikler |
Kim DOM’a müdahale eder? | View | View |
İş akış yönü | View → Presenter → Model → View | View → Controller → Model → View |
Created
June 15, 2025 06:43
-
-
Save cemtopkaya/aa1675d2f6fabe4aab8a4cd7c4c22d08 to your computer and use it in GitHub Desktop.
MVC desenine uygun vanillajs web sayfası örneği
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// controller.js KONTROLLER Katmanı (Beyin) | |
class UserController { | |
constructor(model, view) { | |
this.model = model; | |
this.view = view; | |
this.view.onLoadUsersClick(this.handleLoadUsers.bind(this)); | |
} | |
async handleLoadUsers() { | |
try { | |
const users = await this.model.getUsers(); | |
this.view.renderUsers(users); | |
} catch (err) { | |
this.view.renderError('Kullanıcılar yüklenemedi.'); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/model.js | |
/view.js | |
/controller.js | |
/index.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- index.html HTML View Katmanı --> | |
<!DOCTYPE html> | |
<html lang="tr"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>MVC Örneği</title> | |
</head> | |
<body> | |
<h1>Kullanıcı Listesi</h1> | |
<button id="load-users">Kullanıcıları Yükle</button> | |
<ul id="user-list"></ul> | |
<script src="model.js"></script> | |
<script src="view.js"></script> | |
<script src="controller.js"></script> | |
<script> | |
const app = new UserController(new UserModel(), new UserView()); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// model.js MODEL Katmanı | |
class UserModel { | |
async getUsers() { | |
const res = await fetch('https://jsonplaceholder.typicode.com/users'); | |
return res.json(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// view.js HTML View Katmanı ile Etkileşimi | |
class UserView { | |
constructor() { | |
this.loadButton = document.getElementById('load-users'); | |
this.userList = document.getElementById('user-list'); | |
} | |
onLoadUsersClick(callback) { | |
this.loadButton.addEventListener('click', callback); | |
} | |
renderUsers(users) { | |
this.userList.innerHTML = ''; | |
users.forEach(user => { | |
const li = document.createElement('li'); | |
li.textContent = user.name; | |
this.userList.appendChild(li); | |
}); | |
} | |
renderError(message) { | |
this.userList.innerHTML = `<li style="color:red;">${message}</li>`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment