Created
August 18, 2016 11:59
-
-
Save nanlabsweb/2506bd17deda9e294c9221f84076d803 to your computer and use it in GitHub Desktop.
POST - JS plain object example.
This file contains 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
/** | |
* contact.js | |
**/ | |
var Contact = function () { | |
// Constructor, sets the defaults values of the object | |
var clazz = function (attributes) { | |
var defaults = { | |
id: null, | |
contactType: CONTACT_TYPES.UNKNOWN, | |
firstName: null, | |
lastName: null, | |
phone: null, | |
address: null, | |
colleague: { | |
from: null, | |
internPhone: null, | |
isCurrent: null | |
} | |
}; | |
_.extend(this, defaults, attributes); | |
}; | |
// Class Methods | |
_.extend(clazz.prototype, { | |
isNew: function () { | |
return (this.id === null); | |
}, | |
isFriendContactType: function () { | |
return (this.contactType === CONTACT_TYPES.FRIEND); | |
}, | |
isFamilyContactType: function () { | |
return (this.contactType === CONTACT_TYPES.FAMILY); | |
}, | |
isColleagueContactType: function () { | |
return (this.contactType === CONTACT_TYPES.COLLEAGUE); | |
}, | |
isUnknownContactType: function () { | |
return (this.contactType === CONTACT_TYPES.UNKNOWN); | |
}, | |
getId: function() { | |
return this.id.toString(); | |
}, | |
getFullName: function() { | |
if (this.firstName !== '' && this.lastName !== '') { | |
return this.firstName + ', ' + this.lastName; | |
} else if (this.firstName !== '') { | |
return this.firstName; | |
} else if (this.lastName !== '') { | |
return this.lastName; | |
} else { | |
return ''; | |
} | |
}, | |
// Validation helper methods | |
isPhoneRequired: function() { | |
return (!this.isUnknownContactType()); | |
}, | |
isAddressRequired: function() { | |
return (!this.isUnknownContactType()); | |
}, | |
/** | |
* Useful method to parse server response and populate the fields. | |
*/ | |
populateData: function (data) { | |
_.extend(this, data); | |
} | |
}); | |
return clazz; | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment