Александра Шинкевич (@neesoglasnaja)
1. HTML
1. HTML
2. JS
1. HTML
2. JS
3. AJAX
<!-- ... -->
<body>
<script src="js/utils.js"></script>
<script src="js/models.js"></script>
<script src="js/views.js"></script>
<script src="js/controllers.js"></script>
<script src="js/app.js"></script>
</body>
</html>
<!-- ... -->
<body>
<script src="js/utils.js"></script>
<script src="js/models.js"></script>
<script src="js/views.js"></script>
<script src="js/controllers.js"></script>
<script src="js/app.js"></script>
</body>
</html>
var list = new ListController();
<!-- ... -->
<body>
<script src="js/utils.js"></script>
<script src="js/models.js"></script>
<script src="js/views.js"></script>
<script src="js/controllers.js"></script>
<script src="js/app.js"></script>
</body>
</html>
function ListController() {
var model = new TasksModel();
var view = new ListView(model);
view.addCreateTaskHandler(function(taskTitle) { ... });
view.addCheckedHandler(function(id) { ... });
}
function ListController() {
var model = new TasksModel();
var view = new ListView(model);
view.addCreateTaskHandler(function(taskTitle) { ... });
view.addCheckedHandler(function(id) { ... });
}
<!-- ... -->
<body>
<script src="js/utils.js"></script>
<script src="js/models.js"></script>
<script src="js/views.js"></script>
<script src="js/controllers.js"></script>
<script src="js/app.js"></script>
</body>
</html>
function ListView(model) {
var html = $( ... );
customEvents.registerEvent('UpdateList');
customEvents.addEventListener('UpdateList', function() { ... });
return {
addCreateTaskHandler: function(handler) { ... },
addCheckedHandler: function(handler) { ... }
};
}
function ListView(model) {
var html = $( ... );
customEvents.registerEvent('UpdateList');
customEvents.addEventListener('UpdateList', function() { ... });
return {
addCreateTaskHandler: function(handler) { ... },
addCheckedHandler: function(handler) { ... }
};
}
function ListView(model) {
var html = $( ... );
customEvents.registerEvent('UpdateList');
customEvents.addEventListener('UpdateList', function() { ... });
return {
addCreateTaskHandler: function(handler) { ... },
addCheckedHandler: function(handler) { ... }
};
}
<!-- ... -->
<body>
<script src="js/utils.js"></script>
<script src="js/models.js"></script>
<script src="js/views.js"></script>
<script src="js/controllers.js"></script>
<script src="js/app.js"></script>
</body>
</html>
function TasksModel() {
return {
data: {},
getAll: function() { ... },
addItem: function(name) { ... },
deleteItem: function(id) { ... },
};
};
addItem: function(name) {
this.data[(new Date()).getTime()] = name;
customEvents.dispatchEvent('UpdateList');
}
<!-- ... -->
<body>
<script src="js/utils.js"></script>
<script src="js/models.js"></script>
<script src="js/views.js"></script>
<script src="js/presenters.js"></script>
<script src="js/app.js"></script>
</body>
</html>
<!-- ... -->
<body>
<script src="js/utils.js"></script>
<script src="js/models.js"></script>
<script src="js/views.js"></script>
<script src="js/presenters.js"></script>
<script src="js/app.js"></script>
</body>
</html>
var list = new ListPresenter();
<!-- ... -->
<body>
<script src="js/utils.js"></script>
<script src="js/models.js"></script>
<script src="js/views.js"></script>
<script src="js/presenters.js"></script>
<script src="js/app.js"></script>
</body>
</html>
function ListPresenter() {
var model = new TasksModel();
var view = new ListView(model.getAll());
view.addCreateTaskHandler(function(taskTitle) { ... });
view.addCheckedHandler(function(id) { ... });
customEvents.registerEvent('UpdateList');
customEvents.addEventListener('UpdateList', function() { ... });
}
<!-- ... -->
<body>
<script src="js/utils.js"></script>
<script src="js/models.js"></script>
<script src="js/views.js"></script>
<script src="js/presenters.js"></script>
<script src="js/app.js"></script>
</body>
</html>
function ListView(data) {
var html = $( ... );
return {
addCreateTaskHandler: function(handler) { ... },
addCheckedHandler: function(handler) { ... },
updateView: function(data) { ... }
};
}
Это такой механизм, при котором изменение значения модели с любой стороны (View или Model) моментально вступает в силу на другой.
Open-source фреймворк, реализующий Model-View-ViewModel на Javascript и data binding.
<body>
<!-- Разметка html -->
<script src="js/knockout-3.4.0.min.js"></script>
<script src="js/viewmodel.js"></script>
</body>
</html>
<form data-bind="submit: addItem">
<ul data-bind="foreach: items">
<li>
<label data-bind="click: $parent.removeItem">
<input type="checkbox">
<span data-bind="text: $data"></span>
</label>
</li>
<!-- ... -->
<body>
<!-- Разметка html -->
<script src="js/knockout-3.4.0.min.js"></script>
<script src="js/viewmodel.js"></script>
</body>
</html>
var ListViewModel = function(items) {
return {
items: ko.observableArray(items),
itemToAdd: ko.observable(""),
addItem: function() { ... },
removeItem: function() { ... },
};
};
ko.applyBindings(new ListViewModel([]));
var ListViewModel = function(items) {
return {
items: ko.observableArray(items),
itemToAdd: ko.observable(""),
addItem: function() { ... },
removeItem: function() { ... },
};
};
ko.applyBindings(new ListViewModel([]));
var ListViewModel = function(items) {
return {
items: ko.observableArray(items),
itemToAdd: ko.observable(""),
addItem: function() { ... },
removeItem: function() { ... },
};
};
ko.applyBindings(new ListViewModel([]));
bit.ly/wsd-mv
Александра Шинкевич (@neesoglasnaja)