From 47ab34af888278626c1e323e93ac0fd7c37e4afe Mon Sep 17 00:00:00 2001 From: Sara C Date: Fri, 15 Dec 2017 12:53:51 -0800 Subject: [PATCH 01/13] added quote list and quote views --- src/app.js | 2 ++ src/views/quote_list_view.js | 0 src/views/quote_view.js | 9 +++++++++ 3 files changed, 11 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..27a1007 100644 --- a/src/app.js +++ b/src/app.js @@ -32,4 +32,6 @@ $(document).ready(function() { }); simulator.start(); + + const quoteTemp = _.template($('#quote-template').html()); }); diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js new file mode 100644 index 0000000..e69de29 diff --git a/src/views/quote_view.js b/src/views/quote_view.js new file mode 100644 index 0000000..dffec01 --- /dev/null +++ b/src/views/quote_view.js @@ -0,0 +1,9 @@ +import Backbone from 'backbone'; + +const QuoteView = Backbone.View.extend ({ +initialize(params) { + this.template = params.template; +}, +}); + +export default QuoteView; From ef92b3c83033ba3108cc83a98fd7947ac8d6cb0b Mon Sep 17 00:00:00 2001 From: Sara C Date: Fri, 15 Dec 2017 15:20:43 -0800 Subject: [PATCH 02/13] filling out views for quote --- src/app.js | 10 ++++++++++ src/views/quote_list_view.js | 22 ++++++++++++++++++++++ src/views/quote_view.js | 21 ++++++++++++++------- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/app.js b/src/app.js index 27a1007..5f18731 100644 --- a/src/app.js +++ b/src/app.js @@ -5,6 +5,7 @@ import $ from 'jquery'; import Simulator from 'models/simulator'; import QuoteList from 'collections/quote_list'; +import QuoteListView from 'views/quote_list_view'; const quoteData = [ { @@ -26,11 +27,20 @@ const quoteData = [ ]; $(document).ready(function() { + const quotes = new QuoteList(quoteData); const simulator = new Simulator({ quotes: quotes, }); + //create new quote view obj + const quoteListView = new QuoteListView({ + model: quotes + template: quoteTemp, + tagName: 'li', + className: 'quote', + }); + simulator.start(); const quoteTemp = _.template($('#quote-template').html()); diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index e69de29..4ca7a95 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -0,0 +1,22 @@ +import Backbone from 'backbone'; +import _ from 'underscore'; +import Quote from 'models/quote'; + +const QuoteListView = Backbone.View.extend ({ +initialize(params) { + this.template = params.template; +}, +render() { + //use the template + const quoteHTML = this.template({ + symbol: this.model.get('symbol'), + price: this.model.get('price'), + }); + + //append template to html + this.$ + return this; +}, +}); + +export default QuoteListView; diff --git a/src/views/quote_view.js b/src/views/quote_view.js index dffec01..d9ade88 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -1,9 +1,16 @@ import Backbone from 'backbone'; +import Quote from 'models/quote'; -const QuoteView = Backbone.View.extend ({ -initialize(params) { - this.template = params.template; -}, -}); - -export default QuoteView; +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; + }, + //events + }); From dbb5441d4fc84cfea89b46de4c3ce7e920d09e50 Mon Sep 17 00:00:00 2001 From: Sara C Date: Fri, 15 Dec 2017 16:19:39 -0800 Subject: [PATCH 03/13] quotes view working --- src/app.js | 12 +++++++----- src/views/quote_list_view.js | 20 ++++++++++++++------ src/views/quote_view.js | 6 ++++-- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/app.js b/src/app.js index 5f18731..9115fa0 100644 --- a/src/app.js +++ b/src/app.js @@ -2,6 +2,7 @@ import 'foundation-sites/dist/foundation.css'; import 'css/app.css'; import $ from 'jquery'; +import _ from 'underscore'; import Simulator from 'models/simulator'; import QuoteList from 'collections/quote_list'; @@ -32,16 +33,17 @@ $(document).ready(function() { const simulator = new Simulator({ quotes: quotes, }); + const quoteTemp = _.template($('#quote-template').html()); //create new quote view obj const quoteListView = new QuoteListView({ - model: quotes - template: quoteTemp, - tagName: 'li', - className: 'quote', + model: quotes, + quoteTemplate: quoteTemp, + el: 'main', }); + quoteListView.render(); + simulator.start(); - const quoteTemp = _.template($('#quote-template').html()); }); diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index 4ca7a95..9fed336 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -1,20 +1,28 @@ import Backbone from 'backbone'; import _ from 'underscore'; -import Quote from 'models/quote'; +import Quote from '../models/quote'; +import QuoteView from '../views/quote_view'; const QuoteListView = Backbone.View.extend ({ initialize(params) { - this.template = params.template; + this.quoteTemplate = params.quoteTemplate; + this.listenTo(this.model, 'update', this.render); }, render() { + this.$('#quotes').empty(); //use the template - const quoteHTML = this.template({ - symbol: this.model.get('symbol'), - price: this.model.get('price'), + this.model.each((quote) => { + const quoteView = new QuoteView({ + model: quote, + template: this.quoteTemplate, + tagName: 'li', + className: 'quote', + }); + this.$('#quotes').append(quoteView.render().$el); }); + //append template to html - this.$ return this; }, }); diff --git a/src/views/quote_view.js b/src/views/quote_view.js index d9ade88..02e2e94 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -1,5 +1,5 @@ import Backbone from 'backbone'; -import Quote from 'models/quote'; +import Quote from '../models/quote'; const QuoteView = Backbone.View.extend({ initialize(params) { @@ -9,8 +9,10 @@ const QuoteView = Backbone.View.extend({ render() { const compiledTemplate = this.template(this.model.toJSON()); this.$el.html(compiledTemplate); - + return this; }, //events }); + +export default QuoteView; From 8e3f5c811d5e54a54d90af336ac2957507f8aadc Mon Sep 17 00:00:00 2001 From: Sara C Date: Sun, 17 Dec 2017 21:40:13 -0800 Subject: [PATCH 04/13] buy and sell button will call correct function --- src/app.js | 3 +++ src/models/quote.js | 2 ++ src/views/quote_list_view.js | 1 + src/views/quote_view.js | 13 ++++++++++++- 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index 9115fa0..cfd8119 100644 --- a/src/app.js +++ b/src/app.js @@ -28,6 +28,8 @@ const quoteData = [ ]; $(document).ready(function() { + let bus = {}; + bus = _.extend(bus, Backbone.Events); const quotes = new QuoteList(quoteData); const simulator = new Simulator({ @@ -40,6 +42,7 @@ $(document).ready(function() { model: quotes, quoteTemplate: quoteTemp, el: 'main', + bus: bus, }); quoteListView.render(); diff --git a/src/models/quote.js b/src/models/quote.js index 4fbf466..543135c 100644 --- a/src/models/quote.js +++ b/src/models/quote.js @@ -8,10 +8,12 @@ const Quote = Backbone.Model.extend({ buy() { // Implement this function to increase the price by $1.00 + this.set('price', this.get('price') + 1.00 ) }, sell() { // Implement this function to decrease the price by $1.00 + this.set('price', this.get('price') - 1.00 ) }, }); diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index 9fed336..b1be7a2 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.quoteTemplate = params.quoteTemplate; this.listenTo(this.model, 'update', this.render); + this.bus = params.bus; }, render() { this.$('#quotes').empty(); diff --git a/src/views/quote_view.js b/src/views/quote_view.js index 02e2e94..8399455 100644 --- a/src/views/quote_view.js +++ b/src/views/quote_view.js @@ -4,6 +4,7 @@ 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); }, render() { @@ -12,7 +13,17 @@ const QuoteView = Backbone.View.extend({ return this; }, - //events + events: { + 'click button.btn-buy': 'buyTrade', + 'click button.btn-sell': 'sellTrade', + }, + + buyTrade: function(event) { + this.model.buy(); + }, + sellTrade: function(event) { + this.model.sell(); + }, }); export default QuoteView; From 0745a79f8a6f1610fc4b1a961d6a60b485f263d7 Mon Sep 17 00:00:00 2001 From: Sara C Date: Sun, 17 Dec 2017 21:58:33 -0800 Subject: [PATCH 05/13] added trade view --- src/views/trade_view.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/views/trade_view.js diff --git a/src/views/trade_view.js b/src/views/trade_view.js new file mode 100644 index 0000000..a4ae0df --- /dev/null +++ b/src/views/trade_view.js @@ -0,0 +1,18 @@ +import Backbone from 'backbone'; +import Quote from '../models/quote'; + +const TradeView = Backbone.View.extend ({ + initialize(params) { + this.template = params.template; + }, + + render() { + const compiledTemplate = this.template(this.model.toJSON()); + this.$el.html(compiledTemplate); + + return this; + }, +}); + + +export default TradeView; From 3224f53c7fc32a47a02754b704eb3fa825ec7015 Mon Sep 17 00:00:00 2001 From: Sara C Date: Sun, 17 Dec 2017 22:45:47 -0800 Subject: [PATCH 06/13] decided to use quote to implement trade view --- src/app.js | 2 ++ src/models/quote.js | 12 ++++++++++++ src/views/quote_list_view.js | 9 +++++++++ src/views/trade_view.js | 36 ++++++++++++++++++------------------ 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/app.js b/src/app.js index cfd8119..97f63de 100644 --- a/src/app.js +++ b/src/app.js @@ -36,11 +36,13 @@ $(document).ready(function() { quotes: quotes, }); const quoteTemp = _.template($('#quote-template').html()); + const tradeTemp = _.template($('#trade-template').html()); //create new quote view obj const quoteListView = new QuoteListView({ model: quotes, quoteTemplate: quoteTemp, + tradeTemplate: tradeTemp, el: 'main', bus: bus, }); diff --git a/src/models/quote.js b/src/models/quote.js index 543135c..b06f361 100644 --- a/src/models/quote.js +++ b/src/models/quote.js @@ -8,11 +8,23 @@ const Quote = Backbone.Model.extend({ buy() { // Implement this function to increase the price by $1.00 + const data = { + buy: true, + symbol: this.get('symbol'), + price: this.get('price') + } + this.trigger('addQuote', data); this.set('price', this.get('price') + 1.00 ) }, sell() { // Implement this function to decrease the price by $1.00 + const data = { + buy: false, + symbol: this.get('symbol'), + price: this.get('price') + } + this.trigger('addQuote', data); this.set('price', this.get('price') - 1.00 ) }, }); diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index b1be7a2..f3d6e2c 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -6,6 +6,7 @@ import QuoteView from '../views/quote_view'; const QuoteListView = Backbone.View.extend ({ initialize(params) { this.quoteTemplate = params.quoteTemplate; + this.tradeTemplate = params.tradeTemplate; this.listenTo(this.model, 'update', this.render); this.bus = params.bus; }, @@ -19,6 +20,7 @@ render() { tagName: 'li', className: 'quote', }); + this.listenTo(quote, 'addQuote', this.events.seeTrade); this.$('#quotes').append(quoteView.render().$el); }); @@ -26,6 +28,13 @@ render() { //append template to html return this; }, + +events: { + seeTrade: function(data) { + const seeTradeTemp = this.tradeTemplate(data); + this.$('#trades').prepend(seeTradeTemp); + } +} }); export default QuoteListView; diff --git a/src/views/trade_view.js b/src/views/trade_view.js index a4ae0df..c291e97 100644 --- a/src/views/trade_view.js +++ b/src/views/trade_view.js @@ -1,18 +1,18 @@ -import Backbone from 'backbone'; -import Quote from '../models/quote'; - -const TradeView = Backbone.View.extend ({ - initialize(params) { - this.template = params.template; - }, - - render() { - const compiledTemplate = this.template(this.model.toJSON()); - this.$el.html(compiledTemplate); - - return this; - }, -}); - - -export default TradeView; +// import Backbone from 'backbone'; +// import Quote from '../models/quote'; +// +// const TradeView = Backbone.View.extend ({ +// initialize(params) { +// this.template = params.template; +// }, +// +// render() { +// const compiledTemplate = this.template(this.model.toJSON()); +// this.$el.html(compiledTemplate); +// +// return this; +// }, +// }); +// +// +// export default TradeView; From 668068375974d633851e8a19335dd327300a87ea Mon Sep 17 00:00:00 2001 From: Sara C Date: Sun, 17 Dec 2017 23:35:09 -0800 Subject: [PATCH 07/13] added validations to order model --- src/models/order.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/models/order.js diff --git a/src/models/order.js b/src/models/order.js new file mode 100644 index 0000000..52fd106 --- /dev/null +++ b/src/models/order.js @@ -0,0 +1,31 @@ +import Backbone from 'backbone'; + + +const Order = Backbone.model.extend ({ + initialize(attributes) { + + }, + validate(attributes) { + const errors = {}; + if (!attributes.symbol) { + errors['symbol'] = ["You must specify a symbol"]; + } + if (!attributes.price) { + errors['price'] = ["You must specify price."]; + } + if (this.get('buy') && attributes.price >= this.get('quote').get('price')) { + errors['price'] = ["The buy price needs to be less than the current price."] + } + if (this.get('buy') && attributes.price <= this.get('quote').get('price')) { + errors['price'] = ["The sell price needs to be greater than the current price."] + } + if ( Object.keys(errors).length > 0 ) { + return errors; + } else { + return false; + } + }, +}); + + +export default Order; From a543080d960954de4658ccc97de49716c0650317 Mon Sep 17 00:00:00 2001 From: Sara C Date: Sun, 17 Dec 2017 23:37:58 -0800 Subject: [PATCH 08/13] added order collection --- src/collections/order_list.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/collections/order_list.js diff --git a/src/collections/order_list.js b/src/collections/order_list.js new file mode 100644 index 0000000..b224ecd --- /dev/null +++ b/src/collections/order_list.js @@ -0,0 +1,8 @@ +import Backbone from 'backbone'; +import Order from 'models/order'; + +const OrderList = Backbone.collection.extend({ + model: Order, +}); + +export default OrderList; From e3d7459a0515e19458690dd6c125351ce5fe38e1 Mon Sep 17 00:00:00 2001 From: Sara C Date: Mon, 18 Dec 2017 00:05:12 -0800 Subject: [PATCH 09/13] adding order view --- src/app.js | 2 ++ src/collections/order_list.js | 2 +- src/models/order.js | 6 +++--- src/views/order_view.js | 0 4 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 src/views/order_view.js diff --git a/src/app.js b/src/app.js index 97f63de..bcfb4df 100644 --- a/src/app.js +++ b/src/app.js @@ -7,6 +7,7 @@ import _ from 'underscore'; import Simulator from 'models/simulator'; import QuoteList from 'collections/quote_list'; import QuoteListView from 'views/quote_list_view'; +import OrderList from 'collections/order_list'; const quoteData = [ { @@ -32,6 +33,7 @@ $(document).ready(function() { bus = _.extend(bus, Backbone.Events); const quotes = new QuoteList(quoteData); + const orders = new OrderList(); const simulator = new Simulator({ quotes: quotes, }); diff --git a/src/collections/order_list.js b/src/collections/order_list.js index b224ecd..6d9ed9f 100644 --- a/src/collections/order_list.js +++ b/src/collections/order_list.js @@ -1,5 +1,5 @@ import Backbone from 'backbone'; -import Order from 'models/order'; +import Order from '../models/order'; const OrderList = Backbone.collection.extend({ model: Order, diff --git a/src/models/order.js b/src/models/order.js index 52fd106..929b186 100644 --- a/src/models/order.js +++ b/src/models/order.js @@ -10,13 +10,13 @@ const Order = Backbone.model.extend ({ if (!attributes.symbol) { errors['symbol'] = ["You must specify a symbol"]; } - if (!attributes.price) { + if (!attributes.setPrice) { errors['price'] = ["You must specify price."]; } - if (this.get('buy') && attributes.price >= this.get('quote').get('price')) { + if (this.get('buy') && attributes.setPrice >= this.get('quote').get('price')) { errors['price'] = ["The buy price needs to be less than the current price."] } - if (this.get('buy') && attributes.price <= this.get('quote').get('price')) { + if (this.get('buy') && attributes.setPrice <= this.get('quote').get('price')) { errors['price'] = ["The sell price needs to be greater than the current price."] } if ( Object.keys(errors).length > 0 ) { diff --git a/src/views/order_view.js b/src/views/order_view.js new file mode 100644 index 0000000..e69de29 From 17330350f369ef5b2eaca369bca058e25d7e3ae1 Mon Sep 17 00:00:00 2001 From: Sara C Date: Mon, 18 Dec 2017 00:21:55 -0800 Subject: [PATCH 10/13] working on order view --- src/views/order_view.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/views/order_view.js b/src/views/order_view.js index e69de29..93991c3 100644 --- a/src/views/order_view.js +++ b/src/views/order_view.js @@ -0,0 +1,16 @@ +import Backbone from 'backbone'; + + +const OrderView = Backbone.View.extend({ + initialize(params) { + this.template = params.template; + this.bus = params.bus; + this.listenTo(this.model, 'change', this.render); + this.listenTo(this.model.get('quote'), 'change', this.swapOrder); + }, + render() { + const compiledTemplate = this.template(this.model.toJSON()); + this.$el.html(compiledTemplate); + return this; + }, + From 6421968254c88721d0f089b4a9297ff082a3cfc1 Mon Sep 17 00:00:00 2001 From: Sara C Date: Mon, 18 Dec 2017 00:35:20 -0800 Subject: [PATCH 11/13] methods for order view made --- src/app.js | 1 + src/collections/order_list.js | 2 +- src/models/order.js | 2 +- src/views/order_list_view.js | 1 + src/views/order_view.js | 24 +++++++++++++++++++++++- 5 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 src/views/order_list_view.js diff --git a/src/app.js b/src/app.js index bcfb4df..e6be51a 100644 --- a/src/app.js +++ b/src/app.js @@ -8,6 +8,7 @@ import Simulator from 'models/simulator'; import QuoteList from 'collections/quote_list'; import QuoteListView from 'views/quote_list_view'; import OrderList from 'collections/order_list'; +import OrderListView from 'views/order_list_view'; const quoteData = [ { diff --git a/src/collections/order_list.js b/src/collections/order_list.js index 6d9ed9f..6b77957 100644 --- a/src/collections/order_list.js +++ b/src/collections/order_list.js @@ -1,7 +1,7 @@ import Backbone from 'backbone'; import Order from '../models/order'; -const OrderList = Backbone.collection.extend({ +const OrderList = Backbone.Collection.extend({ model: Order, }); diff --git a/src/models/order.js b/src/models/order.js index 929b186..f135bb2 100644 --- a/src/models/order.js +++ b/src/models/order.js @@ -1,7 +1,7 @@ import Backbone from 'backbone'; -const Order = Backbone.model.extend ({ +const Order = Backbone.Model.extend ({ initialize(attributes) { }, diff --git a/src/views/order_list_view.js b/src/views/order_list_view.js new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/views/order_list_view.js @@ -0,0 +1 @@ + diff --git a/src/views/order_view.js b/src/views/order_view.js index 93991c3..ea64193 100644 --- a/src/views/order_view.js +++ b/src/views/order_view.js @@ -13,4 +13,26 @@ const OrderView = Backbone.View.extend({ this.$el.html(compiledTemplate); return this; }, - + events: { + 'click .btn-cancel': 'cancelOrder', + }, + cancelOrder() { + this.model.destroy(); + this.remove(); + }, + swapOrder() { + if (this.model.get('buy')) { + if (this.model.get('quote').get('price') <= this.model.get('setPrice')) { + this.bus.trigger('buyOrder', this); + this.cancelOrder(); + } + } else { + if (this.model.get('quote').get('price') >= this.model.get('setPrice')) { + this.bus.trigger('sellOrder', this); + this.cancelOrder(); + } + } + }, +}); + +export default OrderView; From b1656667ba076241850dc3808f9f57b6c11b9d93 Mon Sep 17 00:00:00 2001 From: Sara C Date: Mon, 18 Dec 2017 00:41:43 -0800 Subject: [PATCH 12/13] making render function for order list view --- src/views/order_list_view.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/views/order_list_view.js b/src/views/order_list_view.js index 8b13789..69777dc 100644 --- a/src/views/order_list_view.js +++ b/src/views/order_list_view.js @@ -1 +1,32 @@ +import Backbone from 'backbone'; +import Order from '../models/order'; +import OrderView from './order_view'; +import $ from 'jquery'; +const OrderListView = Backbone.View.extend({ + initialize(params) { + this.bus = params.bus; + this.template = params.template; + this.quotes = params.quotes; + this.listenTo(this.model, 'update', this.render); + }, + render() { + this.$('#orders').empty(); + + this.model.each((order) => { + const orderView = new OrderView({ + model: order, + template: this.template, + bus: this.bus, + tagName: 'li', + quotes: 'this.quotes', + className: 'order', + }); + this.$('#orders').append(orderView.render().$el); + }); + return this; + }, +}); + + +export default OrderListView; From 1b88b980fbd56e77e32d69d41a731d079978828b Mon Sep 17 00:00:00 2001 From: Sara C Date: Mon, 18 Dec 2017 01:58:09 -0800 Subject: [PATCH 13/13] orders mostly work. Needs refactoring for full functionality --- src/app.js | 18 ++++++++++++++++ src/models/order.js | 8 +++---- src/views/order_list_view.js | 42 ++++++++++++++++++++++++++++++++++++ src/views/order_view.js | 4 ++-- src/views/quote_list_view.js | 1 + 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/app.js b/src/app.js index e6be51a..1129790 100644 --- a/src/app.js +++ b/src/app.js @@ -40,6 +40,14 @@ $(document).ready(function() { }); const quoteTemp = _.template($('#quote-template').html()); const tradeTemp = _.template($('#trade-template').html()); + const orderTemp = _.template($('#order-template').html()); + + const dropdown = function dropdown() { + quotes.each((quote) => { + const symbol = quote.get('symbol'); + $('select[name="symbol"]').append(``); + }); + }; //create new quote view obj const quoteListView = new QuoteListView({ @@ -50,6 +58,16 @@ $(document).ready(function() { bus: bus, }); + const orderListView = new OrderListView({ + model: orders, + quotes: quotes, + template: orderTemp, + bus: bus, + el: '#order-workspace' + }); + + dropdown(); + orderListView.render(); quoteListView.render(); simulator.start(); diff --git a/src/models/order.js b/src/models/order.js index f135bb2..90d5269 100644 --- a/src/models/order.js +++ b/src/models/order.js @@ -10,14 +10,14 @@ const Order = Backbone.Model.extend ({ if (!attributes.symbol) { errors['symbol'] = ["You must specify a symbol"]; } - if (!attributes.setPrice) { + if (!attributes.targetPrice) { errors['price'] = ["You must specify price."]; } - if (this.get('buy') && attributes.setPrice >= this.get('quote').get('price')) { + if (this.get('buy') && attributes.targetPrice >= this.get('quote').get('price')) { errors['price'] = ["The buy price needs to be less than the current price."] } - if (this.get('buy') && attributes.setPrice <= this.get('quote').get('price')) { - errors['price'] = ["The sell price needs to be greater than the current price."] + if (!this.get('buy') && attributes.targetPrice <= this.get('quote').get('price')) { + errors['price'] = ["The price is lower than the market price."] } if ( Object.keys(errors).length > 0 ) { return errors; diff --git a/src/views/order_list_view.js b/src/views/order_list_view.js index 69777dc..6703fe0 100644 --- a/src/views/order_list_view.js +++ b/src/views/order_list_view.js @@ -26,6 +26,48 @@ const OrderListView = Backbone.View.extend({ }); return this; }, + events: { + 'click .btn-buy': 'buyOrder', + 'click .btn-sell': 'sellOrder', + }, + makeOrder(value) { + this.$('.form-errors').empty(); + + const orderData = { + bus: this.bus, + symbol: this.$('select[name=symbol]').val(), + quote: this.quotes.find({symbol: this.$('select[name=symbol]').val()}), + buy: value.buy, + targetPrice: parseFloat(this.$('input[name=price-target]').val()) + }; + return new Order(orderData); + }, + buyOrder: function(e) { + e.preventDefault(); + const order = this.makeOrder({buy: true}); + this.validate(order); + }, + sellOrder: function(e) { + e.preventDefault(); + const order = this.makeOrder({buy: false}); + this.validate(order); + }, + validate(order) { + if (order.isValid()) { + this.model.add(order); + this.$el.find('form').trigger('reset'); + } else { + const errors = order.validationError; + const errorSection = this.$('.form-errors'); + + Object.keys(errors).forEach((field) => { + errors[field].forEach((error) => { + const html = `

${error}

`; + errorSection.append(html); + }); + }); + } + }, }); diff --git a/src/views/order_view.js b/src/views/order_view.js index ea64193..7b3ed5a 100644 --- a/src/views/order_view.js +++ b/src/views/order_view.js @@ -22,12 +22,12 @@ const OrderView = Backbone.View.extend({ }, swapOrder() { if (this.model.get('buy')) { - if (this.model.get('quote').get('price') <= this.model.get('setPrice')) { + if (this.model.get('quote').get('price') <= this.model.get('targetPrice')) { this.bus.trigger('buyOrder', this); this.cancelOrder(); } } else { - if (this.model.get('quote').get('price') >= this.model.get('setPrice')) { + if (this.model.get('quote').get('price') >= this.model.get('targetPrice')) { this.bus.trigger('sellOrder', this); this.cancelOrder(); } diff --git a/src/views/quote_list_view.js b/src/views/quote_list_view.js index f3d6e2c..7c05627 100644 --- a/src/views/quote_list_view.js +++ b/src/views/quote_list_view.js @@ -17,6 +17,7 @@ render() { const quoteView = new QuoteView({ model: quote, template: this.quoteTemplate, + bus: this.bus, tagName: 'li', className: 'quote', });