Conversation
…ng and triggering events on bus through quote view and open order list view
Ada TraderWhat We're Looking For
|
|
|
||
| let openOrders = this.model.where({symbol: quote.get('symbol')}); | ||
| if (openOrders.length >= 1) { | ||
| openOrders.forEach((openOrder) => { |
There was a problem hiding this comment.
You don't need to wrap the call to forEach with a check that length >= 1. If the list has zero elements, then the loop body will execute zero times.
| this.listenTo(this.bus, 'current_quote_list', this.getQuoteList); | ||
| }, | ||
| getQuoteList(quoteList) { | ||
| this.quoteList = quoteList; |
There was a problem hiding this comment.
Instead of using an event on the bus to get the quoteList, why not pass it in as an extra parameter when instantiating the OpenOrderListView? Building an event for it is overkill.
| }, | ||
| checkOpenOrders(quote) { | ||
|
|
||
| let openOrders = this.model.where({symbol: quote.get('symbol')}); |
There was a problem hiding this comment.
Currently, the workflow for executing an open order looks like this:
- Quote price changes
quote_changeevent on the bus- Handled here by
OpenOrderListView.checkOpenOrders() - Find
OpenOrders matching the quote - For each one, determine if it needs to be sold
But we can simplify this! The key observation is that each OpenOrder already knows which Quote it's for - we need this information to validate the OpenOrder. Instead of working through the intermediaries of the bus and the OpenOrderListView, each OpenOrder could listen directly to its Quote for price changes. Then the workflow would look like:
- Quote price changes
- Event on the quote
- Handler in
OpenOrder - Determine if the order needs to be sold
This eliminates dependencies on the bus and the OpenOrderListView. It also simplifies the code, since we don't need to work with a list of OpenOrders. If there are multiple OpenOrders corresponding to the same quote each will have a separate handler registered, and each will run when the event occurs (steps 3-4).
Ada Trader
Congratulations! You're submitting your assignment!
Comprehension Questions