summaryrefslogtreecommitdiffstats
path: root/js
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-04-11 11:33:05 +0200
committerBernhard Posselt <nukeawhale@gmail.com>2013-04-11 11:33:05 +0200
commitb54b454f8a36e50e46a71a8cc14c5caa59a778e4 (patch)
treebf51c7f3c4104c89049a9b7165e09f2d4bbdad8d /js
parent50902e1c0550de481fe655c5d560d57da0fe3e12 (diff)
fixed feedmodel
Diffstat (limited to 'js')
-rw-r--r--js/app/services/models/feedmodel.coffee82
-rw-r--r--js/public/app.js58
-rw-r--r--js/tests/services/models/feedmodelSpec.coffee20
-rw-r--r--js/tests/services/models/foldermodelSpec.coffee4
4 files changed, 109 insertions, 55 deletions
diff --git a/js/app/services/models/feedmodel.coffee b/js/app/services/models/feedmodel.coffee
index bd10ccb8b..b7e0a4361 100644
--- a/js/app/services/models/feedmodel.coffee
+++ b/js/app/services/models/feedmodel.coffee
@@ -37,34 +37,68 @@ angular.module('News').factory '_FeedModel',
super()
- add: (item, clearCache=true) ->
- if item.faviconLink == null
- item.faviconLink = 'url(' +
+ add: (data, clearCache=true) ->
+ if data.faviconLink == null
+ data.faviconLink = 'url(' +
@_utils.imagePath('news', 'rss.svg') + ')'
-
- # since the feedurl is also unique, we want to update items that have
- # the same url
- entry = @_urlHash[item.urlHash]
- if angular.isDefined(entry)
- @update(item, clearCache)
+ ###
+ We want to add a feed on the client side before
+ we have an id from the server. Once the server returns
+ an id, we have to update the existing item without id
+ ###
+
+ item = @_urlHash[data.urlHash]
+
+ # update in the following cases:
+ # * the id is defined and the item exists
+ # * the id is not defined and the name exists in the cache
+ updateById = angular.isDefined(data.id) and
+ angular.isDefined(@getById(data.id))
+
+ updateByUrlHash = angular.isDefined(item) and
+ angular.isUndefined(item.id)
+
+ if updateById or updateByUrlHash
+ @update(data)
else
- @_urlHash[item.urlHash] = item
- super(item, clearCache)
-
-
- update: (item, clearCache=true) ->
- entry = @_urlHash[item.urlHash]
+ # if the item is not yet in the name cache it must be added
+ @_urlHash[data.urlHash] = data
+
+ # in case there is an id it can go through the normal add method
+ if angular.isDefined(data.id)
+ super(data, clearCache)
+
+ # if there is no id we just want it to appear in the list
+ else
+ @_data.push(data)
- # first update id that could have changed
- delete @_dataMap[entry.id]
- @_dataMap[item.id] = entry
- # now copy over the elements data attrs
- for key, value of item
- if key == 'urlHash'
- continue
- else
- entry[key] = value
+ update: (data, clearCache=true) ->
+ # only when the id on the updated item does not exist we wish
+ # to update by name, otherwise we always update by id
+ item = @_urlHash[data.urlHash]
+ # update by name
+ if angular.isUndefined(data.id) and angular.isDefined(item)
+ angular.extend(item, data)
+
+ else
+ # this case happens when there exists an element with the same
+ # name but it has no id yet
+ if angular.isDefined(data.id) and
+ angular.isDefined(item) and
+ angular.isUndefined(item.id)
+ item.id = data.id
+ @_dataMap[data.id] = item
+
+ # if an update comes in and we update because of the id
+ # we need to fix the name cache if the name was changed
+ itemWithId = @getById(data.id)
+ if angular.isDefined(itemWithId) and
+ itemWithId.urlHash != data.urlHash
+ delete @_urlHash[itemWithId.urlHash]
+ @_urlHash[data.urlHash] = itemWithId
+
+ super(data, clearCache)
removeById: (id) ->
diff --git a/js/public/app.js b/js/public/app.js
index edae69266..f5fdf95ae 100644
--- a/js/public/app.js
+++ b/js/public/app.js
@@ -1214,43 +1214,57 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
return FeedModel.__super__.clear.call(this);
};
- FeedModel.prototype.add = function(item, clearCache) {
- var entry;
+ FeedModel.prototype.add = function(data, clearCache) {
+ var item, updateById, updateByUrlHash;
if (clearCache == null) {
clearCache = true;
}
- if (item.faviconLink === null) {
- item.faviconLink = 'url(' + this._utils.imagePath('news', 'rss.svg') + ')';
+ if (data.faviconLink === null) {
+ data.faviconLink = 'url(' + this._utils.imagePath('news', 'rss.svg') + ')';
}
- entry = this._urlHash[item.urlHash];
- if (angular.isDefined(entry)) {
- return this.update(item, clearCache);
+ /*
+ We want to add a feed on the client side before
+ we have an id from the server. Once the server returns
+ an id, we have to update the existing item without id
+ */
+
+ item = this._urlHash[data.urlHash];
+ updateById = angular.isDefined(data.id) && angular.isDefined(this.getById(data.id));
+ updateByUrlHash = angular.isDefined(item) && angular.isUndefined(item.id);
+ if (updateById || updateByUrlHash) {
+ return this.update(data);
} else {
- this._urlHash[item.urlHash] = item;
- return FeedModel.__super__.add.call(this, item, clearCache);
+ this._urlHash[data.urlHash] = data;
+ if (angular.isDefined(data.id)) {
+ return FeedModel.__super__.add.call(this, data, clearCache);
+ } else {
+ return this._data.push(data);
+ }
}
};
- FeedModel.prototype.update = function(item, clearCache) {
- var entry, key, value, _results;
+ FeedModel.prototype.update = function(data, clearCache) {
+ var item, itemWithId;
if (clearCache == null) {
clearCache = true;
}
- entry = this._urlHash[item.urlHash];
- delete this._dataMap[entry.id];
- this._dataMap[item.id] = entry;
- _results = [];
- for (key in item) {
- value = item[key];
- if (key === 'urlHash') {
- continue;
- } else {
- _results.push(entry[key] = value);
+ item = this._urlHash[data.urlHash];
+ if (angular.isUndefined(data.id) && angular.isDefined(item)) {
+ return angular.extend(item, data);
+ } else {
+ if (angular.isDefined(data.id) && angular.isDefined(item) && angular.isUndefined(item.id)) {
+ item.id = data.id;
+ this._dataMap[data.id] = item;
}
+ itemWithId = this.getById(data.id);
+ if (angular.isDefined(itemWithId) && itemWithId.urlHash !== data.urlHash) {
+ delete this._urlHash[itemWithId.urlHash];
+ this._urlHash[data.urlHash] = itemWithId;
+ }
+ return FeedModel.__super__.update.call(this, data, clearCache);
}
- return _results;
};
FeedModel.prototype.removeById = function(id) {
diff --git a/js/tests/services/models/feedmodelSpec.coffee b/js/tests/services/models/feedmodelSpec.coffee
index 8c2043a39..1ac76a65e 100644
--- a/js/tests/services/models/feedmodelSpec.coffee
+++ b/js/tests/services/models/feedmodelSpec.coffee
@@ -52,8 +52,11 @@ describe 'FeedModel', ->
item = {faviconLink: null, urlHash: 'hi'}
@FeedModel.add(item)
+ item2 = {faviconLink: null, urlHash: 'his'}
+ @FeedModel.add(item2)
+
expect(@FeedModel.getByUrlHash('hi')).toBe(item)
- expect(@FeedModel.size()).toBe(1)
+ expect(@FeedModel.size()).toBe(2)
it 'should clear the url hash cache', =>
@@ -78,16 +81,14 @@ describe 'FeedModel', ->
item = {faviconLink: null, urlHash: 'hi', test: 'heheh'}
@FeedModel.add(item)
- item = {id: 3, faviconLink: null, urlHash: 'hi', test: 'hoho'}
- @FeedModel.add(item)
-
- item = {id: 4, faviconLink: null, urlHash: 'his'}
- @FeedModel.add(item)
+ item2 = {id: 3, faviconLink: null, urlHash: 'hi', test: 'hoho'}
+ @FeedModel.add(item2)
expect(@FeedModel.getByUrlHash('hi').id).toBe(3)
+ expect(@FeedModel.getByUrlHash('hi').test).toBe('hoho')
expect(@FeedModel.getById(3).id).toBe(3)
expect(@FeedModel.getById(3).test).toBe('hoho')
- expect(@FeedModel.size()).toBe(2)
+ expect(@FeedModel.size()).toBe(1)
it 'should update normally', =>
@@ -97,8 +98,11 @@ describe 'FeedModel', ->
item2 = {id: 3, faviconLink: null, urlHash: 'his', test: 'hoho'}
@FeedModel.add(item2)
- expect(@FeedModel.getById(3).id).toBe(3)
+ expect(@FeedModel.getByUrlHash('hi')).toBe(undefined)
+ expect(@FeedModel.getByUrlHash('his').id).toBe(3)
+ expect(@FeedModel.getByUrlHash('his').test).toBe('hoho')
expect(@FeedModel.getById(3).test).toBe('hoho')
expect(@FeedModel.size()).toBe(1)
+
diff --git a/js/tests/services/models/foldermodelSpec.coffee b/js/tests/services/models/foldermodelSpec.coffee
index 8c2dd4b71..d34b2e309 100644
--- a/js/tests/services/models/foldermodelSpec.coffee
+++ b/js/tests/services/models/foldermodelSpec.coffee
@@ -35,8 +35,10 @@ describe 'FolderModel', ->
it 'should add folders without id but name if they dont exist yet', =>
item = {name: 'Hi'}
@FolderModel.add(item)
+ item1 = {name: 'His'}
+ @FolderModel.add(item1)
expect(@FolderModel.getByName('hi')).toBe(item)
- expect(@FolderModel.size()).toBe(1)
+ expect(@FolderModel.size()).toBe(2)
it 'should clear the fodername cache', =>