Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/develop-activity/skeletons/Web (sugar >= 0.100)/js/controller.js
blob: 36962ba8a92724d148f310854e48b56788655328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
define(function (require) {

    var controller = {};

    // Take a model and view and act as the controller between them.
    controller.Controller = function (model, view) {
        this.model = model;
        this.view = view;

        this.ENTER_KEY = 13;
        this.ESCAPE_KEY = 27;
    };

    controller.Controller.prototype.loadItems = function (items) {
        this.model.load(items);
        var list = document.getElementById("todo-list");
        list.innerHTML = this.view.show(items);
    };

    controller.Controller.prototype.addItem = function (title) {
        if (title.trim() === '') {
            return false;
        }
        var item = this.model.create(title);
        var list = document.getElementById("todo-list");
        list.innerHTML += this.view.show([item]);
        return true;
    };

    controller.Controller.prototype.removeItem = function (id) {
        this.model.remove(id);
        this.loadItems(this.model.items);
    };

    controller.Controller.prototype.toggleComplete = function (id, checkbox) {
        var completed = checkbox.checked ? 1 : 0;
        this.model.update(id, {completed: completed});
        this.loadItems(this.model.items);
    };

    return controller;

});