From 2459002dcc070dcb66b2a44c237b87e8a1b8e968 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Sun, 18 May 2014 22:49:30 +0200 Subject: add models --- js/build/app.js | 50 ++++++++++++++++++++++++++++++++++ js/service/feed.js | 20 ++++++++++++++ js/service/folder.js | 20 ++++++++++++++ js/service/item.js | 20 ++++++++++++++ js/service/model.js | 11 ++++++++ js/service/publisher.js | 9 ++++++ js/tests/unit/service/ModelSpec.js | 26 ++++++++++++++++++ js/tests/unit/service/PublisherSpec.js | 15 ++++++++++ 8 files changed, 171 insertions(+) create mode 100644 js/service/feed.js create mode 100644 js/service/folder.js create mode 100644 js/service/item.js diff --git a/js/build/app.js b/js/build/app.js index 206114cd4..8c5835c06 100644 --- a/js/build/app.js +++ b/js/build/app.js @@ -54,6 +54,39 @@ app.run([ }); } ]); +app.factory('Feed', [ + 'Model', + function (Model) { + 'use strict'; + var Feed = function () { + Model.call(this, 'url'); + }; + Feed.prototype = Object.create(Model.prototype); + return new Feed(); + } +]); +app.factory('Folder', [ + 'Model', + function (Model) { + 'use strict'; + var Folder = function () { + Model.call(this, 'name'); + }; + Folder.prototype = Object.create(Model.prototype); + return new Folder(); + } +]); +app.factory('Item', [ + 'Model', + function (Model) { + 'use strict'; + var Item = function () { + Model.call(this, 'id'); + }; + Item.prototype = Object.create(Model.prototype); + return new Item(); + } +]); app.service('Loading', function () { 'use strict'; this.loading = { @@ -117,6 +150,15 @@ app.factory('Model', function () { if (this.hashMap[id] !== undefined) { delete this.hashMap[id]; } + }, + clear: function () { + this.hashMap = {}; + // http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript + // this is the fastes way to empty an array when you want to keep the + // reference around + while (this.values.length > 0) { + this.values.pop(); + } } }; return Model; @@ -133,6 +175,14 @@ app.service('Publisher', function () { } }; }; + this.publishAll = function (values) { + var key; + for (key in values) { + if (values.hasOwnProperty(key)) { + this.publish(values[key]).onChannel(key); + } + } + }; this.publish = function (value) { return { onChannel: function (channel) { diff --git a/js/service/feed.js b/js/service/feed.js new file mode 100644 index 000000000..9223ca6e7 --- /dev/null +++ b/js/service/feed.js @@ -0,0 +1,20 @@ +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bernhard Posselt + * @copyright Bernhard Posselt 2014 + */ +app.factory('Feed', function (Model) { + 'use strict'; + + var Feed = function () { + Model.call(this, 'url'); + }; + + Feed.prototype = Object.create(Model.prototype); + + return new Feed(); +}); \ No newline at end of file diff --git a/js/service/folder.js b/js/service/folder.js new file mode 100644 index 000000000..101b8ec66 --- /dev/null +++ b/js/service/folder.js @@ -0,0 +1,20 @@ +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bernhard Posselt + * @copyright Bernhard Posselt 2014 + */ +app.factory('Folder', function (Model) { + 'use strict'; + + var Folder = function () { + Model.call(this, 'name'); + }; + + Folder.prototype = Object.create(Model.prototype); + + return new Folder(); +}); \ No newline at end of file diff --git a/js/service/item.js b/js/service/item.js new file mode 100644 index 000000000..0dd9b8677 --- /dev/null +++ b/js/service/item.js @@ -0,0 +1,20 @@ +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bernhard Posselt + * @copyright Bernhard Posselt 2014 + */ +app.factory('Item', function (Model) { + 'use strict'; + + var Item = function () { + Model.call(this, 'id'); + }; + + Item.prototype = Object.create(Model.prototype); + + return new Item(); +}); \ No newline at end of file diff --git a/js/service/model.js b/js/service/model.js index a28608329..dddede788 100644 --- a/js/service/model.js +++ b/js/service/model.js @@ -70,6 +70,17 @@ app.factory('Model', function () { if (this.hashMap[id] !== undefined) { delete this.hashMap[id]; } + }, + + clear: function () { + this.hashMap = {}; + + // http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript + // this is the fastes way to empty an array when you want to keep the + // reference around + while (this.values.length > 0) { + this.values.pop(); + } } }; diff --git a/js/service/publisher.js b/js/service/publisher.js index 185e60047..d7e9bb0e7 100644 --- a/js/service/publisher.js +++ b/js/service/publisher.js @@ -22,6 +22,15 @@ app.service('Publisher', function () { }; }; + this.publishAll = function (values) { + var key; + for (key in values) { + if (values.hasOwnProperty(key)) { + this.publish(values[key]).onChannel(key); + } + } + }; + this.publish = function (value) { return { onChannel: function (channel) { diff --git a/js/tests/unit/service/ModelSpec.js b/js/tests/unit/service/ModelSpec.js index d8aba1520..57b0f422c 100644 --- a/js/tests/unit/service/ModelSpec.js +++ b/js/tests/unit/service/ModelSpec.js @@ -100,4 +100,30 @@ describe('Model', function () { expect(childModel.size()).toBe(1); }); + + it('should clear all models', function () { + var object1, + object2; + + object1 = { + id: 3, + name: 'test', + test: 'ho' + }; + + object2 = { + id: 4, + name: 'test2' + }; + + childModel.add(object1); + childModel.add(object2); + + childModel.clear(); + + expect(childModel.get(3)).not.toBeDefined(); + expect(childModel.get(4)).not.toBeDefined(); + expect(childModel.size()).toBe(0); + }); + }); \ No newline at end of file diff --git a/js/tests/unit/service/PublisherSpec.js b/js/tests/unit/service/PublisherSpec.js index 8a019f0db..59628356f 100644 --- a/js/tests/unit/service/PublisherSpec.js +++ b/js/tests/unit/service/PublisherSpec.js @@ -23,4 +23,19 @@ describe('Publisher', function () { })); + it('should should publish on all possible channels', inject(function (Publisher) { + + var obj = { + receive: jasmine.createSpy('receive') + }; + Publisher.subscribe(obj).toChannel('test'); + + Publisher.publishAll({ + test: 'tom' + }); + + expect(obj.receive).toHaveBeenCalledWith('tom'); + + })); + }); \ No newline at end of file -- cgit v1.2.3