Last active
December 5, 2017 16:45
-
-
Save BrianSeng/a5d3cc7f4a7ef2eeb83a244f5c14213f to your computer and use it in GitHub Desktop.
Messing with js based "classes" & prototypal inheritance in AngularJS
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
(function () | |
{ | |
"use strict"; | |
angular | |
.module('request.travelLegs') | |
.factory('TravelLeg', function () | |
{ | |
let TravelLeg = function (data) | |
{ | |
let self = this; | |
//check the data param to check for a JSON object | |
let isDataObject = (typeof data === "object") && (data !== null); | |
this.departingFrom = ""; | |
this.arrivingTo = ""; | |
this.preferredAirline = ""; | |
this.isCommercialFlight = true; | |
this.mustReturnBy = new Date() + 1; | |
if (data && isDataObject) | |
{ | |
self.departingFrom = data['departingFrom']; | |
self.arrivingTo = data['arrivingTo']; | |
self.preferredAirline = data['preferredAirline']; | |
self.isCommercialFlight = data['isCommercialFlight']; | |
self.mustReturnBy = moment(data['mustReturnBy']).toDate(); | |
} | |
} | |
//returning TravelLeg() constructor to be consumed | |
return TravelLeg; | |
}) | |
.factory('FlightLeg', function (TravelLeg) | |
{ | |
let FlightLeg = function (data) | |
{ | |
let self = this; | |
this.departureDate = new Date(); | |
this.departureTime = moment().format('LT'); | |
this.rental = {}; | |
this.hotel = { | |
preferredHotel: "" | |
, checkInDate: new Date() | |
, checkOutDate: new Date() + 2 | |
}; | |
if (data) | |
{ | |
self.departureDate = data['departureDate']; | |
self.departureTime = data['departureTime']; | |
self.rental = data['rental']; | |
self.hotel = data['hotel']; | |
} | |
} | |
//inherit from TravelLeg superclass | |
FlightLeg.prototype = new TravelLeg(); | |
//returning FlightLeg() constructor to be consumed | |
return FlightLeg; | |
}) | |
.factory('ReturnFlight', function (TravelLeg) | |
{ | |
let ReturnFlight = function (data) | |
{ | |
let self = this; | |
this.returnDate = new Date(); | |
this.returnTime = moment().format('LT'); | |
if (data) | |
{ | |
self.returnDate = data['returnDate']; | |
self.returnTime = data['returnTime']; | |
} | |
} | |
//inherit from TravelLeg superclass | |
ReturnFlight.prototype = new TravelLeg(); | |
//returning ReturnFlight() constructor to be consumed | |
return ReturnFlight; | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment