From 6bd5a67fc0d4fb89decad7e26be3b7652ed63300 Mon Sep 17 00:00:00 2001 From: tomgreenfield Date: Thu, 6 Jul 2017 11:14:26 +0100 Subject: [PATCH 1/4] Add items collection --- src/core/js/models/itemModel.js | 31 ++++++ src/core/js/models/itemsComponentModel.js | 67 ++++++++++++ src/core/js/models/itemsModel.js | 126 ---------------------- 3 files changed, 98 insertions(+), 126 deletions(-) create mode 100644 src/core/js/models/itemModel.js create mode 100644 src/core/js/models/itemsComponentModel.js delete mode 100644 src/core/js/models/itemsModel.js diff --git a/src/core/js/models/itemModel.js b/src/core/js/models/itemModel.js new file mode 100644 index 000000000..2fb899ea7 --- /dev/null +++ b/src/core/js/models/itemModel.js @@ -0,0 +1,31 @@ +define(function() { + + var ItemModel = Backbone.Model.extend({ + + defaults: { _isActive: false, _isVisited: false }, + + reset: function() { + this.set({ _isActive: false, _isVisited: false }); + }, + + toggleActive: function(isActive) { + if (isActive === undefined) { + isActive = !this.get('_isActive'); + } + + this.set('_isActive', isActive); + }, + + toggleVisited: function(isVisited) { + if (isVisited === undefined) { + isVisited = !this.get('_isVisited'); + } + + this.set('_isVisited', isVisited); + } + + }); + + return ItemModel; + +}); diff --git a/src/core/js/models/itemsComponentModel.js b/src/core/js/models/itemsComponentModel.js new file mode 100644 index 000000000..6259b8ed0 --- /dev/null +++ b/src/core/js/models/itemsComponentModel.js @@ -0,0 +1,67 @@ +define([ + 'core/js/models/componentModel', + 'core/js/models/itemModel' +], function(ComponentModel, ItemModel) { + + var ItemsComponentModel = ComponentModel.extend({ + + init: function() { + this.setUpItems(); + this.listenTo(this.get('_items'), { + 'change:_isVisited': this.checkCompletionStatus + }); + }, + + setUpItems: function() { + var items = this.get('_items'); + var collection = new Backbone.Collection(null, { model: ItemModel }); + + items.forEach(function(item, index) { + item._index = index; + collection.add(item); + }); + + this.set('_items', collection); + }, + + getItem: function(index) { + return this.get('_items').findWhere({ _index: index }); + }, + + getVisitedItems: function() { + return this.get('_items').where({ _isVisited: true }); + }, + + getActiveItems: function() { + return this.get('_items').where({ _isActive: true }); + }, + + getActiveItem: function() { + return this.get('_items').findWhere({ _isActive: true }); + }, + + areAllItemsCompleted: function() { + return this.getVisitedItems().length === this.get('_items').length; + }, + + checkCompletionStatus: function() { + if (this.areAllItemsCompleted()) { + this.setCompletionStatus(); + } + }, + + reset: function(type, force) { + this.get('_items').each(function(item) { item.reset(); }); + + ComponentModel.prototype.reset.call(this, type, force); + }, + + resetActiveItems: function() { + this.get('_items').each(function(item) { item.toggleActive(false); }); + } + + }); + + return ItemsComponentModel; + +}); diff --git a/src/core/js/models/itemsModel.js b/src/core/js/models/itemsModel.js deleted file mode 100644 index fb0c64b74..000000000 --- a/src/core/js/models/itemsModel.js +++ /dev/null @@ -1,126 +0,0 @@ -define([ - 'core/js/adapt', - 'core/js/models/componentModel' -], function(Adapt, ComponentModel) { - - var ItemsModel = ComponentModel.extend({ - - reset: function(type, force) { - this.resetItems(); - ComponentModel.prototype.reset.call(this, type, force); - }, - - resetItems: function() { - _.each(this.get('_items'), function(item) { - item._isVisited = false; - item._isActive = false; - }); - }, - - getItemCount: function() { - return this.get('_items').length; - }, - - getItem: function(index) { - return this.get('_items')[index]; - }, - - getVisitedItems: function() { - return _.filter(this.get('_items'), function(item) { - return item._isVisited; - }); - }, - - setItemVisited: function(index) { - var item = this.get('_items')[index]; - if (item) { - item._isVisited = true; - } - }, - - areAllIItemsCompleted: function() { - return (this.getVisitedItems().length === this.getItemCount()); - }, - - checkCompletionStatus: function() { - if (this.areAllIItemsCompleted()) { - this.setCompletionStatus(); - } - }, - - resetActiveItems: function(trigger) { - var items = this.get('_items'); - - _.each(items, function(item) { - item._isActive = false; - }); - - if (trigger !== false) { - this.trigger('change:_items:_isActive', this, items); - } - }, - - getActiveItems: function() { - return _.filter(this.get('_items'), function(item) { - return item._isActive; - }); - }, - - getFirstActiveItem: function() { - return this.getActiveItems()[0]; - }, - - getActiveItemsCount: function() { - return this.getActiveItems().length; - }, - - getActiveItemsIndexes: function() { - var indexes = []; - var items = this.get('_items'); - - for (var i = 0, l = items.length; i < l; i++) { - if (items[i]._isActive) { - indexes.push(i); - } - } - - return indexes; - }, - - getFirstActiveItemIndex: function() { - return this.getActiveItemsIndexes()[0]; - }, - - setItemActive: function(index, trigger) { - var items = this.get('_items'); - var item = items[index]; - - if (item === undefined) return false; - - item._isActive = true; - if (trigger !== false) { - this.trigger('change:_items:_isActive', this, items); - } - - return item; - }, - - setItemInactive: function(index, trigger) { - var items = this.get('_items'); - var item = items[index]; - - if (item === undefined) return false; - - item._isActive = false; - if (trigger !== false) { - this.trigger('change:_items:_isActive', this, items); - } - - return item; - } - - }); - - return ItemsModel; - -}); From 8e5b7e4efa5ba0d2842f62477736d8df87f82ddd Mon Sep 17 00:00:00 2001 From: tomgreenfield Date: Wed, 9 Aug 2017 16:16:47 +0100 Subject: [PATCH 2/4] Add toJSON override --- src/core/js/models/itemsComponentModel.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/core/js/models/itemsComponentModel.js b/src/core/js/models/itemsComponentModel.js index 6259b8ed0..2cd10b483 100644 --- a/src/core/js/models/itemsComponentModel.js +++ b/src/core/js/models/itemsComponentModel.js @@ -5,8 +5,17 @@ define([ var ItemsComponentModel = ComponentModel.extend({ + toJSON: function() { + var json = _.clone(this.attributes); + + json._items = this.get('_items').toJSON(); + + return json; + }, + init: function() { this.setUpItems(); + this.listenTo(this.get('_items'), { 'change:_isVisited': this.checkCompletionStatus }); From 3d09c0b616c1009466ffba0647a51b4d9c0e7730 Mon Sep 17 00:00:00 2001 From: Tom Greenfield Date: Thu, 24 Aug 2017 10:26:32 +0100 Subject: [PATCH 3/4] Tweak whitespace --- src/core/js/models/itemModel.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/js/models/itemModel.js b/src/core/js/models/itemModel.js index 2fb899ea7..76a539b06 100644 --- a/src/core/js/models/itemModel.js +++ b/src/core/js/models/itemModel.js @@ -2,7 +2,10 @@ define(function() { var ItemModel = Backbone.Model.extend({ - defaults: { _isActive: false, _isVisited: false }, + defaults: { + _isActive: false, + _isVisited: false + }, reset: function() { this.set({ _isActive: false, _isVisited: false }); From d0e9af168a4e1b93597f99a68db819fd10d9abf7 Mon Sep 17 00:00:00 2001 From: Tom Greenfield Date: Thu, 24 Aug 2017 10:27:02 +0100 Subject: [PATCH 4/4] Switch to .map() --- src/core/js/models/itemsComponentModel.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/core/js/models/itemsComponentModel.js b/src/core/js/models/itemsComponentModel.js index 2cd10b483..36753de7c 100644 --- a/src/core/js/models/itemsComponentModel.js +++ b/src/core/js/models/itemsComponentModel.js @@ -22,15 +22,13 @@ define([ }, setUpItems: function() { - var items = this.get('_items'); - var collection = new Backbone.Collection(null, { model: ItemModel }); - - items.forEach(function(item, index) { + var items = this.get('_items').map(function(item, index) { item._index = index; - collection.add(item); + + return item; }); - this.set('_items', collection); + this.set('_items', new Backbone.Collection(items, { model: ItemModel })); }, getItem: function(index) {