-
Notifications
You must be signed in to change notification settings - Fork 43
Pipes - Sairagul - AdaTrader #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
35684db
b6c4a82
18ef902
aaf8c5e
f8cde71
a9c0a3f
f48d65c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import Backbone from 'backbone'; | ||
| import Order from 'models/order'; | ||
|
|
||
| const OrderList = Backbone.Collection.extend({ | ||
| model: Order, | ||
| }); | ||
|
|
||
| export default OrderList; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import Backbone from 'backbone'; | ||
| import Quote from '../models/quote'; | ||
|
|
||
| const Order = Backbone.Model.extend({ | ||
|
|
||
| buyIt(){ | ||
| const quote = this.get('quote'); | ||
|
|
||
| if (this.get('targetPrice') >= quote.get('price') || | ||
| this.get('targetPrice') === ''){ | ||
| quote.buy(); | ||
| return true; | ||
| } | ||
| return false; | ||
| }, | ||
|
|
||
| sellIt(){ | ||
| const quote = this.get('quote'); | ||
|
|
||
| if (this.get('targetPrice') <= quote.get('price') || | ||
| this.get('targetPrice') === '') { | ||
|
|
||
| quote.sell(); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| }); | ||
|
|
||
| export default Order; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import Backbone from 'backbone'; | ||
| import _ from 'underscore'; | ||
| import Order from '../models/order'; | ||
| import OrderView from '../views/order_view'; | ||
| import Quote from '../models/quote'; | ||
| import QuoteView from '../views/quote_view'; | ||
|
|
||
| const OrderFormView = Backbone.View.extend({ | ||
| initialize(params){ | ||
| this.template = params.template; | ||
| this.bus = params.bus; | ||
| this.quotesList = params.quotesList; | ||
| }, | ||
|
|
||
| events: { | ||
| 'click button.btn-buy': 'buy', | ||
| 'click button.btn-sell': 'sell' | ||
| }, | ||
| buy(event) { | ||
| event.preventDefault(); | ||
| let orderData = { | ||
| buy: true | ||
| }; | ||
|
|
||
| orderData.symbol = this.$('#orderForm select').val(); | ||
| orderData.targetPrice = parseFloat(this.$('#target-price').val()); | ||
| orderData.quote = this.quotesList.findWhere({symbol: orderData.symbol}); | ||
|
|
||
| let order = new Order(orderData); | ||
| if (!order.isValid()) { | ||
| order.destroy(); | ||
| this.updateStatusMessage(order.validationError); | ||
| return; | ||
| } | ||
|
|
||
| // validate(attributes) { | ||
| // let warning; | ||
| // if (!attributes.targetPrice) { | ||
| // warning = 'Target price can not be blank.'; | ||
| // } | ||
| // if (this.getthis.get('targetPrice') <= quote.get('price') | ||
| // console.log('target price should be higher than current price'); | ||
| // Do validation on the order | ||
| //orders.append(order); | ||
| this.bus.trigger('newOrder', order); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems a little round-about to trigger an event on the bus here. Might be cleaner for the |
||
|
|
||
| }, | ||
|
|
||
| sell(event) { | ||
| event.preventDefault(); | ||
| let orderData = { | ||
| buy: false | ||
| }; | ||
|
|
||
| orderData.symbol = this.$('#orderForm select').val(); | ||
| orderData.targetPrice = parseFloat(this.$('#target-price').val()); | ||
| orderData.quote = this.quotesList.findWhere({symbol: orderData.symbol}); | ||
|
|
||
| let order = new Order(orderData); | ||
| // Do validation on the order | ||
| //orders.append(order); | ||
| this.bus.trigger('newOrder', order); | ||
| }, | ||
|
|
||
|
|
||
| }); | ||
| export default OrderFormView; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import Backbone from 'backbone'; | ||
| import _ from 'underscore'; | ||
| import OrderView from '../views/order_view'; | ||
| import Order from '../models/order'; | ||
| // import OrderList from '../collections/order_list'; | ||
|
|
||
|
|
||
| const OrderListView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.template = params.template; | ||
| this.bus = params.bus; | ||
|
|
||
| this.listenTo(this.model, 'update', this.render); | ||
| this.listenTo(this.bus, 'newOrder', this.addOrder); | ||
|
|
||
| // do something with params.anotherOption | ||
|
|
||
| // log that we're listenTo | ||
| }, | ||
| render() { | ||
| // update event happened | ||
| this.$('#orders').empty(); | ||
| this.model.each((order) => { | ||
| const orderView = new OrderView({ | ||
| model: order, | ||
| template: this.template, | ||
| tagName: 'li', | ||
| className: 'order', | ||
| }); | ||
|
|
||
| this.$('#orders').append(orderView.render().$el); | ||
| }); | ||
| return this; | ||
| }, | ||
| addOrder(order) { | ||
| this.model.add(order); | ||
| } | ||
| }); | ||
|
|
||
| export default OrderListView; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import Backbone from 'backbone'; | ||
| import Order from '../models/order'; | ||
|
|
||
| const OrderView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.template = params.template; | ||
|
|
||
| this.listenTo(this.model.get('quote'), "change", this.processOrder); | ||
| }, | ||
| processOrder(){ | ||
| if(this.model.get('buy')){ | ||
| if(this.model.buyIt()){ | ||
| this.model.destroy(); | ||
| this.remove(); | ||
| } | ||
| } | ||
| else{ | ||
| if(this.model.sellIt()){ | ||
| if(this.model.sellIt()){ | ||
| this.model.destroy(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have the same condition here twice! I suspect this is why sell orders don't clear themselves. |
||
| this.remove(); | ||
| } | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| render() { | ||
| const compiledTemplate = this.template(this.model.toJSON()); | ||
| this.$el.html(compiledTemplate); | ||
| return this; | ||
| }, | ||
|
|
||
| events: { | ||
| 'click .btn-cancel': 'cancel', | ||
| }, | ||
|
|
||
| cancel(){ | ||
| this.model.destroy(); | ||
| this.remove(); | ||
| }, | ||
| }); | ||
|
|
||
| export default OrderView; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import Backbone from 'backbone'; | ||
| import _ from 'underscore'; | ||
| import QuoteView from '../views/quote_view'; | ||
|
|
||
| const QuoteListView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.template = params.template; | ||
| this.listenTo(this.model, 'update', this.render); | ||
| }, | ||
| render() { | ||
| this.$('#quotes').empty(); | ||
| // Iterate through the list rendering each Quote | ||
| this.model.each((quote) => { | ||
| // Create a new QuoteView with the model & template | ||
| const quoteView = new QuoteView({ | ||
| model: quote, | ||
| template: this.template, | ||
| tagName: 'li', | ||
| className: 'quote', | ||
| }); | ||
| // Then render the QuoteView | ||
| // And append the resulting HTML to the DOM. | ||
| this.$('#quotes').append(quoteView.render().$el); | ||
| }); | ||
| return this; | ||
| } | ||
| }); | ||
|
|
||
| export default QuoteListView; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| 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; | ||
| }, | ||
|
|
||
| events: { | ||
| 'click button.btn-buy': 'buy', | ||
| 'click button.btn-sell': 'sell' | ||
| }, | ||
| buy(event) { | ||
| this.model.buy(); | ||
|
|
||
| }, | ||
|
|
||
| sell(event) { | ||
| this.model.sell(); | ||
| }, | ||
| }); | ||
|
|
||
| export default QuoteView; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that the
OrderFormViewknows about theQuoteListso it can associate the quote with a new order.