From 62f94654160a6509302b2043781f36d219d232c5 Mon Sep 17 00:00:00 2001 From: Stef Date: Mon, 11 Dec 2017 16:37:25 -0800 Subject: [PATCH 01/20] View files added. --- src/app.js | 2 ++ src/views/quote_list_view.js | 34 ++++++++++++++++++++++++++++++++++ src/views/quote_view.js | 25 +++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 src/views/quote_list_view.js create mode 100644 src/views/quote_view.js diff --git a/src/app.js b/src/app.js index 03ec910..0e09e5e 100644 --- a/src/app.js +++ b/src/app.js @@ -6,6 +6,8 @@ import $ from 'jquery'; import Simulator from 'models/simulator'; import QuoteList from 'collections/quote_list'; +import QuoteListView from './views/quote_list_view'; + const quoteData = [ { symbol: 'HUMOR', diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js new file mode 100644 index 0000000..3e5fe18 --- /dev/null +++ b/src/views/quote_list_view.js @@ -0,0 +1,34 @@ +// /src/views/task_list_view.js +import Backbone from 'backbone'; +import _ from 'underscore'; +import QuoteView from '../views/quote_view'; +import Quote from '../models/quote'; + +const QuoteListView = Backbone.View.extend({ + initialize(params) { + this.template = params.template; + this.listenTo(this.model, 'update', this.render); + }, + + render() { + // Clear the unordered list + //this.$ -> allows to look up jquery things within this view + this.$('#quotes').empty(); + // Iterate through the list rendering each Task + this.model.each((quote) => { + // Create a new TaskView with the model & template + const quoteView = new QuoteView({ + model: quote, + template: this.template, + tagName: 'li', + className: 'quote', + }); + // Then render the TaskView + // And append the resulting HTML to the DOM. + this.$('#quotes').append(quoteView.render().$el); + }); + return this; + }, +}); + +export default QuoteListView; diff --git a/src/views/quote_view.js b/src/views/quote_view.js new file mode 100644 index 0000000..12e2e13 --- /dev/null +++ b/src/views/quote_view.js @@ -0,0 +1,25 @@ +import Backbone from 'backbone'; +import Quote from '../models/quote'; + +const QuoteView = Backbone.View.extend({ + // params is an object. it contains a template. We're going to assign that tempalte to the new view we are creating. + initialize(params) { + this.template = params.template; + // good practice to have this in every view + this.listenTo(this.model, "change", this.render); + }, + //similar to how we used our underscore templates. It is going to take the template we created on initialize and a model and make a compiled template. + render() { + const compiledTemplate = this.template(this.model.toJSON()); + this.$el.html(compiledTemplate); + if (this.model.get('is_complete')) { + this.$el.addClass('is-complete'); + } else { + this.$el.removeClass('is-complete'); + } + return this; + }, + +}); + +export default QuoteView; From 6701682bb4c0cbc1d0d793fd999bbbdcb85ea53c Mon Sep 17 00:00:00 2001 From: Stef Date: Mon, 11 Dec 2017 17:04:51 -0800 Subject: [PATCH 02/20] Can view quotes, with symbols and prices and all that good stuff that involved the first set of user stories. --- src/app.js | 10 ++++++++++ src/models/quote.js | 4 ++-- src/views/quote_view.js | 19 +++++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/app.js b/src/app.js index 0e09e5e..0b8ed3d 100644 --- a/src/app.js +++ b/src/app.js @@ -2,6 +2,7 @@ import 'foundation-sites/dist/foundation.css'; import 'css/app.css'; import $ from 'jquery'; +import _ from 'underscore'; import Simulator from 'models/simulator'; import QuoteList from 'collections/quote_list'; @@ -33,5 +34,14 @@ $(document).ready(function() { quotes: quotes, }); + const quoteListView = new QuoteListView({ + model: quotes, + template: _.template($('#quote-template').html()), + el: 'main' + + }); + simulator.start(); + quoteListView.render(); + }); diff --git a/src/models/quote.js b/src/models/quote.js index 4fbf466..618dce1 100644 --- a/src/models/quote.js +++ b/src/models/quote.js @@ -7,11 +7,11 @@ const Quote = Backbone.Model.extend({ }, buy() { - // Implement this function to increase the price by $1.00 + this.set('price', this.get('price')+ 1); }, sell() { - // Implement this function to decrease the price by $1.00 + this.set('price', this.get('price')- 1); }, }); diff --git a/src/views/quote_view.js b/src/views/quote_view.js index 12e2e13..20e5698 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -2,13 +2,13 @@ import Backbone from 'backbone'; import Quote from '../models/quote'; const QuoteView = Backbone.View.extend({ - // params is an object. it contains a template. We're going to assign that tempalte to the new view we are creating. + // Params is an object. It contains a template. We're going to assign that template to the new view we are creating. initialize(params) { this.template = params.template; // good practice to have this in every view this.listenTo(this.model, "change", this.render); }, - //similar to how we used our underscore templates. It is going to take the template we created on initialize and a model and make a compiled template. + // Similar to how we used our underscore templates. It is going to take the template we created on initialize and a model and make a compiled template. render() { const compiledTemplate = this.template(this.model.toJSON()); this.$el.html(compiledTemplate); @@ -20,6 +20,21 @@ const QuoteView = Backbone.View.extend({ return this; }, + events: { + // Event => click, select => button.delete (a button with class delete), function to call => deleteTask + 'click button.btn-buy': 'btn-buy', + 'click button.btn-sell': 'btn-sell', + + }, + // All callbakcs for events include an event. You don't have to use it, but it is there if you need it. + buyQuote: function(e) { + this.model.buy(); + }, + + sellQuote: function(e) { + this.model.sell(); + }, + }); export default QuoteView; From 3c91903eeddf9436ed1b6910ce917a3c84406ad6 Mon Sep 17 00:00:00 2001 From: Stef Date: Fri, 15 Dec 2017 11:23:55 -0800 Subject: [PATCH 03/20] Fixed bug with adding and subtracting with buy and sell. --- src/app.js | 5 +++-- src/models/quote.js | 4 ++-- src/views/quote_list_view.js | 7 ------- src/views/quote_view.js | 21 +++++++-------------- 4 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/app.js b/src/app.js index 0b8ed3d..69cc769 100644 --- a/src/app.js +++ b/src/app.js @@ -4,9 +4,10 @@ import 'css/app.css'; import $ from 'jquery'; import _ from 'underscore'; +import Backbone from 'backbone'; import Simulator from 'models/simulator'; -import QuoteList from 'collections/quote_list'; +import QuoteList from 'collections/quote_list'; import QuoteListView from './views/quote_list_view'; const quoteData = [ @@ -37,7 +38,7 @@ $(document).ready(function() { const quoteListView = new QuoteListView({ model: quotes, template: _.template($('#quote-template').html()), - el: 'main' + el: '#quotes-container' }); diff --git a/src/models/quote.js b/src/models/quote.js index 618dce1..d915de6 100644 --- a/src/models/quote.js +++ b/src/models/quote.js @@ -7,11 +7,11 @@ const Quote = Backbone.Model.extend({ }, buy() { - this.set('price', this.get('price')+ 1); + this.set('price', this.get('price')+ 1.00); }, sell() { - this.set('price', this.get('price')- 1); + this.set('price', this.get('price')- 1.00); }, }); diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index 3e5fe18..13cafcd 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -1,4 +1,3 @@ -// /src/views/task_list_view.js import Backbone from 'backbone'; import _ from 'underscore'; import QuoteView from '../views/quote_view'; @@ -11,20 +10,14 @@ const QuoteListView = Backbone.View.extend({ }, render() { - // Clear the unordered list - //this.$ -> allows to look up jquery things within this view this.$('#quotes').empty(); - // Iterate through the list rendering each Task this.model.each((quote) => { - // Create a new TaskView with the model & template const quoteView = new QuoteView({ model: quote, template: this.template, tagName: 'li', className: 'quote', }); - // Then render the TaskView - // And append the resulting HTML to the DOM. this.$('#quotes').append(quoteView.render().$el); }); return this; diff --git a/src/views/quote_view.js b/src/views/quote_view.js index 20e5698..0affb88 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -2,36 +2,29 @@ import Backbone from 'backbone'; import Quote from '../models/quote'; const QuoteView = Backbone.View.extend({ - // Params is an object. It contains a template. We're going to assign that template to the new view we are creating. initialize(params) { this.template = params.template; - // good practice to have this in every view this.listenTo(this.model, "change", this.render); }, - // Similar to how we used our underscore templates. It is going to take the template we created on initialize and a model and make a compiled template. + +// This is the portion of the code, that needs to be listening to the buttons for click events, as it is rendering the buttons on the page. render() { const compiledTemplate = this.template(this.model.toJSON()); this.$el.html(compiledTemplate); - if (this.model.get('is_complete')) { - this.$el.addClass('is-complete'); - } else { - this.$el.removeClass('is-complete'); - } + return this; }, events: { - // Event => click, select => button.delete (a button with class delete), function to call => deleteTask - 'click button.btn-buy': 'btn-buy', - 'click button.btn-sell': 'btn-sell', + 'click button.btn-buy': 'buyQuote', + 'click button.btn-sell': 'sellQuote', }, - // All callbakcs for events include an event. You don't have to use it, but it is there if you need it. - buyQuote: function(e) { + buyQuote: function() { this.model.buy(); }, - sellQuote: function(e) { + sellQuote: function() { this.model.sell(); }, From 3d71ed7c7ffc8edfa45bb04e9b959ad7a9b6dbf9 Mon Sep 17 00:00:00 2001 From: Stef Date: Sat, 16 Dec 2017 15:36:17 -0800 Subject: [PATCH 04/20] Can buy a quote. --- src/views/quote_list_view.js | 3 ++- src/views/quote_view.js | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index 13cafcd..4978bce 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -1,5 +1,6 @@ -import Backbone from 'backbone'; import _ from 'underscore'; +import Backbone from 'backbone'; + import QuoteView from '../views/quote_view'; import Quote from '../models/quote'; diff --git a/src/views/quote_view.js b/src/views/quote_view.js index 0affb88..7a2424d 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -1,13 +1,18 @@ +import $ from 'jquery'; +import _ from 'underscore'; + import Backbone from 'backbone'; import Quote from '../models/quote'; +const tradeTemplate = _.template($('#trade-template').html()); + const QuoteView = Backbone.View.extend({ initialize(params) { this.template = params.template; this.listenTo(this.model, "change", this.render); }, -// This is the portion of the code, that needs to be listening to the buttons for click events, as it is rendering the buttons on the page. + // This is the portion of the code, that needs to be listening to the buttons for click events, as it is rendering the buttons on the page. render() { const compiledTemplate = this.template(this.model.toJSON()); this.$el.html(compiledTemplate); @@ -21,6 +26,9 @@ const QuoteView = Backbone.View.extend({ }, buyQuote: function() { + this.model.set('buy', true); + let tradeTemplate = _.template($('#trade-template').html()); + $('#trades').prepend(tradeTemplate(this.model.attributes)); this.model.buy(); }, From 11a4a3bced22134b0d299e5b4c9d6bbb2129e1f7 Mon Sep 17 00:00:00 2001 From: Stef Date: Sat, 16 Dec 2017 15:37:54 -0800 Subject: [PATCH 05/20] Removed unnecessary code. --- src/views/quote_view.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/views/quote_view.js b/src/views/quote_view.js index 7a2424d..589e39f 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -4,7 +4,7 @@ import _ from 'underscore'; import Backbone from 'backbone'; import Quote from '../models/quote'; -const tradeTemplate = _.template($('#trade-template').html()); + const QuoteView = Backbone.View.extend({ initialize(params) { From 5183778db6f266be025c968c36e6d38850d82fda Mon Sep 17 00:00:00 2001 From: Stef Date: Sat, 16 Dec 2017 15:41:07 -0800 Subject: [PATCH 06/20] Can sell quotes, both buying and selling meets requirements. --- src/views/quote_view.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/views/quote_view.js b/src/views/quote_view.js index 589e39f..d26da0a 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -33,6 +33,9 @@ const QuoteView = Backbone.View.extend({ }, sellQuote: function() { + this.model.set('buy', false); + let tradeTemplate = _.template($('#trade-template').html()); + $('#trades').prepend(tradeTemplate(this.model.attributes)); this.model.sell(); }, From 495838368241a72f5892c2cde9ae805019275328 Mon Sep 17 00:00:00 2001 From: Stef Date: Sat, 16 Dec 2017 15:57:29 -0800 Subject: [PATCH 07/20] Added files for orders in views and models. --- src/models/order.js | 0 src/views/order_list_view.js | 0 src/views/order_view.js | 0 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/models/order.js create mode 100644 src/views/order_list_view.js create mode 100644 src/views/order_view.js diff --git a/src/models/order.js b/src/models/order.js new file mode 100644 index 0000000..e69de29 diff --git a/src/views/order_list_view.js b/src/views/order_list_view.js new file mode 100644 index 0000000..e69de29 diff --git a/src/views/order_view.js b/src/views/order_view.js new file mode 100644 index 0000000..e69de29 From d0b1d9d17269d1177a9295f9dc64b09d39fb4d5c Mon Sep 17 00:00:00 2001 From: Stef Date: Sat, 16 Dec 2017 16:09:37 -0800 Subject: [PATCH 08/20] Addition of necessary import/export information. --- src/app.js | 3 +++ src/models/order.js | 3 +++ src/views/order_list_view.js | 10 ++++++++++ src/views/order_view.js | 3 +++ 4 files changed, 19 insertions(+) diff --git a/src/app.js b/src/app.js index 69cc769..54f875b 100644 --- a/src/app.js +++ b/src/app.js @@ -10,6 +10,9 @@ import Simulator from 'models/simulator'; import QuoteList from 'collections/quote_list'; import QuoteListView from './views/quote_list_view'; +import OrderList from 'collections/order_list'; +import OrderListView from './views/order_list_view'; + const quoteData = [ { symbol: 'HUMOR', diff --git a/src/models/order.js b/src/models/order.js index e69de29..74ceb26 100644 --- a/src/models/order.js +++ b/src/models/order.js @@ -0,0 +1,3 @@ +import Backbone from 'backbone'; + +export default Order; diff --git a/src/views/order_list_view.js b/src/views/order_list_view.js index e69de29..e5e57fb 100644 --- a/src/views/order_list_view.js +++ b/src/views/order_list_view.js @@ -0,0 +1,10 @@ +import _ from 'underscore'; +import $ from 'jquery'; + +import Backbone from 'backbone'; + +import OrderView from '../views/order_view'; +import Order from '../models/order' + + +export default OrderListView; diff --git a/src/views/order_view.js b/src/views/order_view.js index e69de29..6053443 100644 --- a/src/views/order_view.js +++ b/src/views/order_view.js @@ -0,0 +1,3 @@ +import Backbone from 'backbone'; + +export default OrderView; From 79fbbb45825d1dd32ce5ac1a23dc2146bdd34b47 Mon Sep 17 00:00:00 2001 From: Stef Date: Sat, 16 Dec 2017 16:29:39 -0800 Subject: [PATCH 09/20] Collections order_list added. --- src/collections/order_list.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/collections/order_list.js diff --git a/src/collections/order_list.js b/src/collections/order_list.js new file mode 100644 index 0000000..8d7cabd --- /dev/null +++ b/src/collections/order_list.js @@ -0,0 +1,8 @@ +import Backbone from 'backbone'; +import Order from 'models/order'; + +const OrderList = Backbone.Collection.extend({ + model: Order, +}); + +export default OrderList; From 249a50411f45f8ec931fd29f2b8089049901bd55 Mon Sep 17 00:00:00 2001 From: Stef Date: Sat, 16 Dec 2017 17:09:55 -0800 Subject: [PATCH 10/20] Added orderList and orderListView to app.js, similar logic to quote. --- src/app.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/app.js b/src/app.js index 54f875b..3d79bd6 100644 --- a/src/app.js +++ b/src/app.js @@ -34,6 +34,7 @@ const quoteData = [ $(document).ready(function() { const quotes = new QuoteList(quoteData); + const orders = new OrderList(); const simulator = new Simulator({ quotes: quotes, }); @@ -44,8 +45,15 @@ $(document).ready(function() { el: '#quotes-container' }); - - simulator.start(); quoteListView.render(); + + const orderListView = new OrderListView({ + model: orders, + template: _.template($('#order-template').html()), + el: 'main' + }); + orderListView.render(); + + simulator.start(); }); From d6cfc70617ed401bad3071b3425d7ad1d499607b Mon Sep 17 00:00:00 2001 From: Stef Date: Sat, 16 Dec 2017 17:55:38 -0800 Subject: [PATCH 11/20] Added basic necessities to build on logic, initially built off quotes. --- src/models/order.js | 15 +++++++++++++++ src/views/order_list_view.js | 23 +++++++++++++++++++++++ src/views/order_view.js | 21 +++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/src/models/order.js b/src/models/order.js index 74ceb26..51904bc 100644 --- a/src/models/order.js +++ b/src/models/order.js @@ -1,3 +1,18 @@ import Backbone from 'backbone'; +const Order = Backbone.Model.extend({ + defaults: { + symbol: , + price: , + }, + + buy() { + + }, + + sell() { + + }, +}); + export default Order; diff --git a/src/views/order_list_view.js b/src/views/order_list_view.js index e5e57fb..68c2ed8 100644 --- a/src/views/order_list_view.js +++ b/src/views/order_list_view.js @@ -6,5 +6,28 @@ import Backbone from 'backbone'; import OrderView from '../views/order_view'; import Order from '../models/order' +const OrderListView = Backbone.View.extend({ + initialize(params) { + this.template = params.template; + this.listenTo(this.model, 'update', this.render); + }, + + render() { + this.$('#orders').empty(); + this.model.each((order) => { + const orderView = new OrderView({ + model: order, + template: this.template, + tagName: 'li', + className: 'order', + }); + this.$('#orders').append(orderView.render().$el); + }); + return this; + }, + +// Need logic for buy, sell, cancel. + +}); export default OrderListView; diff --git a/src/views/order_view.js b/src/views/order_view.js index 6053443..7f49d84 100644 --- a/src/views/order_view.js +++ b/src/views/order_view.js @@ -1,3 +1,24 @@ +import $ from 'jquery'; +import _ from 'underscore'; + import Backbone from 'backbone'; +const OrderView = Backbone.View.extend({ + initialize(params) { + this.template = params.template; + this.listenTo(this.model, "change", this.render); + }, + render() { + const compiledTemplate = this.template(this.model.toJSON()); + this.$el.html(compiledTemplate); + return this; + }, + events: { + + }, + +// Cancel? + +}); + export default OrderView; From 570d60f065af5df75484657d528dd0f34c8cf47a Mon Sep 17 00:00:00 2001 From: Stef Date: Sat, 16 Dec 2017 19:03:32 -0800 Subject: [PATCH 12/20] Events added for buying/selling orders, working on buy order logic. --- src/models/order.js | 3 +-- src/views/order_list_view.js | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/models/order.js b/src/models/order.js index 51904bc..f76fba8 100644 --- a/src/models/order.js +++ b/src/models/order.js @@ -2,8 +2,7 @@ import Backbone from 'backbone'; const Order = Backbone.Model.extend({ defaults: { - symbol: , - price: , + }, buy() { diff --git a/src/views/order_list_view.js b/src/views/order_list_view.js index 68c2ed8..7f056ce 100644 --- a/src/views/order_list_view.js +++ b/src/views/order_list_view.js @@ -26,8 +26,20 @@ const OrderListView = Backbone.View.extend({ return this; }, -// Need logic for buy, sell, cancel. + events: { + 'click button.btn-buy': 'buyOrder', + 'click button.btn-sell': 'sellOrder' + }, + + buyOrder: function(event) { + event.preventDefault(); + + }; + + + } + -}); + }); -export default OrderListView; + export default OrderListView; From 95d7cc74682b0b102409d77394eb024980ea9d05 Mon Sep 17 00:00:00 2001 From: Stef Date: Sat, 16 Dec 2017 19:21:08 -0800 Subject: [PATCH 13/20] Buy logic WIP. --- src/views/order_list_view.js | 17 +++++++++++++---- src/views/quote_view.js | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/views/order_list_view.js b/src/views/order_list_view.js index 7f056ce..6ea462d 100644 --- a/src/views/order_list_view.js +++ b/src/views/order_list_view.js @@ -33,13 +33,22 @@ const OrderListView = Backbone.View.extend({ buyOrder: function(event) { event.preventDefault(); - + const orderData = { + buy: true, + symbol: this.$('select[name=symbol]').val(), + targetPrice: parseFloat(this.$('input[name=price-target]').val()), }; + const newOrder = new Order(orderData); + if (newOrder.isValid()) { + this.model.add(newOrder); + } else { + console.log( 'Invalid Order!' ); + } } + // sell +}); - }); - - export default OrderListView; +export default OrderListView; diff --git a/src/views/quote_view.js b/src/views/quote_view.js index d26da0a..ff8094d 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -23,8 +23,8 @@ const QuoteView = Backbone.View.extend({ events: { 'click button.btn-buy': 'buyQuote', 'click button.btn-sell': 'sellQuote', - }, + buyQuote: function() { this.model.set('buy', true); let tradeTemplate = _.template($('#trade-template').html()); From ce83f10faf065db3feec1e3c5b43cdd7f439b76d Mon Sep 17 00:00:00 2001 From: Stef Date: Sat, 16 Dec 2017 22:02:33 -0800 Subject: [PATCH 14/20] Sell order logic added, working on getting it to work properly. --- src/views/order_list_view.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/views/order_list_view.js b/src/views/order_list_view.js index 6ea462d..79d6f28 100644 --- a/src/views/order_list_view.js +++ b/src/views/order_list_view.js @@ -46,9 +46,25 @@ const OrderListView = Backbone.View.extend({ } else { console.log( 'Invalid Order!' ); } - } + }, + + sellOrder: function(event) { + event.preventDefault(); + + const orderData = { + buy: false, + symbol: this.$('select[name=symbol]').val(), + targetPrice: parseFloat(this.$('input[name=price-target]').val()), + }; - // sell + const newOrder = new Order(orderData); + + if (newOrder.isValid()) { + this.model.add(newOrder); + } else { + console.log( 'Invalid Order!' ); + } + } }); export default OrderListView; From 43593969330563924f521d3a40ff68b86c37271e Mon Sep 17 00:00:00 2001 From: Stef Date: Sat, 16 Dec 2017 22:28:37 -0800 Subject: [PATCH 15/20] Added destroy for cancel button. --- src/views/order_view.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/views/order_view.js b/src/views/order_view.js index 7f49d84..a13ffab 100644 --- a/src/views/order_view.js +++ b/src/views/order_view.js @@ -14,11 +14,12 @@ const OrderView = Backbone.View.extend({ return this; }, events: { - + 'click button.btn-cancel': 'cancelOrder', }, -// Cancel? - + cancelOrder(){ + this.model.destroy(); + }, }); export default OrderView; From 86caafb6359998131313c9efb6ebcc7d9689fd9c Mon Sep 17 00:00:00 2001 From: Stef Date: Mon, 18 Dec 2017 00:24:01 -0800 Subject: [PATCH 16/20] Form working, can buy/sell with no price. --- src/app.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index 3d79bd6..b828170 100644 --- a/src/app.js +++ b/src/app.js @@ -7,9 +7,11 @@ import _ from 'underscore'; import Backbone from 'backbone'; import Simulator from 'models/simulator'; +import Quote from 'models/quote'; import QuoteList from 'collections/quote_list'; import QuoteListView from './views/quote_list_view'; +import Order from 'models/order'; import OrderList from 'collections/order_list'; import OrderListView from './views/order_list_view'; @@ -47,11 +49,21 @@ $(document).ready(function() { }); quoteListView.render(); + const formDropDown = function formDropDown() { + const $formSelect = $('select[name=symbol]'); + + quotes.forEach((quote) => { + const quoteSymbol = quote.get('symbol'); + $formSelect.append(``); + }); + }; + formDropDown(); const orderListView = new OrderListView({ model: orders, template: _.template($('#order-template').html()), - el: 'main' + quoteList: quotes, + el: '#order-workspace' }); orderListView.render(); From c5ea3dfc0c5d2886bee2f31beee1c295283055c9 Mon Sep 17 00:00:00 2001 From: Stef Date: Mon, 18 Dec 2017 00:32:50 -0800 Subject: [PATCH 17/20] Mild clean-up, logic addition to get orders to render WIP, not buying/selling with cost. --- src/models/order.js | 28 +++++++++++++++++++++------- src/models/quote.js | 4 ++-- src/views/order_list_view.js | 7 ++++--- src/views/order_view.js | 4 ++++ src/views/quote_view.js | 16 ++++++++++++++-- 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/models/order.js b/src/models/order.js index f76fba8..b3aaa01 100644 --- a/src/models/order.js +++ b/src/models/order.js @@ -1,16 +1,30 @@ import Backbone from 'backbone'; const Order = Backbone.Model.extend({ - defaults: { - + initialize(params) { + this.buy = params.buy, + this.currentQuote = params.currentQuote, + this.currentPrice = params.currentPrice, + this.targetPrice = params.targetPrice }, - buy() { - - }, - - sell() { + validate(attributes) { + const error = {}; + if (this.buy && this.targetPrice >= this.currentPrice) { + error.targetPrice = ['Price is higher than market, aim lower!']; + } + else if (!this.buy && this.targetPrice <= this.currentPrice) { + error.targetPrice = ['Price is lower than market, aim higher!']; + } + else if (!attributes.targetPrice) { + error.targetPrice = ['Price must not be blank!']; + } + if (Object.keys(error).length < 1) { + return error; + } else { + return false; + } }, }); diff --git a/src/models/quote.js b/src/models/quote.js index d915de6..62583ec 100644 --- a/src/models/quote.js +++ b/src/models/quote.js @@ -7,11 +7,11 @@ const Quote = Backbone.Model.extend({ }, buy() { - this.set('price', this.get('price')+ 1.00); + this.set('price', this.get('price') + 1.00); }, sell() { - this.set('price', this.get('price')- 1.00); + this.set('price', this.get('price') - 1.00); }, }); diff --git a/src/views/order_list_view.js b/src/views/order_list_view.js index 79d6f28..c6be968 100644 --- a/src/views/order_list_view.js +++ b/src/views/order_list_view.js @@ -9,6 +9,7 @@ import Order from '../models/order' const OrderListView = Backbone.View.extend({ initialize(params) { this.template = params.template; + this.quoteList = params.quoteList; this.listenTo(this.model, 'update', this.render); }, @@ -44,10 +45,10 @@ const OrderListView = Backbone.View.extend({ if (newOrder.isValid()) { this.model.add(newOrder); } else { - console.log( 'Invalid Order!' ); + console.log('Invalid Order!'); } }, - + sellOrder: function(event) { event.preventDefault(); @@ -62,7 +63,7 @@ const OrderListView = Backbone.View.extend({ if (newOrder.isValid()) { this.model.add(newOrder); } else { - console.log( 'Invalid Order!' ); + console.log('Invalid Order!'); } } }); diff --git a/src/views/order_view.js b/src/views/order_view.js index a13ffab..5ff9356 100644 --- a/src/views/order_view.js +++ b/src/views/order_view.js @@ -3,16 +3,20 @@ import _ from 'underscore'; import Backbone from 'backbone'; +import Order from '../models/order'; + const OrderView = Backbone.View.extend({ initialize(params) { this.template = params.template; this.listenTo(this.model, "change", this.render); }, + render() { const compiledTemplate = this.template(this.model.toJSON()); this.$el.html(compiledTemplate); return this; }, + events: { 'click button.btn-cancel': 'cancelOrder', }, diff --git a/src/views/quote_view.js b/src/views/quote_view.js index ff8094d..7df8829 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -4,8 +4,6 @@ import _ from 'underscore'; import Backbone from 'backbone'; import Quote from '../models/quote'; - - const QuoteView = Backbone.View.extend({ initialize(params) { this.template = params.template; @@ -39,6 +37,20 @@ const QuoteView = Backbone.View.extend({ this.model.sell(); }, + buyOrder: function(orderView) { + if (orderView.quote == this.model) { + this.buyQuote(); + + } + }, + + sellOrder: function(orderView) { + if (orderView.quote == this.model) { + this.sellQuote(); + + } + }, + }); export default QuoteView; From 8445492e15035b621531988916c2ad749dee0985 Mon Sep 17 00:00:00 2001 From: Stef Date: Mon, 18 Dec 2017 00:47:10 -0800 Subject: [PATCH 18/20] Added some basic order tests. --- spec/models/order_spec.js | 67 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 spec/models/order_spec.js diff --git a/spec/models/order_spec.js b/spec/models/order_spec.js new file mode 100644 index 0000000..b73770b --- /dev/null +++ b/spec/models/order_spec.js @@ -0,0 +1,67 @@ +import Order from 'models/order'; + +describe('Order spec', () => { + + describe('Initialize', () => { + it('Can access all instance variables.', () => { + const order = new Order({ + currentQuote: 'HELLO', + currentPrice: 100.00, + buy: true, + targetPrice: 101.00 + }); + + expect(order.get('currentQuote')).toEqual('HELLO'); + expect(order.get('currentPrice')).toEqual(100.00); + expect(order.get('buy')).toEqual(true); + expect(order.get('targetPrice')).toEqual(101.00); + }); + }); + + describe('Creates a new Order.', () => { + + it('Creates a valid instance of Order', () => { + const order = new Order({ + currentQuote: 'HELLO', + currentPrice: 100.00, + buy: true, + targetPrice: 50.00, + }); + + expect(order.isValid()).toEqual(true); + }); + + it ('Rejects an empty price.', () => { + const order = new Order({ + symbol: 'HELLO', + currentPrice: '', + action: true, + }); + + expect(order.isValid()).toEqual(false); + }); + + it('Can not create an order with target price greater than or equal to the current price when buying.', () => { + const order = new Order({ + currentQuote: 'HELLO', + currentPrice: 100.00, + buy: true, + targetPrice: 101.00, + }); + + expect(order.isValid()).toEqual(false); + }); + + it('Can not create an order with target price less than or equal to the current price when selling.', () => { + const order = new Order({ + currentQuote: 'HELLO', + currentPrice: 100.00, + buy: false, + targetPrice: 99.00, + }); + + expect(order.isValid()).toEqual(false); + }); + + }); +}); From 03225f4fc6b4c1de62809627949d66e5a529d0cd Mon Sep 17 00:00:00 2001 From: Stef Date: Mon, 18 Dec 2017 02:00:13 -0800 Subject: [PATCH 19/20] Removed unnecessary code from previous attempt at another thought process. --- src/views/quote_view.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/views/quote_view.js b/src/views/quote_view.js index 7df8829..3de4d2c 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -36,21 +36,6 @@ const QuoteView = Backbone.View.extend({ $('#trades').prepend(tradeTemplate(this.model.attributes)); this.model.sell(); }, - - buyOrder: function(orderView) { - if (orderView.quote == this.model) { - this.buyQuote(); - - } - }, - - sellOrder: function(orderView) { - if (orderView.quote == this.model) { - this.sellQuote(); - - } - }, - }); export default QuoteView; From dbaba172ddc3ebbc8035ceb7909548f36b2d0902 Mon Sep 17 00:00:00 2001 From: Stef Date: Mon, 18 Dec 2017 08:23:32 -0800 Subject: [PATCH 20/20] Testing something to see if I can get it to buy with a price, know what I need to do will add asap. --- src/views/order_list_view.js | 8 ++++++-- src/views/order_view.js | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/views/order_list_view.js b/src/views/order_list_view.js index c6be968..a97495a 100644 --- a/src/views/order_list_view.js +++ b/src/views/order_list_view.js @@ -38,6 +38,8 @@ const OrderListView = Backbone.View.extend({ buy: true, symbol: this.$('select[name=symbol]').val(), targetPrice: parseFloat(this.$('input[name=price-target]').val()), + currentPrice: 0, + currentQuote: 0 }; const newOrder = new Order(orderData); @@ -45,7 +47,7 @@ const OrderListView = Backbone.View.extend({ if (newOrder.isValid()) { this.model.add(newOrder); } else { - console.log('Invalid Order!'); + console.log('Invalid Order!', newOrder.validationError); } }, @@ -65,7 +67,9 @@ const OrderListView = Backbone.View.extend({ } else { console.log('Invalid Order!'); } - } + }, + + }); export default OrderListView; diff --git a/src/views/order_view.js b/src/views/order_view.js index 5ff9356..289cf99 100644 --- a/src/views/order_view.js +++ b/src/views/order_view.js @@ -8,6 +8,7 @@ import Order from '../models/order'; const OrderView = Backbone.View.extend({ initialize(params) { this.template = params.template; + this.orderTemplate = params.orderTemplate; this.listenTo(this.model, "change", this.render); },