Last active
June 9, 2016 15:33
-
-
Save blogui91/c7c32fb3e0dfd5a548a0ce65d511179f to your computer and use it in GitHub Desktop.
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
var required_fields = ['place','start_date','end_date','description']; | |
var message_errors = { | |
place : 'Place is required', | |
start_date : 'start_date required', | |
end_date : 'end_date is required', | |
description : 'description is required', | |
invalid_dates : 'Start date has to be less than end date' | |
}; | |
var errors = []; | |
class Event{ | |
constructor(new_event){ | |
//Default values | |
this.attributes = { | |
id : null, | |
place : null, | |
description : null, | |
guests : [], | |
start_date : null, | |
end_date : null | |
} | |
//If we are loading data on create instance object | |
if(new_event){ | |
this.attributes = { | |
id : new_event.id? new_event.id : null, | |
place : new_event.place? new_event.place : null, | |
description : new_event.description? new_event.description : null, | |
guests : new_event.guests? new_event.guests : [], | |
start_date : new_event.start_date? new_event.start_date : null, | |
end_date : new_event.end_date? new_event.end_date : null | |
} | |
} | |
} | |
//Validators | |
validForCreation(){ | |
var model = this.attributes; | |
errors = []; //Remove message errors | |
for(var f = 0; f < required_fields.length ;f++){ | |
var value = required_fields[f]; // Required field | |
var form_attr = this.attributes[value] //Model property | |
if(!(form_attr)){ | |
errors.push(message_errors[value]); | |
} | |
} | |
//If you want to validate end_date > to start_date validation | |
model.start_date > model.end_date ? errors.push(message_errors['invalid_dates']) : model.start_date; | |
return errors.length === 0? true : false; | |
} | |
showErrors(){ | |
return errors; | |
} | |
//Create setters and getters | |
set id(id){ | |
this.attributes.id = id; | |
} | |
get id(){ | |
return this.attributes.id; | |
} | |
set place(place){ | |
this.attributes.place = place; | |
} | |
get place(){ | |
return this.attributes.place; | |
} | |
set description(description){ | |
this.attributes.description = description; | |
} | |
get description(){ | |
return this.attributes.description; | |
} | |
set guests(guests){ | |
this.attributes.guests = guests; | |
} | |
get guests(){ | |
return this.attributes.guests; | |
} | |
set start_date(start_date){ | |
this.attributes.start_date = start_date; | |
} | |
get start_date(){ | |
return this.attributes.start_date; | |
} | |
set end_date(end_date){ | |
this.attributes.end_date = end_date; | |
} | |
get end_date(){ | |
return this.attributes.end_date; | |
} | |
} | |
export | |
default Event |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment