Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/develop-activity/skeletons/Web (sugar >= 0.100)/js/model.js
blob: f8e028ff9e1f18b6cf01c027be14e9397060edf9 (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
44
45
46
47
define(function (require) {

    var model = {};

    model.Model = function () {
        this.items = [];
    };

    model.Model.prototype.load = function (items) {
        this.items = items;
    };

    model.Model.prototype.create = function (title) {
        title = title || '';

        var newItem = {
            id: new Date().getTime(),
            title: title.trim(),
            completed: 0
        };

        this.items.push(newItem);
        return newItem;
    };

    model.Model.prototype.remove = function (id) {
        for (var i = 0; i < this.items.length; i++) {
            if (this.items[i].id == id) {
                this.items.splice(i, 1);
                break;
            }
        }
    };

    model.Model.prototype.update = function (id, updateData) {
        for (var i = 0; i < this.items.length; i++) {
            if (this.items[i].id == id) {
                for (var x in updateData) {
                    this.items[i][x] = updateData[x];
                }
            }
        }
    };

    return model;

});