From cc0c808f219a23a3910f379c74f568295c60d12a Mon Sep 17 00:00:00 2001 From: leejaew Date: Mon, 7 Oct 2013 14:30:44 -0500 Subject: [PATCH 1/3] add default values to the model --- src/models/shirt.js | 6 +++++- src/views/shirt-view.js | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/models/shirt.js b/src/models/shirt.js index 624d37c..330be4d 100644 --- a/src/models/shirt.js +++ b/src/models/shirt.js @@ -1,7 +1,11 @@ (function () { window.Shirt = Backbone.Model.extend({ - // TODO: Add defaults property + defaults: { + small: 5, + medium: 5, + large: 5 + } }); })(); diff --git a/src/views/shirt-view.js b/src/views/shirt-view.js index da6f40f..98a9be2 100644 --- a/src/views/shirt-view.js +++ b/src/views/shirt-view.js @@ -7,7 +7,7 @@ className: 'shirt', render: function () { // TODO: Complete the following line - var newShirtHtml = shirtTemplate( ??? ); + var newShirtHtml = shirtTemplate( this.model.toJSON() ) ; $(this.el).html(newShirtHtml); } }); From 1a117d14db2798b54dc73ab40aefabf07b56fcd8 Mon Sep 17 00:00:00 2001 From: leejaew Date: Mon, 7 Oct 2013 15:59:52 -0500 Subject: [PATCH 2/3] add for loop that iterates through this.options.shirts array, render when model updates its data --- src/models/shirt.js | 6 +++--- src/views/inventory-view.js | 27 +++++++++++++++++++++++++-- src/views/shirt-view.js | 3 +++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/models/shirt.js b/src/models/shirt.js index 330be4d..28bc60c 100644 --- a/src/models/shirt.js +++ b/src/models/shirt.js @@ -2,9 +2,9 @@ window.Shirt = Backbone.Model.extend({ defaults: { - small: 5, - medium: 5, - large: 5 + small: 0, + medium: 0, + large: 0 } }); diff --git a/src/views/inventory-view.js b/src/views/inventory-view.js index 9d2fce9..3783c66 100644 --- a/src/views/inventory-view.js +++ b/src/views/inventory-view.js @@ -4,10 +4,33 @@ events: { 'click .add-shirts': 'addShirts' }, - addShirts: function (e) { var type = $(e.currentTarget).data('type'); - console.log('Adding one of every', type, 'shirt for', this.options.shirts); + //console.log('Adding one of every', type, 'shirt for', this.options.shirts); + + //console.log(this.options.shirts.length); + for (var s = 0; s < this.options.shirts.length; s++){ + if (type === 'small') { + //console.log(this.options.shirts[s].get('small')); + var newSmall = this.options.shirts[s].get('small'); + newSmall += 1; + //console.log(newSmall); + this.options.shirts[s].set({ small: newSmall }); + } else if (type === 'medium') { + //console.log(this.options.shirts[s].get('medium')); + var newMedium = this.options.shirts[s].get('medium'); + newMedium += 1; + //console.log(newMedium); + this.options.shirts[s].set({ medium: newMedium }); + } else if (type === 'large') { + //console.log(this.options.shirts[s].get('large')); + var newLarge = this.options.shirts[s].get('large'); + newLarge += 1; + //console.log(newLarge); + this.options.shirts[s].set({ large: newLarge }); + } + + } // TODO: Write a for loop to iterate through this.options.shirts and // add +1 to each of that shirt's `type` inventory. // For example, if type === 'small', then add +1 to every small stock diff --git a/src/views/shirt-view.js b/src/views/shirt-view.js index 98a9be2..b6c83a6 100644 --- a/src/views/shirt-view.js +++ b/src/views/shirt-view.js @@ -4,6 +4,9 @@ var shirtTemplate = _.template(shirtTemplateHtml); window.ShirtView = Backbone.View.extend({ + initialize: function(){ + this.listenTo(this.model, 'change', this.render); + }, className: 'shirt', render: function () { // TODO: Complete the following line From 5785ea3e5ce3ac9d4e84366695b0022f5dce7d40 Mon Sep 17 00:00:00 2001 From: leejaew Date: Mon, 7 Oct 2013 16:12:46 -0500 Subject: [PATCH 3/3] simplify code --- src/views/inventory-view.js | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/views/inventory-view.js b/src/views/inventory-view.js index 3783c66..364a859 100644 --- a/src/views/inventory-view.js +++ b/src/views/inventory-view.js @@ -6,30 +6,29 @@ }, addShirts: function (e) { var type = $(e.currentTarget).data('type'); - //console.log('Adding one of every', type, 'shirt for', this.options.shirts); + + var myShirts = this.options.shirts; - //console.log(this.options.shirts.length); - for (var s = 0; s < this.options.shirts.length; s++){ + for (var s = 0; s < myShirts.length; s++){ + + var eachShirts = myShirts[s] + if (type === 'small') { - //console.log(this.options.shirts[s].get('small')); - var newSmall = this.options.shirts[s].get('small'); + var newSmall = myShirts[s].get('small'); newSmall += 1; - //console.log(newSmall); - this.options.shirts[s].set({ small: newSmall }); - } else if (type === 'medium') { - //console.log(this.options.shirts[s].get('medium')); - var newMedium = this.options.shirts[s].get('medium'); + //myShirts[s].set({ small: newSmall }); + eachShirts.set(type, newSmall) + } else if (type === 'medium') { + var newMedium = myShirts[s].get('medium'); newMedium += 1; - //console.log(newMedium); - this.options.shirts[s].set({ medium: newMedium }); + //myShirts[s].set({ medium: newMedium }); + eachShirts.set(type, newMedium) } else if (type === 'large') { - //console.log(this.options.shirts[s].get('large')); - var newLarge = this.options.shirts[s].get('large'); + var newLarge = myShirts[s].get('large'); newLarge += 1; - //console.log(newLarge); - this.options.shirts[s].set({ large: newLarge }); + //myShirts[s].set({ large: newLarge }); + eachShirts.set(type, newLarge) } - } // TODO: Write a for loop to iterate through this.options.shirts and // add +1 to each of that shirt's `type` inventory.