From 2b6b681eb59c16014f1a05b141b344fcf9a9aba1 Mon Sep 17 00:00:00 2001 From: Jesse Kevon Date: Sat, 5 Oct 2013 15:18:05 -0500 Subject: [PATCH 1/5] add parseFloat function to convert newPrice to decimal number --- src/models/stock.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/models/stock.js b/src/models/stock.js index d05bcda..331830a 100644 --- a/src/models/stock.js +++ b/src/models/stock.js @@ -1,10 +1,16 @@ -(function () { +// The parseFloat() function converts the incoming newPrice from a string to floating integer. +// +// +// Sets the new price value on the model + +(function(){ window.Stock = Backbone.Model.extend({ updatePrice: function (newPrice) { - console.log('Updating', this.get('name'), 'price to:', newPrice); - // TODO + this.price = parseFloat(newPrice); + this.set({ price: this.price }); + console.log('Updating', this.get('name'), 'price to:', this.get('price')); } }); -})(); + })(); From 0662b71395cb2d658b927859fee5f901734cd668 Mon Sep 17 00:00:00 2001 From: Jesse Kevon Date: Sun, 6 Oct 2013 13:32:23 -0500 Subject: [PATCH 2/5] listen for StockView model change in initialize --- src/views/stock-view.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/views/stock-view.js b/src/views/stock-view.js index faa5795..594a0e9 100644 --- a/src/views/stock-view.js +++ b/src/views/stock-view.js @@ -1,8 +1,11 @@ + // Listen for StockView's model's changes in initialize. + (function () { window.StockView = Backbone.View.extend({ - className: 'stock' - // TODO - }); - -})(); + className: 'stock', + initialize: function(){ + this.listenTo(this.model, 'change:price', this.render); + }, + }); +}); \ No newline at end of file From 8b523636d50063410f197bc56f27ea5f7e07cace Mon Sep 17 00:00:00 2001 From: Jesse Kevon Date: Sun, 6 Oct 2013 13:35:29 -0500 Subject: [PATCH 3/5] render function updates el using underscore and render stock name and price. --- src/views/stock-view.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/views/stock-view.js b/src/views/stock-view.js index 594a0e9..cc44499 100644 --- a/src/views/stock-view.js +++ b/src/views/stock-view.js @@ -1,4 +1,8 @@ // Listen for StockView's model's changes in initialize. + // + // Render function updates the 'el'ement using an underscore template. + // + // Render the stock name and price. (function () { @@ -7,5 +11,12 @@ initialize: function(){ this.listenTo(this.model, 'change:price', this.render); }, + + render: function(){ + var priceTemplateHtml = $('#templates .stock').html(); + var priceTemplate = _.template(priceTemplateHtml); + var newTemplateHtml = priceTemplate({ name: this.model.get('name'), price: this.model.get('price') }); + $(this.el).html(newTemplateHtml); + } }); -}); \ No newline at end of file +}); From 9bebcedbdf9cebeb8b8c8c3bef0e6a59b6c6e0e2 Mon Sep 17 00:00:00 2001 From: Jesse Kevon Date: Sun, 6 Oct 2013 13:38:33 -0500 Subject: [PATCH 4/5] add html template --- index.html | 11 ++++++++++- src/main.js | 1 - 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 9adc1b3..b9bdf8f 100644 --- a/index.html +++ b/index.html @@ -6,12 +6,21 @@ +
+ + + +
+ +
- diff --git a/src/main.js b/src/main.js index 3872b23..619079f 100644 --- a/src/main.js +++ b/src/main.js @@ -18,7 +18,6 @@ // ---- // Get realtime stock data - // It's not required that you understand the code. window.updateStocks = function (data) { From dec02018af7b0a9e3420f3a14e75de8b84579036 Mon Sep 17 00:00:00 2001 From: Jesse Kevon Date: Sun, 6 Oct 2013 20:23:37 -0500 Subject: [PATCH 5/5] Fix syntax error preventing window.StockView from running. --- src/models/stock.js | 2 +- src/views/stock-view.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/models/stock.js b/src/models/stock.js index 331830a..71b6776 100644 --- a/src/models/stock.js +++ b/src/models/stock.js @@ -13,4 +13,4 @@ } }); - })(); +})(); diff --git a/src/views/stock-view.js b/src/views/stock-view.js index cc44499..2b0c32d 100644 --- a/src/views/stock-view.js +++ b/src/views/stock-view.js @@ -19,4 +19,4 @@ $(this.el).html(newTemplateHtml); } }); -}); +})();