-
Notifications
You must be signed in to change notification settings - Fork 43
pipes | tanisha | ada trader #31
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
ee06eab
2a08136
1738942
c73865f
21e79a9
af80f71
1d798e0
4673754
2618d65
33123b6
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,12 @@ | ||
| // list of all open orders | ||
| // Orders are listed with the oldest at the top. | ||
| // Each open order entry in the list includes the symbol being ordered, whether it is a buy or sell order, the target price, and a cancel button. | ||
|
|
||
| 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,22 @@ | ||
| import Backbone from 'backbone'; | ||
| import Quote from 'models/quote'; | ||
| // order needs quote | ||
| // Buy and Sell button creates the order | ||
|
|
||
| // order is listening to quote for changes in price | ||
| // when it hears the price changes the handler will have some logic to determine if the price is at a level in which to buy or sell | ||
|
|
||
| // before you have the quote buy or sell, you should destroy the order - to prevent duplicate buying or selling | ||
|
|
||
| const Order = Backbone.Model.extend({ | ||
| initialize: function(attributes) { | ||
| }, | ||
| validate: function(attributes) { | ||
| const errors = {}; | ||
|
|
||
| console.log(attributes); | ||
| }, | ||
|
|
||
| }); | ||
|
|
||
| export default Order; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // import Backbone from 'backbone'; | ||
| // import Order from 'models/order'; | ||
| // | ||
| // const FormView = Backbone.View.extend({ | ||
| // initialize(params) { | ||
| // this.template = params.template; | ||
| // }, | ||
| // createOrder: { | ||
| // const order = new Order({ | ||
| // | ||
| // }) | ||
| // } | ||
| // }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import Backbone from 'backbone'; | ||
| import Order from 'models/order'; | ||
| import _ from 'underscore'; | ||
| import OrderList from 'collections/order_list'; | ||
| import OrderView from 'views/order_view'; | ||
|
|
||
|
|
||
| const OrderListView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.template = params.template; | ||
| this.listenTo(this.model, 'update', this.render); | ||
| }, | ||
| render() { | ||
| this.$('#orders').empty(); | ||
|
|
||
| this.model.each((order) => { | ||
| console.log(`rendering ${order}`); | ||
| const orderView = new OrderView({ | ||
| model: order, | ||
| template: this.template, | ||
| tagName: 'li', | ||
| className: 'order', | ||
| }); | ||
| this.$('#orders').append(orderView.render().$el); | ||
| }); | ||
| return this; | ||
| }, | ||
| events: { | ||
| 'click button.btn-buy': 'newBuy', | ||
| 'click button.btn-sell': 'newSell' | ||
| }, | ||
| getFormData: function() { | ||
| const formData = { | ||
| // buy: false, | ||
| symbol: this.$('select option:selected').text(), | ||
| targetPrice: parseFloat(this.$('input').val()), | ||
| }; | ||
| console.log(formData); | ||
| return formData; | ||
| }, | ||
| newBuy: function(e) { | ||
| e.preventDefault(); | ||
| this.createOrder(true); | ||
| }, | ||
| newSell: function(e) { | ||
| e.preventDefault(); | ||
| this.createOrder(false); | ||
| }, | ||
| createOrder: function(buy) { | ||
|
|
||
| let formData = this.getFormData(); | ||
| formData.buy = buy; | ||
| const newOrder = new Order(formData); | ||
| this.model.add(newOrder); | ||
|
|
||
| }, | ||
|
|
||
| }); | ||
|
|
||
| export default OrderListView; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import Backbone from 'backbone'; | ||
| import Order from 'models/order'; | ||
|
|
||
| const OrderView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.template = params.template; | ||
| }, | ||
| render() { | ||
| console.log(this.model.attributes); | ||
| const compiledTemplate = this.template(this.model.attributes); | ||
| console.log('rendered 1'); | ||
|
|
||
| this.$el.html(compiledTemplate); | ||
|
|
||
| return this; | ||
| }, | ||
| }); | ||
|
|
||
| export default OrderView; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import Backbone from 'backbone'; | ||
| import _ from 'underscore'; | ||
| import Quote from 'models/quote'; | ||
| import QuoteList from 'collections/quote_list'; | ||
| import QuoteView from 'views/quote_view'; | ||
|
|
||
| const QuoteListView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.template = params.template; | ||
| this.tradeTemplate = params.tradeTemplate; | ||
| this.listenTo(this.model, 'update', this.renderQuote); | ||
| }, | ||
| renderQuote() { | ||
| this.$('#quotes').empty(); | ||
|
|
||
| this.model.each((quote) => { | ||
| const quoteView = new QuoteView({ | ||
| model: quote, | ||
| template: this.template, | ||
| className: 'quote', | ||
| }); | ||
| this.listenTo(quoteView, 'tradeUpdate', this.trade); | ||
| this.$('#quotes').append(quoteView.render().$el); | ||
| }); | ||
| return this; | ||
| }, | ||
| trade: function(quoteView) { | ||
| const compiledTradeTemplate = this.tradeTemplate(quoteView.model.toJSON()); | ||
| this.$('#trades').prepend(compiledTradeTemplate); | ||
| }, | ||
|
|
||
| }); | ||
|
|
||
| export default QuoteListView; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| 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() { | ||
| this.model.set('buy', true); | ||
| this.trigger('tradeUpdate', this); | ||
|
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. I'm not sure I like the attribute name Since the type of the most recent trade isn't really relevant for the quote, an even better strategy would be to not set it on the model at all, but provide it as an extra parameter when you trigger the event: this.trigger('tradeUpdate', this, true);And then the event listener in trade: function(quoteView, buy) {
const tradeData = quoteView.model.toJSON();
tradeData.buy = buy;
const compiledTradeTemplate = this.tradeTemplate(tradeData);
// ...
} |
||
| this.model.buy(); | ||
| }, | ||
| sell() { | ||
| this.model.set('buy', false); | ||
| this.trigger('tradeUpdate', this); | ||
| 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.
Right now you only have the symbol for this quote, since that's what you get when you read the form. However, in order to connect all the pieces, you really need the
Quoteitself, not just the symbol. That will let theOrderlisten to events on the quote, validate the initial buy/sell price, etc.One way to do this would be for the
OrderListViewto know about theQuoteListcollection, passing it in as an extra property to the constructor fromapp.js. Then inQuoteListyou could write a function that, given a symbol, returns the corresponding quote.