Conversation
… display trip detail info
BackTREKWhat We're Looking For
|
|
|
||
| for (let i = 0; i < fields.length; i += 1) { | ||
| const field = fields[i]; | ||
| formData[field.name] = $(`#${field.id}`).val(); |
There was a problem hiding this comment.
I think in this case our field variable is actually already a jQuery object, so we don't need to do the ID-based lookup:
formData[field.name] = field.val();| }, | ||
| // how is this handled??? | ||
| error: function(trip) { | ||
| console.log(trip); |
There was a problem hiding this comment.
I think a good option here would be to do the same "Trip Not Found" error message that you have for the 204 response code case in the success handler. Although it'd probably be more accurate to use a message like "Something went wrong, please try again later."
| // if already sorted, sort desc | ||
| if (fields.includes(className)) { | ||
| if (className === allTrips.comparator) { | ||
| allTrips.models.reverse(); |
There was a problem hiding this comment.
This is a temporary change -- Backbone won't "know" that we've reversed the sort order for this comparator.
As a result, when Backbone decides to re-sort the collection for us (such as when a new trip is added), it will go back to the non-reversed order. How could we implement the sorting for our Backbone collection such that it will keep track of whether the sort was reversed or not?
| filterBy(field, value) { | ||
| if (field === 'cost' || field === 'weeks') return this.greaterThan(field, value); | ||
|
|
||
| return this.includes(field, value); |
There was a problem hiding this comment.
Creating a new instance of the collection every time we do filterBy is definitely an interesting approach.
Contrary to most of the advice we've given about where to put code in MVC projects, I think for this case it might be preferable to implement the filtering logic in the renderTripList function, because the filtering is only intended to impact which models are displayed.
Creating new collections like this could cause significant headaches when combined with Backbone Views and the fact that we generally connect them to collections and models through event listeners.
| if (model.get(attributes[i]).toString().includes(value)) return true; | ||
| // return model.get(attributes[i]).toString().includes(value); | ||
| } else { | ||
| if (model.get(attributes[i]).toLowerCase().includes(value.toLowerCase())) return true; |
There was a problem hiding this comment.
This code section could be simplified a bit by having each attribute always get toString() called on it (which is unnecessary but harmless for the string attributes):
const val = value.toLowerCase();
for (let i = 0; i < attributes.length; i++) {
const attr = model.get(attributes[i]).toString().toLowerCase();
if (attr.includes(val)) return true;
}
BackTREK
Congratulations! You're submitting your assignment!
Comprehension Questions