-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.js
More file actions
57 lines (52 loc) · 2.3 KB
/
Copy pathModel.js
File metadata and controls
57 lines (52 loc) · 2.3 KB
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
48
49
50
51
52
53
54
55
56
57
// This file is part of DQX - (C) Copyright 2014, Paul Vauterin, Ben Jeffery, Alistair Miles <info@cggh.org>
// This program is free software licensed under the GNU Affero General Public License.
// You can find a copy of this license in LICENSE in the top directory of the source code or at <http://opensource.org/licenses/AGPL-3.0>
define(["jquery", "DQX/Utils", "DQX/Msg"],
function ($, Utils, Msg) {
var Model = function(attrs) {
var that = {};
//Take a copy of the attrs
that.attributes = $.extend({}, attrs);
that.id = 'Model_'+DQX.getNextUniqueID();
that.get = function(attr) {
if (attr)
return that.attributes[attr];
else
return that.attributes;
};
that.set = function(attr, value) {
// Handle both `"key", value` and `{key: value}` -style arguments.
var attrs = {};
if (typeof attr === 'object') {
attrs = attr;
} else {
(attrs = {})[attr] = value;
}
//Iterate over the new data, keeping track of which changed.
var changed = [];
$.each(attrs, function (attr, value) {
if (attr in that.attributes) {
if (that.attributes[attr] != value) {
that.attributes[attr] = value;
changed.push(attr);
}
} else {
DQX.reportError("attr " + attr + " not in model");
}
});
//For each of the changed attributes fire off an event
$.each(changed, function(i, attr) {
Msg.broadcast({id: that.id, change:attr});
});
//If we have any changed fire off the general change event
if (changed.length > 0)
Msg.broadcast({id: that.id, change:true});
return (changed.length > 0);
};
that.on = function(spec, callback) {
Msg.listen("", $.extend(spec, {id: that.id}), callback, that);
};
return that;
};
return Model;
});