From 1b29b3620749c706c6b1c3dac468c3ea0d3a3d8d Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Tue, 12 Dec 2017 15:19:51 -0800 Subject: [PATCH 01/17] add quote list view and make buy and sell buttons change quote's market price --- src/app.js | 24 ++++++++++++++++ src/views/quote_list_view.js | 54 ++++++++++++++++++++++++++++++++++++ src/views/quote_view.js | 47 +++++++++++++++++++++++++++++++ 3 files changed, 125 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..b0d4855 100644 --- a/src/app.js +++ b/src/app.js @@ -2,10 +2,18 @@ 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'; +import Quote from './models/quote'; +// import TaskList from './collections/task_list'; +import QuoteView from './views/quote_view'; +// import TaskListView from './views/task_list_view'; +import QuoteListView from './views/quote_list_view'; + + const quoteData = [ { symbol: 'HUMOR', @@ -25,11 +33,27 @@ const quoteData = [ }, ]; +const quoteList = new QuoteList(); +let quoteTemplate; + $(document).ready(function() { + quoteTemplate = _.template($('#quote-template').html()); + const quotes = new QuoteList(quoteData); const simulator = new Simulator({ quotes: quotes, }); + // Tasklistview will encompass the main tag + const quoteListView = new QuoteListView({ + el: 'main', + model: quotes, + template: quoteTemplate, + }); + + quoteListView.render(); + + // wave 3 + // any time the stock "changes" change event on model simulator.start(); }); diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js new file mode 100644 index 0000000..6b6acc6 --- /dev/null +++ b/src/views/quote_list_view.js @@ -0,0 +1,54 @@ +import Backbone from 'backbone'; +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() { + this.$('#quotes').empty(); + console.log('in quotelistview render'); + this.model.each((quote) => { + const quoteView = new QuoteView({ + model: quote, + template: this.template, + tagName: 'li', + // TODO: check for styling + className: 'quote', + }); + console.log('in quotelistview render'); + this.$('#quotes').append(quoteView.render().$el); + + }); + return this; + } +}); +// const TaskListView = Backbone.View.extend({ +// initialize(params) { +// this.template = params.template; +// this.listenTo(this.model, 'update', this.render); +// }, +// render() { +// // Clear the unordered list +// this.$('#todo-items').empty(); +// // Iterate through the list rendering each Task +// this.model.each((task) => { +// // Create a new TaskView with the model & template +// const taskView = new TaskView({ +// model: task, +// template: this.template, +// tagName: 'li', +// className: 'task', +// }); +// // Then render the TaskView +// // And append the resulting HTML to the DOM. +// this.$('#todo-items').append(taskView.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..343baa8 --- /dev/null +++ b/src/views/quote_view.js @@ -0,0 +1,47 @@ +import Backbone from 'backbone'; +import Quote from '../models/quote'; + +const QuoteView = Backbone.View.extend({ + + initialize(params) { + // use #quote-template + this.template = params.template; + // any time the stock "changes" change event on model + this.listenTo(this.model, 'change', this.render); + }, + events: { + 'click button.btn-buy': 'buyQuote', + 'click button.btn-sell': 'sellQuote', + }, + buyQuote() { + console.log('buying quote'); + console.log(this.model.get('price')); + // $() + let newPrice = this.model.get('price') + 1; + this.model.set({price: newPrice}); + console.log(this.model.get('price')); + + }, + sellQuote() { + console.log('selling quote'); + console.log(this.model.get('price')); + let newPrice = this.model.get('price') - 1; + this.model.set({price: newPrice}); + console.log(this.model.get('price')); + + }, + render() { + const compiledTemplate = this.template(this.model.toJSON()); + this.$el.html(compiledTemplate); + return this; + }, + +// Click the Buy button for a quote: +// That quote's market price increases by $1.00 +// Click the Sell button for a quote: +// That quote's market price decreases by $1.00 + + +}); + +export default QuoteView; From 0d08a664db6a68fe3ec8c137daa50aa3bef6bf6e Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Tue, 12 Dec 2017 15:53:48 -0800 Subject: [PATCH 02/17] moving buy and sell functions to quote model --- src/app.js | 14 ++++++++++- src/models/quote.js | 11 ++++++++- src/models/trade.js | 0 src/views/quote_view.js | 15 ++--------- src/views/trade_list_view.js | 48 ++++++++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 src/models/trade.js create mode 100644 src/views/trade_list_view.js diff --git a/src/app.js b/src/app.js index b0d4855..e841c9b 100644 --- a/src/app.js +++ b/src/app.js @@ -12,6 +12,8 @@ import Quote from './models/quote'; import QuoteView from './views/quote_view'; // import TaskListView from './views/task_list_view'; import QuoteListView from './views/quote_list_view'; +import TradeListView from './views/trade_list_view'; + const quoteData = [ @@ -35,22 +37,32 @@ const quoteData = [ const quoteList = new QuoteList(); let quoteTemplate; +let tradeTemplate; $(document).ready(function() { quoteTemplate = _.template($('#quote-template').html()); + tradeTemplate = _.template($('#trade-template').html()); + const quotes = new QuoteList(quoteData); const simulator = new Simulator({ quotes: quotes, }); - // Tasklistview will encompass the main tag + // Quotelistview will encompass the main tag const quoteListView = new QuoteListView({ el: 'main', model: quotes, template: quoteTemplate, }); + // TradeHistoryView will encompass the main tag + const tradelistView = new TradeListView({ + el: 'main', + model: quotes, + template: tradeTemplate, + }); + quoteListView.render(); // wave 3 diff --git a/src/models/quote.js b/src/models/quote.js index 4fbf466..c0f0b54 100644 --- a/src/models/quote.js +++ b/src/models/quote.js @@ -7,11 +7,20 @@ const Quote = Backbone.Model.extend({ }, buy() { - // Implement this function to increase the price by $1.00 + console.log('buying quote'); + console.log(this.get('price')); + let newPrice = this.get('price') + 1; + this.set({price: newPrice}); + console.log(this.get('price')); }, sell() { // Implement this function to decrease the price by $1.00 + console.log('selling quote'); + console.log(this.get('price')); + let newPrice = this.get('price') - 1; + this.set({price: newPrice}); + console.log(this.get('price')); }, }); diff --git a/src/models/trade.js b/src/models/trade.js new file mode 100644 index 0000000..e69de29 diff --git a/src/views/quote_view.js b/src/views/quote_view.js index 343baa8..d1ed508 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -14,21 +14,10 @@ const QuoteView = Backbone.View.extend({ 'click button.btn-sell': 'sellQuote', }, buyQuote() { - console.log('buying quote'); - console.log(this.model.get('price')); - // $() - let newPrice = this.model.get('price') + 1; - this.model.set({price: newPrice}); - console.log(this.model.get('price')); - + this.model.buy(); }, sellQuote() { - console.log('selling quote'); - console.log(this.model.get('price')); - let newPrice = this.model.get('price') - 1; - this.model.set({price: newPrice}); - console.log(this.model.get('price')); - + this.model.sell(); }, render() { const compiledTemplate = this.template(this.model.toJSON()); diff --git a/src/views/trade_list_view.js b/src/views/trade_list_view.js new file mode 100644 index 0000000..f697fa8 --- /dev/null +++ b/src/views/trade_list_view.js @@ -0,0 +1,48 @@ +import Backbone from 'backbone'; +import Quote from '../models/quote'; + +const TradeListView = Backbone.View.extend({ +// +// initialize(params) { +// // use #quote-template +// this.template = params.template; +// // any time the stock "changes" change event on model +// this.listenTo(this.model, 'change', this.render); +// }, +// events: { +// 'click button.btn-buy': 'buyQuote', +// 'click button.btn-sell': 'sellQuote', +// }, +// buyQuote() { +// console.log('buying quote'); +// console.log(this.model.get('price')); +// // change no JQUERY +// // $('#trades').prepend(); +// let newPrice = this.model.get('price') + 1; +// this.model.set({price: newPrice}); +// console.log(this.model.get('price')); +// +// }, +// sellQuote() { +// console.log('selling quote'); +// console.log(this.model.get('price')); +// let newPrice = this.model.get('price') - 1; +// this.model.set({price: newPrice}); +// console.log(this.model.get('price')); +// +// }, +// render() { +// const compiledTemplate = this.template(this.model.toJSON()); +// this.$el.html(compiledTemplate); +// return this; +// }, +// +// // Click the Buy button for a quote: +// // That quote's market price increases by $1.00 +// // Click the Sell button for a quote: +// // That quote's market price decreases by $1.00 + + +}); + +export default TradeListView; From 5f15c9b2fd1b26a93a7704bc121506591c318db7 Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Tue, 12 Dec 2017 20:34:37 -0800 Subject: [PATCH 03/17] add tradelistview and event bus --- src/app.js | 10 ++++++-- src/models/quote.js | 1 - src/models/trade.js | 0 src/views/quote_list_view.js | 2 ++ src/views/quote_view.js | 22 +++++++++++----- src/views/trade_list_view.js | 50 +++++++----------------------------- 6 files changed, 35 insertions(+), 50 deletions(-) delete mode 100644 src/models/trade.js diff --git a/src/app.js b/src/app.js index e841c9b..7aedfa2 100644 --- a/src/app.js +++ b/src/app.js @@ -3,6 +3,7 @@ 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'; @@ -40,6 +41,9 @@ let quoteTemplate; let tradeTemplate; $(document).ready(function() { + let bus = {}; + // need to import backbone + bus = _.extend(bus, Backbone.Events); quoteTemplate = _.template($('#quote-template').html()); tradeTemplate = _.template($('#trade-template').html()); @@ -54,12 +58,14 @@ $(document).ready(function() { el: 'main', model: quotes, template: quoteTemplate, + bus: bus, }); // TradeHistoryView will encompass the main tag const tradelistView = new TradeListView({ - el: 'main', - model: quotes, + el: '#trades-container', + bus: bus, + // model: quotes, template: tradeTemplate, }); diff --git a/src/models/quote.js b/src/models/quote.js index c0f0b54..50f359f 100644 --- a/src/models/quote.js +++ b/src/models/quote.js @@ -15,7 +15,6 @@ const Quote = Backbone.Model.extend({ }, sell() { - // Implement this function to decrease the price by $1.00 console.log('selling quote'); console.log(this.get('price')); let newPrice = this.get('price') - 1; diff --git a/src/models/trade.js b/src/models/trade.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index 6b6acc6..c43b8a4 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -7,6 +7,7 @@ const QuoteListView = Backbone.View.extend({ initialize(params) { this.template = params.template; this.listenTo(this.model, 'update', this.render); + this.bus = params.bus; }, render() { this.$('#quotes').empty(); @@ -18,6 +19,7 @@ const QuoteListView = Backbone.View.extend({ tagName: 'li', // TODO: check for styling className: 'quote', + bus: this.bus, }); console.log('in quotelistview render'); this.$('#quotes').append(quoteView.render().$el); diff --git a/src/views/quote_view.js b/src/views/quote_view.js index d1ed508..c61ad2b 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -6,6 +6,7 @@ const QuoteView = Backbone.View.extend({ initialize(params) { // use #quote-template this.template = params.template; + this.bus = params.bus; // any time the stock "changes" change event on model this.listenTo(this.model, 'change', this.render); }, @@ -14,9 +15,24 @@ const QuoteView = Backbone.View.extend({ 'click button.btn-sell': 'sellQuote', }, buyQuote() { + // this.bus.trigger('selected_task', this.model); + + let tradeObject = { + price: this.model.get('price'), + symbol: this.model.get('symbol'), + buy: true, + } + + this.bus.trigger('add_trade', tradeObject) this.model.buy(); }, sellQuote() { + let tradeObject = { + price: this.model.get('price'), + symbol: this.model.get('symbol'), + buy: false, + } + this.bus.trigger('add_trade', tradeObject) this.model.sell(); }, render() { @@ -25,12 +41,6 @@ const QuoteView = Backbone.View.extend({ return this; }, -// Click the Buy button for a quote: -// That quote's market price increases by $1.00 -// Click the Sell button for a quote: -// That quote's market price decreases by $1.00 - - }); export default QuoteView; diff --git a/src/views/trade_list_view.js b/src/views/trade_list_view.js index f697fa8..5e9ab09 100644 --- a/src/views/trade_list_view.js +++ b/src/views/trade_list_view.js @@ -2,47 +2,15 @@ import Backbone from 'backbone'; import Quote from '../models/quote'; const TradeListView = Backbone.View.extend({ -// -// initialize(params) { -// // use #quote-template -// this.template = params.template; -// // any time the stock "changes" change event on model -// this.listenTo(this.model, 'change', this.render); -// }, -// events: { -// 'click button.btn-buy': 'buyQuote', -// 'click button.btn-sell': 'sellQuote', -// }, -// buyQuote() { -// console.log('buying quote'); -// console.log(this.model.get('price')); -// // change no JQUERY -// // $('#trades').prepend(); -// let newPrice = this.model.get('price') + 1; -// this.model.set({price: newPrice}); -// console.log(this.model.get('price')); -// -// }, -// sellQuote() { -// console.log('selling quote'); -// console.log(this.model.get('price')); -// let newPrice = this.model.get('price') - 1; -// this.model.set({price: newPrice}); -// console.log(this.model.get('price')); -// -// }, -// render() { -// const compiledTemplate = this.template(this.model.toJSON()); -// this.$el.html(compiledTemplate); -// return this; -// }, -// -// // Click the Buy button for a quote: -// // That quote's market price increases by $1.00 -// // Click the Sell button for a quote: -// // That quote's market price decreases by $1.00 - - + initialize(params) { + this.template = params.template; + this.bus = params.bus; + this.listenTo(this.bus, 'add_trade', this.render); + }, + render(tradeObject) { + const compiledTemplate= this.template(tradeObject) + this.$('#trades').prepend(compiledTemplate); + }, }); export default TradeListView; From 554a3a378bb4ed6a6809d15bce91aadc0ed869eb Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Wed, 13 Dec 2017 09:41:42 -0800 Subject: [PATCH 04/17] set up open order collection, model, and views --- src/app.js | 9 ++++-- src/collections/open_order_list.js | 8 ++++++ src/models/open_order.js | 26 +++++++++++++++++ src/views/open_order_list_view.js | 33 +++++++++++++++++++++ src/views/open_order_view.js | 46 ++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 src/collections/open_order_list.js create mode 100644 src/models/open_order.js create mode 100644 src/views/open_order_list_view.js create mode 100644 src/views/open_order_view.js diff --git a/src/app.js b/src/app.js index 7aedfa2..86227a5 100644 --- a/src/app.js +++ b/src/app.js @@ -7,13 +7,16 @@ import Backbone from 'backbone'; import Simulator from 'models/simulator'; import QuoteList from 'collections/quote_list'; +import OpenOrderList from 'collections/open_order_list'; import Quote from './models/quote'; -// import TaskList from './collections/task_list'; import QuoteView from './views/quote_view'; -// import TaskListView from './views/task_list_view'; import QuoteListView from './views/quote_list_view'; import TradeListView from './views/trade_list_view'; +import OpenOrder from './models/open_order'; +import OpenOrderView from './views/open_order_view'; +import OpenOrderListView from './views/open_order_list_view'; + @@ -61,7 +64,7 @@ $(document).ready(function() { bus: bus, }); - // TradeHistoryView will encompass the main tag + // TradelistView will encompass the main tag const tradelistView = new TradeListView({ el: '#trades-container', bus: bus, diff --git a/src/collections/open_order_list.js b/src/collections/open_order_list.js new file mode 100644 index 0000000..24aa3d5 --- /dev/null +++ b/src/collections/open_order_list.js @@ -0,0 +1,8 @@ +import Backbone from 'backbone'; +import OpenOrder from 'models/open_order'; + +const OpenOrderList = Backbone.Collection.extend({ + model: OpenOrder, +}); + +export default OpenOrderList; diff --git a/src/models/open_order.js b/src/models/open_order.js new file mode 100644 index 0000000..245ab68 --- /dev/null +++ b/src/models/open_order.js @@ -0,0 +1,26 @@ +import Backbone from 'backbone'; + +const OpenOrder = Backbone.Model.extend({ + // defaults: { + // symbol: 'UNDEF', + // price: 0.00 + // }, + // + // buy() { + // console.log('buying quote'); + // console.log(this.get('price')); + // let newPrice = this.get('price') + 1; + // this.set({price: newPrice}); + // console.log(this.get('price')); + // }, + // + // sell() { + // console.log('selling quote'); + // console.log(this.get('price')); + // let newPrice = this.get('price') - 1; + // this.set({price: newPrice}); + // console.log(this.get('price')); + // }, +}); + +export default OpenOrder; diff --git a/src/views/open_order_list_view.js b/src/views/open_order_list_view.js new file mode 100644 index 0000000..dbac3c2 --- /dev/null +++ b/src/views/open_order_list_view.js @@ -0,0 +1,33 @@ +import Backbone from 'backbone'; +import OpenOrderView from '../views/open_order_view' + +import OpenOrder from '../models/open_order'; + +const OpenOrderListView = Backbone.View.extend({ + initialize(params) { + // this.template = params.template; + // this.listenTo(this.model, 'update', this.render); + // this.bus = params.bus; + }, + render() { + this.$('#quotes').empty(); + console.log('in quotelistview render'); + this.model.each((quote) => { + const quoteView = new QuoteView({ + model: quote, + template: this.template, + tagName: 'li', + // TODO: check for styling + className: 'quote', + bus: this.bus, + }); + console.log('in quotelistview render'); + this.$('#quotes').append(quoteView.render().$el); + + }); + return this; + } +}); + + +export default OpenOrderListView; diff --git a/src/views/open_order_view.js b/src/views/open_order_view.js new file mode 100644 index 0000000..1749f7c --- /dev/null +++ b/src/views/open_order_view.js @@ -0,0 +1,46 @@ +import Backbone from 'backbone'; +import OpenOrder from '../models/open_order'; + +const OpenOrderView = Backbone.View.extend({ + // + // initialize(params) { + // // use #quote-template + // this.template = params.template; + // this.bus = params.bus; + // // any time the stock "changes" change event on model + // this.listenTo(this.model, 'change', this.render); + // }, + // events: { + // 'click button.btn-buy': 'buyQuote', + // 'click button.btn-sell': 'sellQuote', + // }, + // buyQuote() { + // // this.bus.trigger('selected_task', this.model); + // + // let tradeObject = { + // price: this.model.get('price'), + // symbol: this.model.get('symbol'), + // buy: true, + // } + // + // this.bus.trigger('add_trade', tradeObject) + // this.model.buy(); + // }, + // sellQuote() { + // let tradeObject = { + // price: this.model.get('price'), + // symbol: this.model.get('symbol'), + // buy: false, + // } + // this.bus.trigger('add_trade', tradeObject) + // this.model.sell(); + // }, + // render() { + // const compiledTemplate = this.template(this.model.toJSON()); + // this.$el.html(compiledTemplate); + // return this; + // }, + +}); + +export default OpenOrderView; From b6a7c9c1434462be5f7e5e9e89d3748622ce53b7 Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Thu, 14 Dec 2017 15:50:44 -0800 Subject: [PATCH 05/17] implement open order purchasing functionality --- src/app.js | 15 ++- src/collections/quote_list.js | 7 ++ src/models/open_order.js | 58 ++++++---- src/views/open_order_list_view.js | 184 ++++++++++++++++++++++++++++-- src/views/open_order_view.js | 64 +++++------ src/views/quote_list_view.js | 27 ++++- src/views/quote_view.js | 3 + 7 files changed, 284 insertions(+), 74 deletions(-) diff --git a/src/app.js b/src/app.js index 86227a5..c35a076 100644 --- a/src/app.js +++ b/src/app.js @@ -39,15 +39,20 @@ const quoteData = [ }, ]; -const quoteList = new QuoteList(); +// const quoteList = new QuoteList(); +const openOrderList = new OpenOrderList(); + let quoteTemplate; let tradeTemplate; +let orderTemplate; $(document).ready(function() { let bus = {}; // need to import backbone bus = _.extend(bus, Backbone.Events); quoteTemplate = _.template($('#quote-template').html()); + orderTemplate = _.template($('#order-template').html()); + tradeTemplate = _.template($('#trade-template').html()); @@ -72,6 +77,14 @@ $(document).ready(function() { template: tradeTemplate, }); + // OpenOrderListview will encompass the orderWorkspace div + const openOrderListView = new OpenOrderListView({ + el: '#order-workspace', + model: openOrderList, + template: orderTemplate, + bus: bus, + }); + quoteListView.render(); // wave 3 diff --git a/src/collections/quote_list.js b/src/collections/quote_list.js index 8da08cb..d174361 100644 --- a/src/collections/quote_list.js +++ b/src/collections/quote_list.js @@ -3,6 +3,13 @@ import Quote from 'models/quote'; const QuoteList = Backbone.Collection.extend({ model: Quote, + symbols() { + let symbols = []; + this.each((quote) => { + symbols.push(quote.get('symbol')); + }); + return symbols; + } }); export default QuoteList; diff --git a/src/models/open_order.js b/src/models/open_order.js index 245ab68..f5a5c18 100644 --- a/src/models/open_order.js +++ b/src/models/open_order.js @@ -1,26 +1,44 @@ import Backbone from 'backbone'; const OpenOrder = Backbone.Model.extend({ - // defaults: { - // symbol: 'UNDEF', - // price: 0.00 - // }, - // - // buy() { - // console.log('buying quote'); - // console.log(this.get('price')); - // let newPrice = this.get('price') + 1; - // this.set({price: newPrice}); - // console.log(this.get('price')); - // }, - // - // sell() { - // console.log('selling quote'); - // console.log(this.get('price')); - // let newPrice = this.get('price') - 1; - // this.set({price: newPrice}); - // console.log(this.get('price')); - // }, + initialize(attributes) { + }, + // TODO: validations + validate(attributes) { + const errors = {}; + if (!attributes.targetPrice) { + errors['target_price'] = ["Price cannot be blank."]; + console.log('error') + } + // if buy is true + // if (attributes.buy && attributes.targetPrice >= CurrentMarketPrice) { + // errors['target_price'] = ["Price cannot be blank."]; + // } + // if (!attributes.task_name) { + // errors['task_name'] = ["Task name is required"]; + // } + // + if ( Object.keys(errors).length > 0 ) { + return errors; + } else { + return false; + } + }, }); +// Click Buy in the order entry form: +// If the target price is blank OR is greater than or equal to the current market price: +// That order is not created and an appropriate error message is displayed beneath the form +// If the target price is less than to the current market price: +// A new Buy order is added to the bottom of the open orders list, with the: +// Symbol from the the form +// Target price from the form +// Click Sell in the order entry form: +// If the target price is blank OR is less than or equal to the current market price: +// That order is not created and an appropriate error message is displayed beneath the form +// If the target price is greater than the current market price: +// A new Sell order is added to the bottom of the open orders list, with the: +// Symbol from the the form +// Target price from the form + export default OpenOrder; diff --git a/src/views/open_order_list_view.js b/src/views/open_order_list_view.js index dbac3c2..d743878 100644 --- a/src/views/open_order_list_view.js +++ b/src/views/open_order_list_view.js @@ -5,28 +5,188 @@ import OpenOrder from '../models/open_order'; const OpenOrderListView = Backbone.View.extend({ initialize(params) { - // this.template = params.template; + this.template = params.template; // this.listenTo(this.model, 'update', this.render); - // this.bus = params.bus; + this.bus = params.bus; + this.listenTo(this.model, 'update', this.render); + this.listenTo(this.bus, 'quote_symbols', this.symbolDropdown); + // when the quotelist changes, the checkOpenOrders function will check if orders should be purchased or sold + this.listenTo(this.bus, 'updated_quote_list', this.checkOpenOrders); + }, render() { - this.$('#quotes').empty(); - console.log('in quotelistview render'); - this.model.each((quote) => { - const quoteView = new QuoteView({ - model: quote, + this.$('#orders').empty(); + + this.model.each((openOrder) => { + const openOrderView = new OpenOrderView({ + model: openOrder, template: this.template, tagName: 'li', - // TODO: check for styling - className: 'quote', + className: 'order', bus: this.bus, }); - console.log('in quotelistview render'); - this.$('#quotes').append(quoteView.render().$el); + // render returns the taskview which (this) which allows you to append + this.$('#orders').append(openOrderView.render().$el); + // this.$('#orders').append(openOrderView.render().$el); + // at this point want to start listening for things + // this list view is going to listen to task view for edit me events + // now any time anyone clicks the edit button on a task view it will call + // TODO LISTEN FOR CANCEL BUTTON AND FOR TARGET PRICE + // this.listenTo(taskView, 'edit_me', this.editTask); }); return this; - } + }, + checkOpenOrders(quoteList) { + console.log('in open orders'); + console.log(quoteList) + this.model.each((openOrder) => { + quoteList.each((quote) => { + // if you are buying + if (openOrder.get('buy') && openOrder.get('symbol') === quote.get('symbol')) { + console.log('order target price'); + console.log(openOrder.get('targetPrice')); + console.log('quote price'); + console.log(quote.get('price')); + + if (openOrder.get('targetPrice') <= quote.get('price')) { + let tradeObject = { + price: openOrder.get('targetPrice'), + symbol: openOrder.get('symbol'), + buy: true, + } + + this.bus.trigger('add_trade', tradeObject) + console.log(openOrder); + // destroy purchased open order + openOrder.destroy(); + // make sure quote is recognized as bought so price increases appropriately + quote.buy(); + + } + + + // If the target price is less than to the current market price: + // A new Buy order is added to the bottom of the open orders list, with the: + // Symbol from the the form + // Target price from the form + + + } + // else if { + // + // } + }); + }); + }, + symbolDropdown(quotes) { + quotes.forEach((quote) => { + // move to model?? + console.log('in symbol dropdown'); + // console.log(quote.get('symbol')); + // this.$('select[name=symbol]').append(``) + this.$('select[name=symbol]').append(``) + // input[name=${field}] + // + }); + }, + events: { + 'click button.btn-buy': 'addBuyOpenOrder', + 'click button.btn-sell': 'addSellOpenOrder', + + }, + updateStatusMessageFrom(messageHash) { + const $formErrors = this.$('.form-errors'); + + $formErrors.empty(); + Object.keys(messageHash).forEach((messageType) => { + messageHash[messageType].forEach((message) => { + $formErrors.append(`
  • ${message}
  • `); + }); + $formErrors.show(); + }); + }, + addBuyOpenOrder(event) { + event.preventDefault(); + + let formData = this.getFormData(); + console.log(formData); + + formData['buy'] = true; + console.log(formData); + const newOpenOrder = new OpenOrder(formData); + if (newOpenOrder.isValid()) { + this.model.add(newOpenOrder); + this.clearFormData(); + console.log('in add buy open order'); + } else { + console.log('ERROR'); + this.updateStatusMessageFrom(newOpenOrder.validationError); + newOpenOrder.destroy(); + } + }, + addSellOpenOrder(event) { + event.preventDefault(); + + let formData = this.getFormData(); + console.log(formData); + + formData['buy'] = false; + console.log(formData); + const newOpenOrder = new OpenOrder(formData); + // if is valid add newTask and clearformdata + if (newOpenOrder.isValid()) { + this.model.add(newOpenOrder); + this.clearFormData(); + console.log('in add sell open order'); + } else { + console.log('ERROR'); + this.updateStatusMessageFrom(newOpenOrder.validationError); + newOpenOrder.destroy(); + } + }, + + // break out into helper function + getFormData() { + console.log('in get form data'); + const openOrderData = {}; + ['symbol', 'price-target'].forEach((field) => { + let val; + // const val = + // this.$(`.order-entry-form input[name=${field}]`).val(); + console.log(field); + if (field === 'symbol') { + console.log('in if statement if field') + val = + this.$(`.order-entry-form select[name=${field}]`).val(); + } else { + val = + parseFloat(this.$(`.order-entry-form input[name=${field}]`).val()); + field = 'targetPrice'; + } + console.log(field); + // console.log('button buy or sell') + // console.log(this.$(`.order-entry-form button`).text()) + console.log(val); + // const val = this.$( `.order-entry-form select[name=${field}] option:selected` ).text(); + if (val !== '') { + openOrderData[field] = val; + } + }); + console.log('openOrderData'); + + console.log(openOrderData); + return openOrderData; + }, + clearFormData () { + ['symbol', 'price-target'].forEach((field) => { + if (field === 'symbol') { + this.$(`.order-entry-form select[name=${field}]`).val(''); + } else { + this.$(`.order-entry-form input[name=${field}]`).val(''); + } + }); + }, }); diff --git a/src/views/open_order_view.js b/src/views/open_order_view.js index 1749f7c..2f32107 100644 --- a/src/views/open_order_view.js +++ b/src/views/open_order_view.js @@ -2,44 +2,32 @@ import Backbone from 'backbone'; import OpenOrder from '../models/open_order'; const OpenOrderView = Backbone.View.extend({ - // - // initialize(params) { - // // use #quote-template - // this.template = params.template; - // this.bus = params.bus; - // // any time the stock "changes" change event on model - // this.listenTo(this.model, 'change', this.render); - // }, - // events: { - // 'click button.btn-buy': 'buyQuote', - // 'click button.btn-sell': 'sellQuote', - // }, - // buyQuote() { - // // this.bus.trigger('selected_task', this.model); - // - // let tradeObject = { - // price: this.model.get('price'), - // symbol: this.model.get('symbol'), - // buy: true, - // } - // - // this.bus.trigger('add_trade', tradeObject) - // this.model.buy(); - // }, - // sellQuote() { - // let tradeObject = { - // price: this.model.get('price'), - // symbol: this.model.get('symbol'), - // buy: false, - // } - // this.bus.trigger('add_trade', tradeObject) - // this.model.sell(); - // }, - // render() { - // const compiledTemplate = this.template(this.model.toJSON()); - // this.$el.html(compiledTemplate); - // return this; - // }, + initialize(params) { + this.template = params.template; + this.bus = params.bus; + 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': 'deleteOpenOrder', + // 'click button.toggle-complete': 'toggleComplete', + // 'click button.edit': 'editTask', + // 'click': 'selectTask' + }, + + deleteOpenOrder(event) { + // deletes model, removes from collection, and stops it from listening + // better to tell model to remove itself + // this.remove will tell the view to remove itself from the DOM + // may be unecessary because will re-render + this.model.destroy(); + this.remove(); + }, }); diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index c43b8a4..c0a371f 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -7,11 +7,15 @@ const QuoteListView = Backbone.View.extend({ initialize(params) { this.template = params.template; this.listenTo(this.model, 'update', this.render); + // when quotes change execute currentQuoteList function which will trigger an event on the bus that will pass the current quotelist to this.bus = params.bus; + this.listenTo(this.bus, 'quote_change', this.currentQuoteList); + }, render() { this.$('#quotes').empty(); - console.log('in quotelistview render'); + console.log('in quotelistview render'); + console.log(this.model) this.model.each((quote) => { const quoteView = new QuoteView({ model: quote, @@ -21,12 +25,29 @@ const QuoteListView = Backbone.View.extend({ className: 'quote', bus: this.bus, }); - console.log('in quotelistview render'); + // console.log('the quotelist'); + // console.log(this.model); + console.log('in quotelistview render after quote models loop'); this.$('#quotes').append(quoteView.render().$el); + console.log('after quoteview is appended'); }); + //TODO: check passing + // let quotes = this.model; + // console.log(quotes); + console.log('symbols array') + console.log(this.model.symbols()); + let quotes = this.model.symbols(); + this.bus.trigger('quote_symbols', quotes); return this; - } + }, + //currentQuoteList triggers an updated_quote_list event which the openOrderListView will listen for + currentQuoteList() { + console.log('in currentQuoteList'); + console.log(this.model); + let quoteList = this.model + this.bus.trigger('updated_quote_list',quoteList); + }, }); // const TaskListView = Backbone.View.extend({ // initialize(params) { diff --git a/src/views/quote_view.js b/src/views/quote_view.js index c61ad2b..087356c 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -36,6 +36,9 @@ const QuoteView = Backbone.View.extend({ this.model.sell(); }, render() { + // trigger quote_change event which quotelist view will listen for + this.bus.trigger('quote_change'); + console.log(this.model); const compiledTemplate = this.template(this.model.toJSON()); this.$el.html(compiledTemplate); return this; From bb8132776ee6ba7e7e0050f57815a8f79a4b903a Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Fri, 15 Dec 2017 10:29:57 -0800 Subject: [PATCH 06/17] change open order buying and selling functionality to involve listening and triggering events on bus through quote view and open order list view --- src/models/open_order.js | 3 + src/views/open_order_list_view.js | 330 +++++++++++++++++------------- src/views/quote_list_view.js | 2 +- src/views/quote_view.js | 4 +- 4 files changed, 197 insertions(+), 142 deletions(-) diff --git a/src/models/open_order.js b/src/models/open_order.js index f5a5c18..96f593d 100644 --- a/src/models/open_order.js +++ b/src/models/open_order.js @@ -10,6 +10,9 @@ const OpenOrder = Backbone.Model.extend({ errors['target_price'] = ["Price cannot be blank."]; console.log('error') } + // TODO: get CurrentMarketPrice from quote +// If the target price is blank OR is greater than or equal to the current market price: +// That order is not created and an appropriate error message is displayed beneath the form // if buy is true // if (attributes.buy && attributes.targetPrice >= CurrentMarketPrice) { // errors['target_price'] = ["Price cannot be blank."]; diff --git a/src/views/open_order_list_view.js b/src/views/open_order_list_view.js index d743878..7b84918 100644 --- a/src/views/open_order_list_view.js +++ b/src/views/open_order_list_view.js @@ -10,8 +10,10 @@ const OpenOrderListView = Backbone.View.extend({ this.bus = params.bus; this.listenTo(this.model, 'update', this.render); this.listenTo(this.bus, 'quote_symbols', this.symbolDropdown); + this.listenTo(this.bus, 'quote_change', this.checkOpenOrders); + // when the quotelist changes, the checkOpenOrders function will check if orders should be purchased or sold - this.listenTo(this.bus, 'updated_quote_list', this.checkOpenOrders); + // this.listenTo(this.bus, 'updated_quote_list', this.checkOpenOrders); }, render() { @@ -37,156 +39,204 @@ const OpenOrderListView = Backbone.View.extend({ }); return this; }, - checkOpenOrders(quoteList) { + // checkOpenOrders(quoteList) { + checkOpenOrders(quote) { console.log('in open orders'); - console.log(quoteList) - this.model.each((openOrder) => { - quoteList.each((quote) => { - // if you are buying - if (openOrder.get('buy') && openOrder.get('symbol') === quote.get('symbol')) { - console.log('order target price'); - console.log(openOrder.get('targetPrice')); - console.log('quote price'); - console.log(quote.get('price')); - - if (openOrder.get('targetPrice') <= quote.get('price')) { - let tradeObject = { - price: openOrder.get('targetPrice'), - symbol: openOrder.get('symbol'), - buy: true, - } - - this.bus.trigger('add_trade', tradeObject) - console.log(openOrder); - // destroy purchased open order - openOrder.destroy(); - // make sure quote is recognized as bought so price increases appropriately - quote.buy(); + // console.log(quoteList) + // var musketeers = friends.where({job: "Musketeer"}); + let openOrders = this.model.where({symbol: quote.get('symbol')}); + console.log(openOrders); + if (openOrders.length >= 1) { + openOrders.forEach((openOrder) => { + // if you are buying + if (openOrder.get('buy') && openOrder.get('targetPrice') >= quote.get('price')) { + let tradeObject = { + price: openOrder.get('targetPrice'), + symbol: openOrder.get('symbol'), + buy: true, } - - - // If the target price is less than to the current market price: - // A new Buy order is added to the bottom of the open orders list, with the: - // Symbol from the the form - // Target price from the form - - + this.bus.trigger('add_trade', tradeObject) + console.log(openOrder); + // destroy purchased open order + openOrder.destroy(); + // make sure quote is recognized as bought so price increases appropriately + quote.buy(); } - // else if { - // - // } - }); - }); - }, - symbolDropdown(quotes) { - quotes.forEach((quote) => { - // move to model?? - console.log('in symbol dropdown'); - // console.log(quote.get('symbol')); - // this.$('select[name=symbol]').append(``) - this.$('select[name=symbol]').append(``) - // input[name=${field}] - // - }); - }, - events: { - 'click button.btn-buy': 'addBuyOpenOrder', - 'click button.btn-sell': 'addSellOpenOrder', - - }, - updateStatusMessageFrom(messageHash) { - const $formErrors = this.$('.form-errors'); + else if ((!openOrder.get('buy')) && openOrder.get('targetPrice') <= quote.get('price')) { + let tradeObject = { + price: openOrder.get('targetPrice'), + symbol: openOrder.get('symbol'), + buy: false, + } - $formErrors.empty(); - Object.keys(messageHash).forEach((messageType) => { - messageHash[messageType].forEach((message) => { - $formErrors.append(`
  • ${message}
  • `); + this.bus.trigger('add_trade', tradeObject) + console.log(openOrder); + // destroy purchased open order + openOrder.destroy(); + // make sure quote is recognized as sold so price decreases appropriately + quote.sell(); + } }); - $formErrors.show(); + } + // console.log(openOrders); + // this.model.each((openOrder) => { + // quoteList.each((quote) => { + // // if you are buying + // if (openOrder.get('buy') && openOrder.get('symbol') === quote.get('symbol')) { + // console.log('order target price'); + // console.log(openOrder.get('targetPrice')); + // console.log('quote price'); + // console.log(quote.get('price')); + // + // if (openOrder.get('targetPrice') >= quote.get('price')) { + // let tradeObject = { + // price: openOrder.get('targetPrice'), + // symbol: openOrder.get('symbol'), + // buy: true, + // } + // + // this.bus.trigger('add_trade', tradeObject) + // console.log(openOrder); + // // destroy purchased open order + // openOrder.destroy(); + // // make sure quote is recognized as bought so price increases appropriately + // quote.buy(); + // } + // } + // else if ((!openOrder.get('buy')) && openOrder.get('symbol') === quote.get('symbol')) { + // console.log('order target price'); + // console.log(openOrder.get('targetPrice')); + // console.log('quote price'); + // console.log(quote.get('price')); + // + // if (openOrder.get('targetPrice') <= quote.get('price')) { + // let tradeObject = { + // price: openOrder.get('targetPrice'), + // symbol: openOrder.get('symbol'), + // buy: false, + // } + // + // this.bus.trigger('add_trade', tradeObject) + // console.log(openOrder); + // // destroy purchased open order + // openOrder.destroy(); + // // make sure quote is recognized as sold so price decreases appropriately + // quote.sell(); + // + // } + // } + // }); + // }); +}, +symbolDropdown(quotes) { + quotes.forEach((quote) => { + // move to model?? + console.log('in symbol dropdown'); + // console.log(quote.get('symbol')); + // this.$('select[name=symbol]').append(``) + this.$('select[name=symbol]').append(``) + // input[name=${field}] + // + }); +}, +events: { + 'click button.btn-buy': 'addBuyOpenOrder', + 'click button.btn-sell': 'addSellOpenOrder', + +}, +updateStatusMessageFrom(messageHash) { + const $formErrors = this.$('.form-errors'); + + $formErrors.empty(); + Object.keys(messageHash).forEach((messageType) => { + messageHash[messageType].forEach((message) => { + $formErrors.append(`
  • ${message}
  • `); }); - }, - addBuyOpenOrder(event) { - event.preventDefault(); - - let formData = this.getFormData(); - console.log(formData); - - formData['buy'] = true; - console.log(formData); - const newOpenOrder = new OpenOrder(formData); - if (newOpenOrder.isValid()) { - this.model.add(newOpenOrder); - this.clearFormData(); - console.log('in add buy open order'); + $formErrors.show(); + }); +}, +addBuyOpenOrder(event) { + event.preventDefault(); + + let formData = this.getFormData(); + console.log(formData); + + formData['buy'] = true; + console.log(formData); + const newOpenOrder = new OpenOrder(formData); + if (newOpenOrder.isValid()) { + this.model.add(newOpenOrder); + this.clearFormData(); + console.log('in add buy open order'); + } else { + console.log('ERROR'); + this.updateStatusMessageFrom(newOpenOrder.validationError); + newOpenOrder.destroy(); + } +}, +addSellOpenOrder(event) { + event.preventDefault(); + + let formData = this.getFormData(); + console.log(formData); + + formData['buy'] = false; + console.log(formData); + const newOpenOrder = new OpenOrder(formData); + // if is valid add newTask and clearformdata + if (newOpenOrder.isValid()) { + this.model.add(newOpenOrder); + this.clearFormData(); + console.log('in add sell open order'); + } else { + console.log('ERROR'); + this.updateStatusMessageFrom(newOpenOrder.validationError); + newOpenOrder.destroy(); + } +}, + +// break out into helper function +getFormData() { + console.log('in get form data'); + const openOrderData = {}; + ['symbol', 'price-target'].forEach((field) => { + let val; + // const val = + // this.$(`.order-entry-form input[name=${field}]`).val(); + console.log(field); + if (field === 'symbol') { + console.log('in if statement if field') + val = + this.$(`.order-entry-form select[name=${field}]`).val(); } else { - console.log('ERROR'); - this.updateStatusMessageFrom(newOpenOrder.validationError); - newOpenOrder.destroy(); + val = + parseFloat(this.$(`.order-entry-form input[name=${field}]`).val()); + field = 'targetPrice'; } - }, - addSellOpenOrder(event) { - event.preventDefault(); - - let formData = this.getFormData(); - console.log(formData); - - formData['buy'] = false; - console.log(formData); - const newOpenOrder = new OpenOrder(formData); - // if is valid add newTask and clearformdata - if (newOpenOrder.isValid()) { - this.model.add(newOpenOrder); - this.clearFormData(); - console.log('in add sell open order'); + console.log(field); + // console.log('button buy or sell') + // console.log(this.$(`.order-entry-form button`).text()) + console.log(val); + // const val = this.$( `.order-entry-form select[name=${field}] option:selected` ).text(); + if (val !== '') { + openOrderData[field] = val; + } + }); + console.log('openOrderData'); + + console.log(openOrderData); + return openOrderData; +}, +clearFormData () { + ['symbol', 'price-target'].forEach((field) => { + if (field === 'symbol') { + this.$(`.order-entry-form select[name=${field}]`).val(''); } else { - console.log('ERROR'); - this.updateStatusMessageFrom(newOpenOrder.validationError); - newOpenOrder.destroy(); + this.$(`.order-entry-form input[name=${field}]`).val(''); } - }, - - // break out into helper function - getFormData() { - console.log('in get form data'); - const openOrderData = {}; - ['symbol', 'price-target'].forEach((field) => { - let val; - // const val = - // this.$(`.order-entry-form input[name=${field}]`).val(); - console.log(field); - if (field === 'symbol') { - console.log('in if statement if field') - val = - this.$(`.order-entry-form select[name=${field}]`).val(); - } else { - val = - parseFloat(this.$(`.order-entry-form input[name=${field}]`).val()); - field = 'targetPrice'; - } - console.log(field); - // console.log('button buy or sell') - // console.log(this.$(`.order-entry-form button`).text()) - console.log(val); - // const val = this.$( `.order-entry-form select[name=${field}] option:selected` ).text(); - if (val !== '') { - openOrderData[field] = val; - } - }); - console.log('openOrderData'); - - console.log(openOrderData); - return openOrderData; - }, - clearFormData () { - ['symbol', 'price-target'].forEach((field) => { - if (field === 'symbol') { - this.$(`.order-entry-form select[name=${field}]`).val(''); - } else { - this.$(`.order-entry-form input[name=${field}]`).val(''); - } - }); - }, + }); +}, }); diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index c0a371f..df22355 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -9,7 +9,7 @@ const QuoteListView = Backbone.View.extend({ this.listenTo(this.model, 'update', this.render); // when quotes change execute currentQuoteList function which will trigger an event on the bus that will pass the current quotelist to this.bus = params.bus; - this.listenTo(this.bus, 'quote_change', this.currentQuoteList); + // this.listenTo(this.bus, 'quote_change', this.currentQuoteList); }, render() { diff --git a/src/views/quote_view.js b/src/views/quote_view.js index 087356c..76e94fe 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -36,8 +36,10 @@ const QuoteView = Backbone.View.extend({ this.model.sell(); }, render() { + let quote = this.model; + this.bus.trigger('quote_change', quote); // trigger quote_change event which quotelist view will listen for - this.bus.trigger('quote_change'); + // this.bus.trigger('quote_change'); console.log(this.model); const compiledTemplate = this.template(this.model.toJSON()); this.$el.html(compiledTemplate); From 6e322b9a37f532f806f43035c6b644f11d1491ce Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Fri, 15 Dec 2017 22:44:33 -0800 Subject: [PATCH 07/17] add validations for target price in open order model --- src/models/open_order.js | 15 +++++++++++++-- src/views/open_order_list_view.js | 26 +++++++++++++++++++------- src/views/quote_list_view.js | 15 +++++++++------ 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/models/open_order.js b/src/models/open_order.js index 96f593d..b53aac0 100644 --- a/src/models/open_order.js +++ b/src/models/open_order.js @@ -5,18 +5,29 @@ const OpenOrder = Backbone.Model.extend({ }, // TODO: validations validate(attributes) { + console.log('the quote'); + console.log(attributes.quote); const errors = {}; if (!attributes.targetPrice) { errors['target_price'] = ["Price cannot be blank."]; console.log('error') + // console.log(attributes.quote); } // TODO: get CurrentMarketPrice from quote // If the target price is blank OR is greater than or equal to the current market price: // That order is not created and an appropriate error message is displayed beneath the form // if buy is true + // TODO: need to get quote.price at that moment in time + if (attributes.buy && attributes.targetPrice >= attributes.quote.get('price')) { + // if (attributes.buy && attributes.targetPrice >= CurrentMarketPrice) { - // errors['target_price'] = ["Price cannot be blank."]; - // } + errors['target_price'] = ["The price you listed for a buy order is greater than or equal to the current quote price."]; + } + if (!attributes.buy && attributes.targetPrice <= attributes.quote.get('price')) { + + // if (attributes.buy && attributes.targetPrice >= CurrentMarketPrice) { + errors['target_price'] = ["The price you listed for a sell order is less than or equal to the current quote price."]; + } // if (!attributes.task_name) { // errors['task_name'] = ["Task name is required"]; // } diff --git a/src/views/open_order_list_view.js b/src/views/open_order_list_view.js index 7b84918..2222231 100644 --- a/src/views/open_order_list_view.js +++ b/src/views/open_order_list_view.js @@ -12,8 +12,19 @@ const OpenOrderListView = Backbone.View.extend({ this.listenTo(this.bus, 'quote_symbols', this.symbolDropdown); this.listenTo(this.bus, 'quote_change', this.checkOpenOrders); + // list for current quote list + this.listenTo(this.bus, 'current_quote_list', this.getQuoteList); + // when the quotelist changes, the checkOpenOrders function will check if orders should be purchased or sold // this.listenTo(this.bus, 'updated_quote_list', this.checkOpenOrders); + // this.listenTo(this.bus, 'updated_quote_list', this.checkOpenOrders); + + + }, + getQuoteList(quoteList) { + this.quoteList = quoteList; + console.log('the quote list !!') + console.log(this.quoteList); }, render() { @@ -29,13 +40,6 @@ const OpenOrderListView = Backbone.View.extend({ }); // render returns the taskview which (this) which allows you to append this.$('#orders').append(openOrderView.render().$el); - // this.$('#orders').append(openOrderView.render().$el); - - // at this point want to start listening for things - // this list view is going to listen to task view for edit me events - // now any time anyone clicks the edit button on a task view it will call - // TODO LISTEN FOR CANCEL BUTTON AND FOR TARGET PRICE - // this.listenTo(taskView, 'edit_me', this.editTask); }); return this; }, @@ -163,6 +167,8 @@ addBuyOpenOrder(event) { console.log(formData); formData['buy'] = true; + let correctQuote = this.quoteList.findWhere({symbol: formData['symbol']}) + formData['quote'] = correctQuote; console.log(formData); const newOpenOrder = new OpenOrder(formData); if (newOpenOrder.isValid()) { @@ -182,6 +188,12 @@ addSellOpenOrder(event) { console.log(formData); formData['buy'] = false; + // .where({symbol: quote.get('symbol')}); + console.log('form data the symbol'); + console.log(formData['symbol']); + let correctQuote = this.quoteList.findWhere({symbol: formData['symbol']}) + formData['quote'] = correctQuote; + console.log('formData after adding the correct quote'); console.log(formData); const newOpenOrder = new OpenOrder(formData); // if is valid add newTask and clearformdata diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index df22355..07c4303 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -30,6 +30,8 @@ const QuoteListView = Backbone.View.extend({ console.log('in quotelistview render after quote models loop'); this.$('#quotes').append(quoteView.render().$el); console.log('after quoteview is appended'); + // pass the quotelist to the open order view + this.bus.trigger('current_quote_list', this.model) }); //TODO: check passing @@ -42,12 +44,13 @@ const QuoteListView = Backbone.View.extend({ return this; }, //currentQuoteList triggers an updated_quote_list event which the openOrderListView will listen for - currentQuoteList() { - console.log('in currentQuoteList'); - console.log(this.model); - let quoteList = this.model - this.bus.trigger('updated_quote_list',quoteList); - }, + // TODO: delete? + // currentQuoteList() { + // console.log('in currentQuoteList'); + // console.log(this.model); + // let quoteList = this.model + // this.bus.trigger('updated_quote_list',quoteList); + // }, }); // const TaskListView = Backbone.View.extend({ // initialize(params) { From 69c732d8f81ea0b9729b04e3d5b3017f50ed0abb Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Fri, 15 Dec 2017 22:46:58 -0800 Subject: [PATCH 08/17] remove extraneous comments and completed todos from open_order model --- src/models/open_order.js | 37 ++++--------------------------------- 1 file changed, 4 insertions(+), 33 deletions(-) diff --git a/src/models/open_order.js b/src/models/open_order.js index b53aac0..3708590 100644 --- a/src/models/open_order.js +++ b/src/models/open_order.js @@ -3,35 +3,21 @@ import Backbone from 'backbone'; const OpenOrder = Backbone.Model.extend({ initialize(attributes) { }, - // TODO: validations validate(attributes) { - console.log('the quote'); - console.log(attributes.quote); const errors = {}; + if (!attributes.targetPrice) { errors['target_price'] = ["Price cannot be blank."]; - console.log('error') - // console.log(attributes.quote); } - // TODO: get CurrentMarketPrice from quote -// If the target price is blank OR is greater than or equal to the current market price: -// That order is not created and an appropriate error message is displayed beneath the form - // if buy is true - // TODO: need to get quote.price at that moment in time - if (attributes.buy && attributes.targetPrice >= attributes.quote.get('price')) { - // if (attributes.buy && attributes.targetPrice >= CurrentMarketPrice) { + if (attributes.buy && attributes.targetPrice >= attributes.quote.get('price')) { errors['target_price'] = ["The price you listed for a buy order is greater than or equal to the current quote price."]; } - if (!attributes.buy && attributes.targetPrice <= attributes.quote.get('price')) { - // if (attributes.buy && attributes.targetPrice >= CurrentMarketPrice) { + if (!attributes.buy && attributes.targetPrice <= attributes.quote.get('price')) { errors['target_price'] = ["The price you listed for a sell order is less than or equal to the current quote price."]; } - // if (!attributes.task_name) { - // errors['task_name'] = ["Task name is required"]; - // } - // + if ( Object.keys(errors).length > 0 ) { return errors; } else { @@ -40,19 +26,4 @@ const OpenOrder = Backbone.Model.extend({ }, }); -// Click Buy in the order entry form: -// If the target price is blank OR is greater than or equal to the current market price: -// That order is not created and an appropriate error message is displayed beneath the form -// If the target price is less than to the current market price: -// A new Buy order is added to the bottom of the open orders list, with the: -// Symbol from the the form -// Target price from the form -// Click Sell in the order entry form: -// If the target price is blank OR is less than or equal to the current market price: -// That order is not created and an appropriate error message is displayed beneath the form -// If the target price is greater than the current market price: -// A new Sell order is added to the bottom of the open orders list, with the: -// Symbol from the the form -// Target price from the form - export default OpenOrder; From 712bd486af3c9e1bb6b31e141dd207f6cb97f3ac Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Sat, 16 Dec 2017 16:52:14 -0800 Subject: [PATCH 09/17] remove console logs from buy and sell functions in quote model --- src/models/quote.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/models/quote.js b/src/models/quote.js index 50f359f..62afb9b 100644 --- a/src/models/quote.js +++ b/src/models/quote.js @@ -7,19 +7,13 @@ const Quote = Backbone.Model.extend({ }, buy() { - console.log('buying quote'); - console.log(this.get('price')); let newPrice = this.get('price') + 1; this.set({price: newPrice}); - console.log(this.get('price')); }, sell() { - console.log('selling quote'); - console.log(this.get('price')); let newPrice = this.get('price') - 1; this.set({price: newPrice}); - console.log(this.get('price')); }, }); From 9ecd6463d37f27baa9958e7c52aeac2311d9571c Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Sat, 16 Dec 2017 17:44:18 -0800 Subject: [PATCH 10/17] OpenOrder model tests for validations --- spec/models/open_order_spec.js | 99 ++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 spec/models/open_order_spec.js diff --git a/spec/models/open_order_spec.js b/spec/models/open_order_spec.js new file mode 100644 index 0000000..5b33675 --- /dev/null +++ b/spec/models/open_order_spec.js @@ -0,0 +1,99 @@ +import OpenOrder from 'models/open_order'; +import Quote from 'models/quote'; + + +describe('OpenOrder spec', () => { + let currentQuote; + let OrderAtQuotePrice; + let OrderAboveQuotePrice; + + beforeEach(() => { + currentQuote = new Quote({ + symbol: 'HUMOR', + price: 85.00, + }) + + OrderAtQuotePrice = new OpenOrder({ + targetPrice: 85.0, + symbol: 'HUMOR', + quote: currentQuote, + buy: true, + }) + + OrderAboveQuotePrice = new OpenOrder({ + targetPrice: 99.0, + symbol: 'HUMOR', + quote: currentQuote, + buy: true, + }) + }); + + describe('validate', () => { + it ('does not allow for price to be blank', () => { + + const invalidOrder = new OpenOrder({ + targetPrice: '', + symbol: 'HUMOR', + quote: currentQuote, + buy: true, + }) + + expect(invalidOrder.isValid()).toBeFalsy(); + }); + + // buy order validations + it ('does not allow for buy orders where target price is equal to the quote', () => { + expect(OrderAtQuotePrice.isValid()).toBeFalsy(); + }); + it ('does not allow for buy orders where target price is greater than the quote', () => { + + // const invalidBuyOrder = new OpenOrder({ + // targetPrice: 99.0, + // symbol: 'HUMOR', + // quote: currentQuote, + // buy: true, + // }) + expect(OrderAboveQuotePrice.isValid()).toBeFalsy(); + }); + it ('allows for buy orders where target price is less than the quote', () => { + + const invalidBuyOrder = new OpenOrder({ + targetPrice: 83.0, + symbol: 'HUMOR', + quote: currentQuote, + buy: true, + }) + expect(invalidBuyOrder.isValid()).toBeTruthy(`error: ${invalidBuyOrder.validationError}`); + }); + + // sell order validations + it ('does not allow for sell orders where target price is equal to the quote', () => { + + OrderAtQuotePrice.set('buy', false); + + expect(OrderAtQuotePrice.isValid()).toBeFalsy(); + }); + + it ('does not allow for sell orders where target price is less than the quote', () => { + const invalidSellOrder = new OpenOrder({ + targetPrice: 83.0, + symbol: 'HUMOR', + quote: currentQuote, + buy: false, + }) + expect(invalidSellOrder.isValid()).toBeFalsy(); + }); + it ('allows for sell orders where target price is greater than the quote', () => { + // const invalidSellOrder = new OpenOrder({ + // targetPrice: 99.0, + // symbol: 'HUMOR', + // quote: currentQuote, + // buy: false, + // }) + OrderAboveQuotePrice.set('buy', false) + + expect(OrderAboveQuotePrice.isValid()).toBeTruthy(`error: ${OrderAboveQuotePrice.validationError}`); + }); + }); + +}); From 9d7699a65aef42feb0403ad83f6d1311556573fb Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Sat, 16 Dec 2017 17:50:53 -0800 Subject: [PATCH 11/17] DRY code with beforeEach in OpenOrder spec --- spec/models/open_order_spec.js | 44 ++++++++++++++-------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/spec/models/open_order_spec.js b/spec/models/open_order_spec.js index 5b33675..a884616 100644 --- a/spec/models/open_order_spec.js +++ b/spec/models/open_order_spec.js @@ -6,6 +6,7 @@ describe('OpenOrder spec', () => { let currentQuote; let OrderAtQuotePrice; let OrderAboveQuotePrice; + let OrderBelowQuotePrice; beforeEach(() => { currentQuote = new Quote({ @@ -26,6 +27,13 @@ describe('OpenOrder spec', () => { quote: currentQuote, buy: true, }) + + OrderBelowQuotePrice = new OpenOrder({ + targetPrice: 83.0, + symbol: 'HUMOR', + quote: currentQuote, + buy: true, + }) }); describe('validate', () => { @@ -43,27 +51,18 @@ describe('OpenOrder spec', () => { // buy order validations it ('does not allow for buy orders where target price is equal to the quote', () => { + expect(OrderAtQuotePrice.isValid()).toBeFalsy(); }); + it ('does not allow for buy orders where target price is greater than the quote', () => { - // const invalidBuyOrder = new OpenOrder({ - // targetPrice: 99.0, - // symbol: 'HUMOR', - // quote: currentQuote, - // buy: true, - // }) expect(OrderAboveQuotePrice.isValid()).toBeFalsy(); }); + it ('allows for buy orders where target price is less than the quote', () => { - const invalidBuyOrder = new OpenOrder({ - targetPrice: 83.0, - symbol: 'HUMOR', - quote: currentQuote, - buy: true, - }) - expect(invalidBuyOrder.isValid()).toBeTruthy(`error: ${invalidBuyOrder.validationError}`); + expect(OrderBelowQuotePrice.isValid()).toBeTruthy(`error: ${OrderBelowQuotePrice.validationError}`); }); // sell order validations @@ -75,21 +74,14 @@ describe('OpenOrder spec', () => { }); it ('does not allow for sell orders where target price is less than the quote', () => { - const invalidSellOrder = new OpenOrder({ - targetPrice: 83.0, - symbol: 'HUMOR', - quote: currentQuote, - buy: false, - }) - expect(invalidSellOrder.isValid()).toBeFalsy(); + + OrderBelowQuotePrice.set('buy', false); + + expect(OrderBelowQuotePrice.isValid()).toBeFalsy(); }); + it ('allows for sell orders where target price is greater than the quote', () => { - // const invalidSellOrder = new OpenOrder({ - // targetPrice: 99.0, - // symbol: 'HUMOR', - // quote: currentQuote, - // buy: false, - // }) + OrderAboveQuotePrice.set('buy', false) expect(OrderAboveQuotePrice.isValid()).toBeTruthy(`error: ${OrderAboveQuotePrice.validationError}`); From 4345a2574088be732485bb74890f505eb644be77 Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Sat, 16 Dec 2017 17:54:03 -0800 Subject: [PATCH 12/17] clean up comments and console logs --- src/app.js | 9 +-------- src/views/quote_view.js | 6 +----- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/src/app.js b/src/app.js index c35a076..58acd78 100644 --- a/src/app.js +++ b/src/app.js @@ -17,9 +17,6 @@ import OpenOrder from './models/open_order'; import OpenOrderView from './views/open_order_view'; import OpenOrderListView from './views/open_order_list_view'; - - - const quoteData = [ { symbol: 'HUMOR', @@ -39,7 +36,6 @@ const quoteData = [ }, ]; -// const quoteList = new QuoteList(); const openOrderList = new OpenOrderList(); let quoteTemplate; @@ -48,7 +44,7 @@ let orderTemplate; $(document).ready(function() { let bus = {}; - // need to import backbone + bus = _.extend(bus, Backbone.Events); quoteTemplate = _.template($('#quote-template').html()); orderTemplate = _.template($('#order-template').html()); @@ -73,7 +69,6 @@ $(document).ready(function() { const tradelistView = new TradeListView({ el: '#trades-container', bus: bus, - // model: quotes, template: tradeTemplate, }); @@ -87,7 +82,5 @@ $(document).ready(function() { quoteListView.render(); - // wave 3 - // any time the stock "changes" change event on model simulator.start(); }); diff --git a/src/views/quote_view.js b/src/views/quote_view.js index 76e94fe..2d347bd 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -4,10 +4,8 @@ import Quote from '../models/quote'; const QuoteView = Backbone.View.extend({ initialize(params) { - // use #quote-template this.template = params.template; this.bus = params.bus; - // any time the stock "changes" change event on model this.listenTo(this.model, 'change', this.render); }, events: { @@ -15,7 +13,6 @@ const QuoteView = Backbone.View.extend({ 'click button.btn-sell': 'sellQuote', }, buyQuote() { - // this.bus.trigger('selected_task', this.model); let tradeObject = { price: this.model.get('price'), @@ -37,9 +34,8 @@ const QuoteView = Backbone.View.extend({ }, render() { let quote = this.model; - this.bus.trigger('quote_change', quote); // trigger quote_change event which quotelist view will listen for - // this.bus.trigger('quote_change'); + this.bus.trigger('quote_change', quote); console.log(this.model); const compiledTemplate = this.template(this.model.toJSON()); this.$el.html(compiledTemplate); From 9939d11b7bd9e857ee7bf40a705e75a5de15dae8 Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Sat, 16 Dec 2017 18:25:42 -0800 Subject: [PATCH 13/17] consolidate buy and sell event handlers into one handler --- src/views/quote_view.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/views/quote_view.js b/src/views/quote_view.js index 2d347bd..a2732e9 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -9,28 +9,28 @@ const QuoteView = Backbone.View.extend({ this.listenTo(this.model, 'change', this.render); }, events: { - 'click button.btn-buy': 'buyQuote', - 'click button.btn-sell': 'sellQuote', + + 'click button.btn-buy': 'buyOrSellQuote', + 'click button.btn-sell': 'buyOrSellQuote', }, - buyQuote() { + buyOrSellQuote(event) { let tradeObject = { price: this.model.get('price'), symbol: this.model.get('symbol'), - buy: true, } - this.bus.trigger('add_trade', tradeObject) - this.model.buy(); - }, - sellQuote() { - let tradeObject = { - price: this.model.get('price'), - symbol: this.model.get('symbol'), - buy: false, + if (event.currentTarget.innerHTML === 'Buy') { + tradeObject['buy'] = true; + console.log(tradeObject); + + this.bus.trigger('add_trade', tradeObject) + this.model.buy(); + } else { + tradeObject['buy'] = false; + this.bus.trigger('add_trade', tradeObject) + this.model.sell(); } - this.bus.trigger('add_trade', tradeObject) - this.model.sell(); }, render() { let quote = this.model; From 609dee4f592c70e7cdc1650ffd59e209d4ea8711 Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Sat, 16 Dec 2017 18:32:31 -0800 Subject: [PATCH 14/17] clean comments in quote list view --- src/views/quote_list_view.js | 46 +----------------------------------- 1 file changed, 1 insertion(+), 45 deletions(-) diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index 07c4303..bc2c9cd 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -7,10 +7,7 @@ const QuoteListView = Backbone.View.extend({ initialize(params) { this.template = params.template; this.listenTo(this.model, 'update', this.render); - // when quotes change execute currentQuoteList function which will trigger an event on the bus that will pass the current quotelist to this.bus = params.bus; - // this.listenTo(this.bus, 'quote_change', this.currentQuoteList); - }, render() { this.$('#quotes').empty(); @@ -21,60 +18,19 @@ const QuoteListView = Backbone.View.extend({ model: quote, template: this.template, tagName: 'li', - // TODO: check for styling className: 'quote', bus: this.bus, }); - // console.log('the quotelist'); - // console.log(this.model); - console.log('in quotelistview render after quote models loop'); this.$('#quotes').append(quoteView.render().$el); - console.log('after quoteview is appended'); // pass the quotelist to the open order view this.bus.trigger('current_quote_list', this.model) }); - //TODO: check passing - // let quotes = this.model; - // console.log(quotes); - console.log('symbols array') - console.log(this.model.symbols()); + // symbols is a custom function in the collection that returns an array of models' symbols let quotes = this.model.symbols(); this.bus.trigger('quote_symbols', quotes); return this; }, - //currentQuoteList triggers an updated_quote_list event which the openOrderListView will listen for - // TODO: delete? - // currentQuoteList() { - // console.log('in currentQuoteList'); - // console.log(this.model); - // let quoteList = this.model - // this.bus.trigger('updated_quote_list',quoteList); - // }, }); -// const TaskListView = Backbone.View.extend({ -// initialize(params) { -// this.template = params.template; -// this.listenTo(this.model, 'update', this.render); -// }, -// render() { -// // Clear the unordered list -// this.$('#todo-items').empty(); -// // Iterate through the list rendering each Task -// this.model.each((task) => { -// // Create a new TaskView with the model & template -// const taskView = new TaskView({ -// model: task, -// template: this.template, -// tagName: 'li', -// className: 'task', -// }); -// // Then render the TaskView -// // And append the resulting HTML to the DOM. -// this.$('#todo-items').append(taskView.render().$el); -// }); -// return this; -// } -// }); export default QuoteListView; From 5cb59cb048a74f4a3e39b51c7bae8b69c8279910 Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Sat, 16 Dec 2017 18:34:41 -0800 Subject: [PATCH 15/17] clean comments in open order and quote list views --- src/views/open_order_view.js | 9 +-------- src/views/quote_list_view.js | 2 +- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/views/open_order_view.js b/src/views/open_order_view.js index 2f32107..715a7d1 100644 --- a/src/views/open_order_view.js +++ b/src/views/open_order_view.js @@ -7,7 +7,7 @@ const OpenOrderView = Backbone.View.extend({ this.bus = params.bus; this.listenTo(this.model, 'change', this.render); }, - + render() { const compiledTemplate = this.template(this.model.toJSON()); this.$el.html(compiledTemplate); @@ -15,16 +15,9 @@ const OpenOrderView = Backbone.View.extend({ }, events: { 'click button.btn-cancel': 'deleteOpenOrder', - // 'click button.toggle-complete': 'toggleComplete', - // 'click button.edit': 'editTask', - // 'click': 'selectTask' }, deleteOpenOrder(event) { - // deletes model, removes from collection, and stops it from listening - // better to tell model to remove itself - // this.remove will tell the view to remove itself from the DOM - // may be unecessary because will re-render this.model.destroy(); this.remove(); }, diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index bc2c9cd..54c06e6 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -26,7 +26,7 @@ const QuoteListView = Backbone.View.extend({ this.bus.trigger('current_quote_list', this.model) }); - // symbols is a custom function in the collection that returns an array of models' symbols + // symbols() is a custom function in the collection that returns an array of models' symbols let quotes = this.model.symbols(); this.bus.trigger('quote_symbols', quotes); return this; From 0c29ff4f3ca97df6f08d2a94a67c09e89063c2af Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Mon, 18 Dec 2017 04:48:50 -0800 Subject: [PATCH 16/17] clean up extraneous comments in open order list view --- src/views/open_order_list_view.js | 96 ++----------------------------- 1 file changed, 4 insertions(+), 92 deletions(-) diff --git a/src/views/open_order_list_view.js b/src/views/open_order_list_view.js index 2222231..91c36f6 100644 --- a/src/views/open_order_list_view.js +++ b/src/views/open_order_list_view.js @@ -6,7 +6,6 @@ import OpenOrder from '../models/open_order'; const OpenOrderListView = Backbone.View.extend({ initialize(params) { this.template = params.template; - // this.listenTo(this.model, 'update', this.render); this.bus = params.bus; this.listenTo(this.model, 'update', this.render); this.listenTo(this.bus, 'quote_symbols', this.symbolDropdown); @@ -14,12 +13,6 @@ const OpenOrderListView = Backbone.View.extend({ // list for current quote list this.listenTo(this.bus, 'current_quote_list', this.getQuoteList); - - // when the quotelist changes, the checkOpenOrders function will check if orders should be purchased or sold - // this.listenTo(this.bus, 'updated_quote_list', this.checkOpenOrders); - // this.listenTo(this.bus, 'updated_quote_list', this.checkOpenOrders); - - }, getQuoteList(quoteList) { this.quoteList = quoteList; @@ -38,19 +31,14 @@ const OpenOrderListView = Backbone.View.extend({ className: 'order', bus: this.bus, }); - // render returns the taskview which (this) which allows you to append + // render returns the taskview (this) which allows you to append this.$('#orders').append(openOrderView.render().$el); }); return this; }, - // checkOpenOrders(quoteList) { checkOpenOrders(quote) { - console.log('in open orders'); - // console.log(quoteList) - // var musketeers = friends.where({job: "Musketeer"}); let openOrders = this.model.where({symbol: quote.get('symbol')}); - console.log(openOrders); if (openOrders.length >= 1) { openOrders.forEach((openOrder) => { // if you are buying @@ -61,7 +49,6 @@ const OpenOrderListView = Backbone.View.extend({ buy: true, } this.bus.trigger('add_trade', tradeObject) - console.log(openOrder); // destroy purchased open order openOrder.destroy(); // make sure quote is recognized as bought so price increases appropriately @@ -73,9 +60,7 @@ const OpenOrderListView = Backbone.View.extend({ symbol: openOrder.get('symbol'), buy: false, } - this.bus.trigger('add_trade', tradeObject) - console.log(openOrder); // destroy purchased open order openOrder.destroy(); // make sure quote is recognized as sold so price decreases appropriately @@ -83,71 +68,15 @@ const OpenOrderListView = Backbone.View.extend({ } }); } - // console.log(openOrders); - // this.model.each((openOrder) => { - // quoteList.each((quote) => { - // // if you are buying - // if (openOrder.get('buy') && openOrder.get('symbol') === quote.get('symbol')) { - // console.log('order target price'); - // console.log(openOrder.get('targetPrice')); - // console.log('quote price'); - // console.log(quote.get('price')); - // - // if (openOrder.get('targetPrice') >= quote.get('price')) { - // let tradeObject = { - // price: openOrder.get('targetPrice'), - // symbol: openOrder.get('symbol'), - // buy: true, - // } - // - // this.bus.trigger('add_trade', tradeObject) - // console.log(openOrder); - // // destroy purchased open order - // openOrder.destroy(); - // // make sure quote is recognized as bought so price increases appropriately - // quote.buy(); - // } - // } - // else if ((!openOrder.get('buy')) && openOrder.get('symbol') === quote.get('symbol')) { - // console.log('order target price'); - // console.log(openOrder.get('targetPrice')); - // console.log('quote price'); - // console.log(quote.get('price')); - // - // if (openOrder.get('targetPrice') <= quote.get('price')) { - // let tradeObject = { - // price: openOrder.get('targetPrice'), - // symbol: openOrder.get('symbol'), - // buy: false, - // } - // - // this.bus.trigger('add_trade', tradeObject) - // console.log(openOrder); - // // destroy purchased open order - // openOrder.destroy(); - // // make sure quote is recognized as sold so price decreases appropriately - // quote.sell(); - // - // } - // } - // }); - // }); }, symbolDropdown(quotes) { quotes.forEach((quote) => { - // move to model?? - console.log('in symbol dropdown'); - // console.log(quote.get('symbol')); - // this.$('select[name=symbol]').append(``) this.$('select[name=symbol]').append(``) - // input[name=${field}] - // }); }, events: { 'click button.btn-buy': 'addBuyOpenOrder', 'click button.btn-sell': 'addSellOpenOrder', - }, updateStatusMessageFrom(messageHash) { const $formErrors = this.$('.form-errors'); @@ -160,16 +89,15 @@ updateStatusMessageFrom(messageHash) { $formErrors.show(); }); }, +// TODO: consolidate addBuyOpenOrder and addSellOpenOrder addBuyOpenOrder(event) { event.preventDefault(); let formData = this.getFormData(); - console.log(formData); formData['buy'] = true; let correctQuote = this.quoteList.findWhere({symbol: formData['symbol']}) formData['quote'] = correctQuote; - console.log(formData); const newOpenOrder = new OpenOrder(formData); if (newOpenOrder.isValid()) { this.model.add(newOpenOrder); @@ -185,16 +113,12 @@ addSellOpenOrder(event) { event.preventDefault(); let formData = this.getFormData(); - console.log(formData); formData['buy'] = false; - // .where({symbol: quote.get('symbol')}); - console.log('form data the symbol'); - console.log(formData['symbol']); + let correctQuote = this.quoteList.findWhere({symbol: formData['symbol']}) formData['quote'] = correctQuote; - console.log('formData after adding the correct quote'); - console.log(formData); + const newOpenOrder = new OpenOrder(formData); // if is valid add newTask and clearformdata if (newOpenOrder.isValid()) { @@ -208,17 +132,12 @@ addSellOpenOrder(event) { } }, -// break out into helper function getFormData() { console.log('in get form data'); const openOrderData = {}; ['symbol', 'price-target'].forEach((field) => { let val; - // const val = - // this.$(`.order-entry-form input[name=${field}]`).val(); - console.log(field); if (field === 'symbol') { - console.log('in if statement if field') val = this.$(`.order-entry-form select[name=${field}]`).val(); } else { @@ -226,18 +145,11 @@ getFormData() { parseFloat(this.$(`.order-entry-form input[name=${field}]`).val()); field = 'targetPrice'; } - console.log(field); - // console.log('button buy or sell') - // console.log(this.$(`.order-entry-form button`).text()) - console.log(val); - // const val = this.$( `.order-entry-form select[name=${field}] option:selected` ).text(); if (val !== '') { openOrderData[field] = val; } }); - console.log('openOrderData'); - console.log(openOrderData); return openOrderData; }, clearFormData () { From 8dbec33916524353d0c3a30f4fed5c261146f237 Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Mon, 18 Dec 2017 05:02:08 -0800 Subject: [PATCH 17/17] fixed a comment --- 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 a2732e9..491e6a5 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -34,7 +34,7 @@ const QuoteView = Backbone.View.extend({ }, render() { let quote = this.model; - // trigger quote_change event which quotelist view will listen for + // trigger quote_change event which openOrderlist view will listen for this.bus.trigger('quote_change', quote); console.log(this.model); const compiledTemplate = this.template(this.model.toJSON());