-
Notifications
You must be signed in to change notification settings - Fork 44
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
Carets - Maria - BackTrek #37
base: master
Are you sure you want to change the base?
Changes from all commits
18353f6
3359fd9
75448cb
2c216b5
3f494d1
6e61aff
6b19e9f
5219405
e14410d
387d966
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 |
---|---|---|
|
@@ -6,8 +6,108 @@ import _ from 'underscore'; | |
import './css/foundation.css'; | ||
import './css/style.css'; | ||
|
||
console.log('it loaded!'); | ||
import Trip from './app/models/trip'; | ||
import TripList from './app/collections/trip_list'; | ||
|
||
const tripList = new TripList(); | ||
|
||
let tripTemplate; | ||
let showTemplate; | ||
|
||
const fields = ['name', 'continent', 'category', 'weeks', 'cost']; | ||
|
||
const render = function render(tripList) { | ||
const tripListElement = $('#trip-list'); | ||
tripListElement.empty(); | ||
|
||
tripList.forEach((trip) => { | ||
tripListElement.append(tripTemplate(trip.attributes)); | ||
}); | ||
}; | ||
|
||
const loadTrips = function loadTrips() { | ||
tripList.fetch(); | ||
tripList.on('update', render, tripList); | ||
}; | ||
|
||
const events = { | ||
loadTrip(id) { | ||
const singleTripElement = $('#single-trip'); | ||
singleTripElement.empty(); | ||
const trip = new Trip({id: id}); | ||
|
||
$('h3').text('Trip Info'); | ||
|
||
trip.fetch().done(() => { | ||
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. There's no detecting that the |
||
let showHTML = showTemplate(trip.attributes); | ||
singleTripElement.append($(showHTML)); | ||
}); | ||
}, | ||
|
||
addTrip(event) { | ||
event.preventDefault(); | ||
|
||
const tripData = {}; | ||
fields.forEach((field) => { | ||
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. Breaking this into a helper method which reads the form would be better. |
||
const val = $(`input[name=${field}]`).val(); | ||
if (val != '') { | ||
tripData[field] = val; | ||
} | ||
}); | ||
|
||
const trip = new Trip(tripData); | ||
if (trip.isValid()) { | ||
trip.save({}, { | ||
success: events.successfulSave, | ||
error: events.failedSave, | ||
}); | ||
} else { | ||
$('#status-messages ul').append(`<li>Error ${trip.validationError['name'][0]}`); | ||
$('#status-messages').show(); | ||
} | ||
console.log(tripList); | ||
}, | ||
|
||
successfulSave(trip, response) { | ||
|
||
console.log('Success!'); | ||
console.log(trip); | ||
console.log(response); | ||
$('#status-messages ul').empty(); | ||
$('#status-messages ul').append(`<li>${trip.get('name')} added!</li>`); | ||
$('#status-messages').show(); | ||
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 would also clear the form on a successful save. |
||
tripList.fetch(); | ||
}, | ||
failedSave(trip, response) { | ||
console.log('Error!'); | ||
console.log(trip); | ||
console.log(response); | ||
$('#status-messages ul').empty(); | ||
console.log(response.responseJSON.errors); | ||
for(let key in response.responseJSON.errors) { | ||
response.responseJSON.errors[key].forEach((error) => { | ||
$('#status-messages ul').append(`<li>${key}: ${error}</li>`); | ||
}) | ||
} | ||
$('#status-messages').show(); | ||
trip.destroy(); | ||
}, | ||
}; | ||
|
||
$(document).ready( () => { | ||
$('main').html('<h1>Hello World!</h1>'); | ||
tripTemplate = _.template($('#trip-template').html()); | ||
showTemplate = _.template($('#show-template').html()); | ||
|
||
$('#trips_button').click(() => { | ||
$('h2').text('Trip Options'); | ||
loadTrips(); | ||
}); | ||
|
||
$('#trip-list').on('click', 'tr', function() { | ||
const tripID = $(this).attr('trip-id'); | ||
events.loadTrip(tripID); | ||
}); | ||
|
||
$('#add-trip-form').submit(events.addTrip); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Backbone from 'backbone'; | ||
import Trip from '../models/trip'; | ||
|
||
const TripList = Backbone.Collection.extend({ | ||
model: Trip, //send it an object as a parameter | ||
url: 'https://ada-backtrek-api.herokuapp.com/trips', | ||
parse(response) { | ||
return response; //array from the JSON | ||
}, | ||
}); | ||
|
||
export default TripList; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Backbone from 'backbone'; | ||
|
||
const Trip = Backbone.Model.extend({ | ||
urlRoot: 'https://ada-backtrek-api.herokuapp.com/trips', | ||
|
||
validate: function(attributes) { | ||
const errors = {}; | ||
|
||
if (!attributes.name) { | ||
errors['name'] = ['Name can\'t be blank']; | ||
} | ||
|
||
if (!attributes.continent) { | ||
errors['continent'] = ['Continent can\'t be blank']; | ||
} | ||
|
||
if (!attributes.category) { | ||
errors['category'] = ['Category can\'t be blank']; | ||
} | ||
|
||
if (!attributes.weeks) { | ||
errors['weeks'] = ['Weeks can\'t be blank']; | ||
} | ||
|
||
if (!attributes.cost) { | ||
errors['cost'] = ['Cost can\'t be blank']; | ||
} | ||
|
||
if ( isNaN(attributes.weeks) ) { | ||
errors['weeks'] = ['Weeks must be a number']; | ||
} | ||
|
||
if ( isNaN(attributes.cost) ) { | ||
errors['cost'] = ['Cost must be a number']; | ||
} | ||
} | ||
}); | ||
|
||
export default Trip |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
|
||
/* Your styles go here! */ | ||
#trips_button { | ||
float: right; | ||
margin-top: 2%; | ||
margin-right: 5%; | ||
} | ||
|
||
header { | ||
background-color: magenta; | ||
max-width: 100%; | ||
} |
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.
It's theoretically possible that the fetch could complete before the
update
event listener is added. So I would register the listener in$(document).ready
when the application starts.This will also register
render
as an event listener every timeloadTrips
is clicked, so the list could be rendered multiple times.