-
Notifications
You must be signed in to change notification settings - Fork 43
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
Amy Cash - Pipes - Ada Trader #41
base: master
Are you sure you want to change the base?
Changes from all commits
2204497
494d2dd
bdf3910
045199c
0dc56d3
ed66950
cc22065
229f394
aaa28d5
de1f223
93d73b8
fb31f60
f7ab750
545c61b
2e616f7
847d83d
361792c
7f8ab5b
52a2eee
8490d2c
3a3cf98
ed2e8d3
a4c8fea
6ef1c52
4d052ab
c3d62af
498666f
ca2b171
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,44 @@ | ||
import Backbone from 'backbone'; | ||
|
||
const Order = Backbone.Model.extend({ | ||
|
||
// need to do validations | ||
validate(attributes) { | ||
|
||
const errors = {}; | ||
// create a hash for errors | ||
|
||
if(!attributes.symbol) { | ||
errors['symbol'] = ['Need to know what you\'re buying or selling']; | ||
//if there's no symbol, give this message | ||
} | ||
|
||
if(!attributes.targetPrice) { | ||
errors['targetPrice'] = ['Please enter a target price']; | ||
} | ||
|
||
if(attributes.buy && ( attributes.targetPrice >= attributes.quote.get('price') ) ){ | ||
errors['buyPriceError'] = ['Your offered price higher than the current price.']; | ||
console.log("too high to get over"); | ||
} | ||
|
||
if(attributes.buy === false && (attributes.targetPrice <= attributes.quote.get('price'))) { | ||
errors['sellPriceError'] = ['Your offered price is lower than the current price.']; | ||
console.log("too low to get under"); | ||
} | ||
|
||
if ( Object.keys(errors).length > 0) { | ||
return errors; | ||
//if there are errors, return them | ||
} else { | ||
return false; | ||
} | ||
}, | ||
|
||
cancelOrder() { | ||
console.log("we're cancelling your order!"); | ||
this.destroy();// | ||
}, | ||
}); | ||
|
||
export default Order; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,15 @@ const Quote = Backbone.Model.extend({ | |
}, | ||
|
||
buy() { | ||
// Implement this function to increase the price by $1.00 | ||
let buyPrice = this.get('price'); | ||
let newPrice = buyPrice + 1.00; | ||
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. We could probably use |
||
this.set('price', newPrice) | ||
}, | ||
|
||
sell() { | ||
// Implement this function to decrease the price by $1.00 | ||
let sellPrice = this.get('price'); | ||
let newPrice = sellPrice - 1.00; | ||
this.set('price', newPrice) | ||
}, | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import Backbone from 'backbone'; | ||
|
||
import OrderView from './order_view'; | ||
import Order from '../models/order'; | ||
|
||
|
||
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, 'send_quotes', this.getQuotes); | ||
// this.listenTo(this.bus, 'price_check', this.checkPrice); | ||
|
||
}, | ||
render(){ | ||
this.$('.orders').empty(); | ||
|
||
this.model.each((order) => { | ||
|
||
const orderView = new OrderView({ | ||
model: order, | ||
template: this.template, | ||
tagName: 'li', | ||
className: 'order', | ||
bus: this.bus, | ||
}); // end orderView const | ||
|
||
this.$('.orders').append(orderView.render().$el); | ||
|
||
}); // end each | ||
|
||
return this; | ||
}, | ||
|
||
events: { | ||
'click button.btn-buy': 'createBuyOrder', | ||
'click button.btn-sell': 'createSellOrder', | ||
}, | ||
|
||
createBuyOrder(event) { | ||
event.preventDefault(); | ||
console.log("you clicked the buy button"); | ||
this.createOrder(true); | ||
}, | ||
|
||
createSellOrder(event) { | ||
event.preventDefault(); | ||
console.log("you clicked the sell button"); | ||
this.createOrder(false); | ||
}, | ||
|
||
createOrder(buy) { | ||
const orderData = this.getOrderData(buy); | ||
this.quoteList.each((quote) => { | ||
if (orderData['symbol'] == quote.get('symbol') ) { | ||
orderData['quote'] = quote; | ||
} | ||
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. This could also be written as: orderData.quote = this.quoteList.findWhere({ symbol: orderData.symbol }); |
||
}); | ||
|
||
const newOrder = new Order(orderData); | ||
|
||
if (newOrder.isValid()) { | ||
this.model.add(newOrder); | ||
console.log("instance was added"); | ||
this.clearFormData(); | ||
} else { | ||
this.updateStatusMessageFrom(newOrder.validationError); | ||
newOrder.destroy(); | ||
} | ||
}, | ||
|
||
getOrderData(buy) { | ||
const orderData = {}; | ||
orderData["symbol"] = this.$(`#order-entry-form #symbol option:checked`).val(); | ||
let stringPrice = this.$(`#order-entry-form input[name=price-target]`).val(); | ||
orderData["targetPrice"] = parseFloat(stringPrice); | ||
orderData["buy"] = buy; | ||
this.bus.trigger('get_quote', this.model); | ||
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 looks like triggering this event isn't doing anything, because there are no objects that are listening for it. |
||
|
||
return orderData; | ||
}, | ||
|
||
clearFormData() { | ||
this.$(`#order-entry-form input[name=price-target]`).val(''); | ||
}, | ||
|
||
getQuotes(quotelist){ | ||
this.quoteList = quotelist; | ||
}, | ||
|
||
updateStatusMessageFrom(messageHash) { | ||
const $formErrors = this.$('.form-errors'); | ||
$formErrors.empty(); | ||
Object.keys(messageHash).forEach((messageType) => { | ||
messageHash[messageType].forEach((message) => { | ||
$formErrors.append(`<li>${message}</li>`); | ||
}); | ||
}); | ||
|
||
$formErrors.show(); | ||
}, | ||
|
||
|
||
}); //end orderlistview | ||
|
||
|
||
|
||
export default OrderListView; |
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.
By passing
quoteList
here we are creating a view for a collection of quotes that is not connected to the simulator at all (and thus there appears to be no automatic change in prices).If we insted write
model: quotes,
for this line then we'll be connecting thisQuoteListView
to the same collection of quotes as the simulator receives, which should solve the issue.