diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-05-18 18:51:16 +0200 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-05-18 18:51:16 +0200 |
commit | e2e9a79aa3acb0d3a02d81ddbfbdcacc676db2d2 (patch) | |
tree | 85640355ebe88fd3627f9701b803e85713854200 /js/service | |
parent | aae89820dbb1b21b14272f5358761b9876e8ea09 (diff) |
add model
Diffstat (limited to 'js/service')
-rw-r--r-- | js/service/model.js | 77 | ||||
-rw-r--r-- | js/service/publisher.js | 35 |
2 files changed, 112 insertions, 0 deletions
diff --git a/js/service/model.js b/js/service/model.js new file mode 100644 index 000000000..a28608329 --- /dev/null +++ b/js/service/model.js @@ -0,0 +1,77 @@ +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @copyright Bernhard Posselt 2014 + */ +app.factory('Model', function () { + 'use strict'; + + var Model = function (id) { + this.id = id; + this.values = []; + this.hashMap = {}; + }; + + Model.prototype = { + receive: function (values) { + var self = this; + values.forEach(function (value) { + self.add(value); + }); + }, + + add: function (value) { + var key, + existing; + + existing = this.hashMap[value[this.id]]; + + if (existing === undefined) { + this.values.push(value); + this.hashMap[value[this.id]] = value; + } else { + // copy values from new to old object if it exists already + for (key in value) { + if (value.hasOwnProperty(key)) { + existing[key] = value[key]; + } + } + } + }, + + size: function () { + return this.values.length; + }, + + get: function (id) { + return this.hashMap[id]; + }, + + delete: function (id) { + // find index of object that should be deleted + var i, + deleteAtIndex; + + for (i = 0; i < this.values.length; i += 1) { + if (this.values[i][this.id] === id) { + deleteAtIndex = i; + break; + } + } + + if (deleteAtIndex !== undefined) { + this.values.splice(deleteAtIndex, 1); + } + + if (this.hashMap[id] !== undefined) { + delete this.hashMap[id]; + } + } + }; + + return Model; +});
\ No newline at end of file diff --git a/js/service/publisher.js b/js/service/publisher.js new file mode 100644 index 000000000..185e60047 --- /dev/null +++ b/js/service/publisher.js @@ -0,0 +1,35 @@ +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @copyright Bernhard Posselt 2014 + */ +app.service('Publisher', function () { + 'use strict'; + + var self = this; + this.channels = {}; + + this.subscribe = function (object) { + return { + toChannel: function (channel) { + self.channels[channel] = self.channels[channel] || []; + self.channels[channel].push(object); + } + }; + }; + + this.publish = function (value) { + return { + onChannel: function (channel) { + self.channels[channel].forEach(function (object) { + object.receive(value); + }); + } + }; + }; + +});
\ No newline at end of file |