From b9894acea4cf7a4a97334ea192e78e41b6be1c1a Mon Sep 17 00:00:00 2001 From: kee nam Date: Mon, 11 Dec 2017 17:39:13 -0800 Subject: [PATCH 01/13] Added trade model file and some import dependencies --- src/app.js | 5 +++++ src/models/trade.js | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100644 src/models/trade.js diff --git a/src/app.js b/src/app.js index 03ec910..7629a23 100644 --- a/src/app.js +++ b/src/app.js @@ -2,9 +2,11 @@ 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 Trade from 'models/trade'; const quoteData = [ { @@ -26,6 +28,9 @@ const quoteData = [ ]; $(document).ready(function() { + quoteTemplate = _.template($('#quote-template').html()); + tradeTemplate = _.template($('#trade-template').html()); + const quotes = new QuoteList(quoteData); const simulator = new Simulator({ quotes: quotes, diff --git a/src/models/trade.js b/src/models/trade.js new file mode 100644 index 0000000..8b60b1b --- /dev/null +++ b/src/models/trade.js @@ -0,0 +1,7 @@ +import Backbone from 'backbone'; + +const Trade = Backbone.Model.extend({ + +}); + +export default Trade; From d4c4c43880dd8a97afd77849c97173f975fd15ac Mon Sep 17 00:00:00 2001 From: kee nam Date: Wed, 13 Dec 2017 10:48:38 -0800 Subject: [PATCH 02/13] Added views for quote and quote list --- src/app.js | 30 ++++++++++++++++++++++++++---- src/views/quote_list_view.js | 28 ++++++++++++++++++++++++++++ src/views/quote_view.js | 19 +++++++++++++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) 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 7629a23..725ff3e 100644 --- a/src/app.js +++ b/src/app.js @@ -1,12 +1,20 @@ +// CSS import 'foundation-sites/dist/foundation.css'; import 'css/app.css'; +// Modules import $ from 'jquery'; import _ from 'underscore'; -import Simulator from 'models/simulator'; -import QuoteList from 'collections/quote_list'; -import Trade from 'models/trade'; +// Imports +import Simulator from './models/simulator'; +import QuoteList from './collections/quote_list'; +// import Trade from './models/trade'; + +import QuoteView from './views/quote_view'; +import QuoteListView from './views/quote_list_view'; + +// ------------------------------------------------------- const quoteData = [ { @@ -27,9 +35,15 @@ const quoteData = [ }, ]; +// Define some variables +let quoteTemplate; + +// ------------------------------------------------------- + +// jQuery Ready $(document).ready(function() { quoteTemplate = _.template($('#quote-template').html()); - tradeTemplate = _.template($('#trade-template').html()); + // tradeTemplate = _.template($('#trade-template').html()); const quotes = new QuoteList(quoteData); const simulator = new Simulator({ @@ -37,4 +51,12 @@ $(document).ready(function() { }); simulator.start(); + + const quoteListView = new QuoteListView({ + el: 'main', + model: quotes, + template: quoteTemplate, + }); + + quoteListView.render(); }); diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js new file mode 100644 index 0000000..eb6af6e --- /dev/null +++ b/src/views/quote_list_view.js @@ -0,0 +1,28 @@ +import Backbone from 'backbone'; +import QuoteView from './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(); + + this.model.each((quote) => { + const quoteView = new QuoteView({ + model: quote, + template: this.template, + tagName: 'li', + className: 'quote', + }); + 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..b3da4c6 --- /dev/null +++ b/src/views/quote_view.js @@ -0,0 +1,19 @@ +import Backbone from 'backbone'; +import Quote from '../models/quote'; + +const QuoteView = 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; + }, + +}); + +export default QuoteView; From c78b267c56617d2af2862f4807795e610737bfa2 Mon Sep 17 00:00:00 2001 From: kee nam Date: Wed, 13 Dec 2017 11:03:15 -0800 Subject: [PATCH 03/13] Added buy and sell events to the quote model and view --- src/models/quote.js | 6 ++++++ src/views/quote_view.js | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/models/quote.js b/src/models/quote.js index 4fbf466..a5bf5d2 100644 --- a/src/models/quote.js +++ b/src/models/quote.js @@ -8,10 +8,16 @@ const Quote = Backbone.Model.extend({ buy() { // Implement this function to increase the price by $1.00 + let current_price = this.get('price'); + console.log(current_price); + this.set('price', current_price += 1); }, sell() { // Implement this function to decrease the price by $1.00 + let current_price = this.get('price'); + console.log(current_price); + this.set('price', current_price -= 1); }, }); diff --git a/src/views/quote_view.js b/src/views/quote_view.js index b3da4c6..1bae87e 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -13,7 +13,20 @@ const QuoteView = Backbone.View.extend({ return this; }, - + + events: { + 'click button.btn-buy': 'buy', + 'click button.btn-sell': 'sell', + }, + + buy(event) { + this.model.buy(); + }, + + sell(event) { + this.model.sell(); + } + }); export default QuoteView; From 98ba06a716a82b5d141d8af48020d765c77648c8 Mon Sep 17 00:00:00 2001 From: kee nam Date: Fri, 15 Dec 2017 10:12:42 -0800 Subject: [PATCH 04/13] Added function to append select option choices --- src/app.js | 11 +++++++++++ src/views/trade_view.js | 8 ++++++++ 2 files changed, 19 insertions(+) create mode 100644 src/views/trade_view.js diff --git a/src/app.js b/src/app.js index 725ff3e..40d5b0c 100644 --- a/src/app.js +++ b/src/app.js @@ -16,6 +16,7 @@ import QuoteListView from './views/quote_list_view'; // ------------------------------------------------------- +// Given quotes const quoteData = [ { symbol: 'HUMOR', @@ -35,6 +36,10 @@ const quoteData = [ }, ]; +// Got quote names & prices into separate arrays +const names = quoteData.map(info => info.symbol); +const prices = quoteData.map(info => info.price); + // Define some variables let quoteTemplate; @@ -50,6 +55,12 @@ $(document).ready(function() { quotes: quotes, }); + // Populate the form with options and prices + let selectBox = $('.order-entry-form form select[name="symbol"]'); + for (name of names) { + selectBox.append(``); + } + simulator.start(); const quoteListView = new QuoteListView({ diff --git a/src/views/trade_view.js b/src/views/trade_view.js new file mode 100644 index 0000000..4d030ab --- /dev/null +++ b/src/views/trade_view.js @@ -0,0 +1,8 @@ +import Backbone from 'backbone'; +import Trade from '../models/trade'; + +const TradeView = Backbone.View.extend({ + +}); + +export default TradeView; From 6d32d2b8439c2931cac7d72c9d68be6033c96e21 Mon Sep 17 00:00:00 2001 From: kee nam Date: Fri, 15 Dec 2017 14:47:46 -0800 Subject: [PATCH 05/13] Working on event bus for trade history --- src/app.js | 11 ++++++++++- src/models/quote.js | 4 ++++ src/views/quote_list_view.js | 1 - src/views/quote_view.js | 7 ++++--- src/views/trade_view.js | 7 ++++++- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/app.js b/src/app.js index 40d5b0c..b88365f 100644 --- a/src/app.js +++ b/src/app.js @@ -9,10 +9,11 @@ import _ from 'underscore'; // Imports import Simulator from './models/simulator'; import QuoteList from './collections/quote_list'; -// import Trade from './models/trade'; +import Trade from './models/trade'; import QuoteView from './views/quote_view'; import QuoteListView from './views/quote_list_view'; +import TradeView from './views/trade_view'; // ------------------------------------------------------- @@ -47,6 +48,14 @@ let quoteTemplate; // jQuery Ready $(document).ready(function() { + + // EVENT BUS + // listen to buy and sell events in quote.js + // append #trade-templates for each; parse datas + // let bus = {}; + // bus = _.extend(bus, Backbone.Events); + // const tradeView = new TradeView({bus: bus}); + quoteTemplate = _.template($('#quote-template').html()); // tradeTemplate = _.template($('#trade-template').html()); diff --git a/src/models/quote.js b/src/models/quote.js index a5bf5d2..37cac16 100644 --- a/src/models/quote.js +++ b/src/models/quote.js @@ -11,6 +11,8 @@ const Quote = Backbone.Model.extend({ let current_price = this.get('price'); console.log(current_price); this.set('price', current_price += 1); + let current_symbol = this.get('symbol'); + return { symbol: current_symbol, price: current_price, buy: 'bought'}; }, sell() { @@ -18,6 +20,8 @@ const Quote = Backbone.Model.extend({ let current_price = this.get('price'); console.log(current_price); this.set('price', current_price -= 1); + let current_symbol = this.get('symbol'); + return { symbol: current_symbol, price: current_price, buy: 'sold'}; }, }); diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index eb6af6e..3366e62 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -7,7 +7,6 @@ const QuoteListView = Backbone.View.extend({ this.template = params.template; this.listenTo(this.model, 'update', this.render); }, - render() { this.$('#quotes').empty(); diff --git a/src/views/quote_view.js b/src/views/quote_view.js index 1bae87e..fed3788 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -20,13 +20,14 @@ const QuoteView = Backbone.View.extend({ }, buy(event) { - this.model.buy(); + let tradeData = this.model.buy(); + console.log(tradeData); }, sell(event) { - this.model.sell(); + let tradeData = this.model.sell(); + console.log(tradeData); } - }); export default QuoteView; diff --git a/src/views/trade_view.js b/src/views/trade_view.js index 4d030ab..e7754f1 100644 --- a/src/views/trade_view.js +++ b/src/views/trade_view.js @@ -2,7 +2,12 @@ import Backbone from 'backbone'; import Trade from '../models/trade'; const TradeView = Backbone.View.extend({ - + initialize(params) { + this.template = params.template; + }, + + render() { + }, }); export default TradeView; From c99265e43d74e07603a4b370b5361bf765847c17 Mon Sep 17 00:00:00 2001 From: kee nam Date: Sun, 17 Dec 2017 01:24:35 -0800 Subject: [PATCH 06/13] Implemented trade history event bus --- dist/index.html | 2 +- src/app.js | 19 ++++++++++++++----- src/models/quote.js | 4 ++-- src/views/quote_list_view.js | 3 +++ src/views/quote_view.js | 9 +++++++++ src/views/trade_view.js | 9 ++++++++- 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/dist/index.html b/dist/index.html index 8a046fa..d5e8528 100644 --- a/dist/index.html +++ b/dist/index.html @@ -100,7 +100,7 @@

$<%- price.toFixed(2) %>

diff --git a/src/app.js b/src/app.js index b88365f..06df59c 100644 --- a/src/app.js +++ b/src/app.js @@ -43,6 +43,7 @@ const prices = quoteData.map(info => info.price); // Define some variables let quoteTemplate; +let tradeTemplate; // ------------------------------------------------------- @@ -52,12 +53,11 @@ $(document).ready(function() { // EVENT BUS // listen to buy and sell events in quote.js // append #trade-templates for each; parse datas - // let bus = {}; - // bus = _.extend(bus, Backbone.Events); - // const tradeView = new TradeView({bus: bus}); + let bus = {}; + bus = _.extend(bus, Backbone.Events); quoteTemplate = _.template($('#quote-template').html()); - // tradeTemplate = _.template($('#trade-template').html()); + tradeTemplate = _.template($('#trade-template').html()); const quotes = new QuoteList(quoteData); const simulator = new Simulator({ @@ -72,11 +72,20 @@ $(document).ready(function() { simulator.start(); + // Render quote list view const quoteListView = new QuoteListView({ - el: 'main', + el: $('#quotes-container'), model: quotes, template: quoteTemplate, + bus: bus, }); quoteListView.render(); + + // Render trade history view + const tradeView = new TradeView({ + template: tradeTemplate, + el: $('#trades-container'), + bus: bus, + }) }); diff --git a/src/models/quote.js b/src/models/quote.js index 37cac16..c26e8eb 100644 --- a/src/models/quote.js +++ b/src/models/quote.js @@ -12,7 +12,7 @@ const Quote = Backbone.Model.extend({ console.log(current_price); this.set('price', current_price += 1); let current_symbol = this.get('symbol'); - return { symbol: current_symbol, price: current_price, buy: 'bought'}; + return { symbol: current_symbol, price: current_price, buy: 'bought' }; }, sell() { @@ -21,7 +21,7 @@ const Quote = Backbone.Model.extend({ console.log(current_price); this.set('price', current_price -= 1); let current_symbol = this.get('symbol'); - return { symbol: current_symbol, price: current_price, buy: 'sold'}; + return { symbol: current_symbol, price: current_price, buy: 'sold' }; }, }); diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index 3366e62..7c10222 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -5,6 +5,7 @@ import Quote from '../models/quote'; const QuoteListView = Backbone.View.extend({ initialize(params) { this.template = params.template; + this.bus = params.bus; this.listenTo(this.model, 'update', this.render); }, render() { @@ -16,8 +17,10 @@ const QuoteListView = Backbone.View.extend({ template: this.template, tagName: 'li', className: 'quote', + bus: this.bus, }); this.$('#quotes').append(quoteView.render().$el); + // console.log(quoteView); }); return this; diff --git a/src/views/quote_view.js b/src/views/quote_view.js index fed3788..69ff392 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -4,7 +4,14 @@ import Quote from '../models/quote'; const QuoteView = Backbone.View.extend({ initialize(params) { this.template = params.template; + this.bus = params.bus; this.listenTo(this.model, 'change', this.render); + this.listenTo(this.bus, 'buy', (model) => { + if (model) { + this.model = model; + this.render(); + } + }); }, render() { @@ -22,11 +29,13 @@ const QuoteView = Backbone.View.extend({ buy(event) { let tradeData = this.model.buy(); console.log(tradeData); + this.bus.trigger('boughtOrSold', tradeData); }, sell(event) { let tradeData = this.model.sell(); console.log(tradeData); + this.bus.trigger('boughtOrSold', tradeData); } }); diff --git a/src/views/trade_view.js b/src/views/trade_view.js index e7754f1..99a7bfc 100644 --- a/src/views/trade_view.js +++ b/src/views/trade_view.js @@ -3,9 +3,16 @@ import Trade from '../models/trade'; const TradeView = Backbone.View.extend({ initialize(params) { + this.bus = params.bus; this.template = params.template; + + this.listenTo(this.bus, 'boughtOrSold', this.addTrade); + }, + addTrade(tradeData) { + const compiledTemplate = this.template(tradeData); + this.$('#trades').prepend(compiledTemplate); }, - + render() { }, }); From 69c5022f3545f8deff989fbdc46f24ba8da4b2e6 Mon Sep 17 00:00:00 2001 From: kee nam Date: Sun, 17 Dec 2017 19:26:59 -0800 Subject: [PATCH 07/13] DRY-ed up code --- src/app.js | 7 +++---- src/models/trade.js | 7 ------- src/views/trade_view.js | 10 +++------- 3 files changed, 6 insertions(+), 18 deletions(-) delete mode 100644 src/models/trade.js diff --git a/src/app.js b/src/app.js index 06df59c..c569c8c 100644 --- a/src/app.js +++ b/src/app.js @@ -9,7 +9,6 @@ import _ from 'underscore'; // Imports import Simulator from './models/simulator'; import QuoteList from './collections/quote_list'; -import Trade from './models/trade'; import QuoteView from './views/quote_view'; import QuoteListView from './views/quote_list_view'; @@ -50,15 +49,15 @@ let tradeTemplate; // jQuery Ready $(document).ready(function() { - // EVENT BUS - // listen to buy and sell events in quote.js - // append #trade-templates for each; parse datas + // Event Bus let bus = {}; bus = _.extend(bus, Backbone.Events); + // Templates quoteTemplate = _.template($('#quote-template').html()); tradeTemplate = _.template($('#trade-template').html()); + // Default - simulator const quotes = new QuoteList(quoteData); const simulator = new Simulator({ quotes: quotes, diff --git a/src/models/trade.js b/src/models/trade.js deleted file mode 100644 index 8b60b1b..0000000 --- a/src/models/trade.js +++ /dev/null @@ -1,7 +0,0 @@ -import Backbone from 'backbone'; - -const Trade = Backbone.Model.extend({ - -}); - -export default Trade; diff --git a/src/views/trade_view.js b/src/views/trade_view.js index 99a7bfc..908299f 100644 --- a/src/views/trade_view.js +++ b/src/views/trade_view.js @@ -1,20 +1,16 @@ import Backbone from 'backbone'; -import Trade from '../models/trade'; +// import Trade from '../models/trade'; const TradeView = Backbone.View.extend({ initialize(params) { this.bus = params.bus; this.template = params.template; - - this.listenTo(this.bus, 'boughtOrSold', this.addTrade); + this.listenTo(this.bus, 'boughtOrSold', this.render); }, - addTrade(tradeData) { + render(tradeData) { const compiledTemplate = this.template(tradeData); this.$('#trades').prepend(compiledTemplate); }, - - render() { - }, }); export default TradeView; From b53a938c14227a32c6669912f7b9623f20858d7d Mon Sep 17 00:00:00 2001 From: kee nam Date: Sun, 17 Dec 2017 19:40:16 -0800 Subject: [PATCH 08/13] Added empty order and order list models and views --- src/collections/order_list.js | 8 ++++++++ src/models/order.js | 7 +++++++ src/views/order_list_view.js | 11 +++++++++++ src/views/order_view.js | 10 ++++++++++ 4 files changed, 36 insertions(+) create mode 100644 src/collections/order_list.js 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/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; diff --git a/src/models/order.js b/src/models/order.js new file mode 100644 index 0000000..ac00259 --- /dev/null +++ b/src/models/order.js @@ -0,0 +1,7 @@ +import Backbone from 'backbone'; + +const Order = Backbone.Model.extend({ + +}); + +export default Order; diff --git a/src/views/order_list_view.js b/src/views/order_list_view.js new file mode 100644 index 0000000..be37b14 --- /dev/null +++ b/src/views/order_list_view.js @@ -0,0 +1,11 @@ +import Backbone from 'backbone'; +import OrderView from './order_view'; +import Order from '../models/order'; + +const OrderListView = Backbone.View.extend({ + initialize(params){}, + render(){}, + events: {}, +}); + +export default OrderListView; diff --git a/src/views/order_view.js b/src/views/order_view.js new file mode 100644 index 0000000..5712c5a --- /dev/null +++ b/src/views/order_view.js @@ -0,0 +1,10 @@ +import Backbone from 'backbone'; +import Order from '../models/order'; + +const OrderView = Backbone.View.extend({ + initialize(params){}, + render(){}, + events: {}, +}); + +export default OrderView; From c526323fe3aa3ba75c6c7f60033ae998f044712d Mon Sep 17 00:00:00 2001 From: kee nam Date: Mon, 18 Dec 2017 03:15:37 -0800 Subject: [PATCH 09/13] Can add to and cancel orders --- dist/index.html | 2 +- src/app.js | 27 ++++++++++++++---- src/views/order_list_view.js | 54 ++++++++++++++++++++++++++++++++++-- src/views/order_view.js | 21 ++++++++++++-- 4 files changed, 91 insertions(+), 13 deletions(-) diff --git a/dist/index.html b/dist/index.html index d5e8528..37d8124 100644 --- a/dist/index.html +++ b/dist/index.html @@ -108,7 +108,7 @@

$<%- price.toFixed(2) %>