Created
November 8, 2011 02:28
-
-
Save billeisenhauer/1346836 to your computer and use it in GitHub Desktop.
Backbone.js Model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TRIPCASE.models.BonusOffersRequest = TRIPCASE.Model.extend({ | |
proxyName: 'BonusOffersRequestProxy', | |
init: function(attrs, options) { | |
this.trip = options.trip; | |
}, | |
validate: function(attrs) { | |
var errors = []; | |
if (_.isEmpty(attrs.vendor_code)) { | |
errors.push("Vendor must be selected"); | |
} | |
if (_.isEmpty(attrs.bonus_types)) { | |
errors.push("Bonus type(s) must be selected"); | |
} | |
if (this.hasDateRange(attrs) && this.isInvalidRange(attrs)) { | |
errors.push("The end date cannot be before the start date"); | |
} | |
if (this.isExpiredRange(attrs)) { | |
errors.push('The end date cannot be in the past'); | |
} | |
return _.any(errors) ? errors : null; | |
}, | |
hasDateRange: function(attrs) { | |
attrs || (attrs = this.attributes); | |
return !_.isEmpty(attrs.start_date) && !_.isEmpty(attrs.end_date); | |
}, | |
isInvalidRange: function(attrs) { | |
attrs || (attrs = this.attributes); | |
var startDate = this.get('start_date').isoDateStrToDate(); | |
var endDate = this.get('end_date').isoDateStrToDate(); | |
return startDate.compareTo(endDate) > 0; | |
}, | |
isExpiredRange: function(attrs) { | |
attrs || (attrs = this.attributes); | |
var endDate = this.get("end_date"); | |
if (_.isEmpty(endDate)) return false; | |
var now = new Date(); | |
var endTime = endDate.isoDateStrToDate(); | |
return now.compareTo(endTime) > 0; | |
}, | |
getTrip: function() { | |
return this.trip; | |
}, | |
getAvailableBonusTypes: function() { | |
return this.get('vendor_types'); | |
}, | |
getBonusTypesStr: function() { | |
return this.get('bonus_types').join(', '); | |
}, | |
hasBonusOffers: function() { | |
return !this.bonusOffers.isEmpty(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment