Aventadorjs Sample App





# First Name Last Name Email

                                (function (window, aventador) {

                                    aventador
                                        .module('myApp')
                                        .handler('UsersHandler', UsersHandler)

                                    function UsersHandler(UsersFactory, UsersService, UsersUtility) {
                                        return {
                                            getUsers: getUsers,
                                            register: register
                                        }

                                        function register(data, callback) {
                                            var callback = callback || false,
                                                errors = UsersUtility.validateUser(data),
                                                response = {
                                                    success: false,
                                                    errors: errors,
                                                    data: {}
                                                }

                                            if (!response.errors.length) {
                                                var user = UsersFactory.create(
                                                    data.firstname,
                                                    data.lastname,
                                                    data.email,
                                                    data.password
                                                )

                                                response.data.user = UsersService.register(user)
                                                response.success = Boolean(response.data.user)
                                            }

                                            if (callback) callback(response)
                                        }

                                        function getUsers() {
                                            return UsersService.getUsers()
                                        }
                                    }

                                })(window, aventador)
                            
                                (function (window, aventador) {

                                    aventador
                                        .module('myApp')
                                        .service('UsersService', UsersService)

                                    function UsersService() {

                                        // NOTE: data should be saved in a database. this is just temporary
                                        var users = {}

                                        return {
                                            getUsers: getUsers,
                                            register: register
                                        }

                                        function getUsers() {
                                            return users
                                        }

                                        function register(user) {
                                            return users[user.getId()] = user
                                        }
                                    }

                                })(window, aventador)
                            
                                (function (window, aventador) {

                                    aventador
                                        .module('myApp')
                                        .factory('UsersFactory', UsersFactory)

                                    function UsersFactory() {
                                        return {
                                            create: create
                                        }

                                        function user(user) {
                                            var user = {
                                                id: Math.floor((Math.random() * 1000) + 1) + '' + user.firstName + ''+ user.lastName,
                                                firstName: user.firstName || '',
                                                lastName: user.lastName || '',
                                                email: user.email || '',
                                                password: user.password || ''
                                            }

                                            this.getId = function() {
                                                return user.id
                                            }

                                            this.getFirstName = function() {
                                                return user.firstName
                                            }

                                            this.getLastName = function() {
                                                return user.lastName
                                            }

                                            this.getEmail = function() {
                                                return user.email
                                            }

                                            this.getPassword = function() {
                                                return user.password
                                            }

                                            return this
                                        }

                                        function create(firstName, lastName, email, password) {
                                            return user({
                                                firstName: firstName,
                                                lastName: lastName,
                                                email: email,
                                                password: password
                                            })
                                        }
                                    }

                                })(window, aventador);
                            
                                (function () {

                                    aventador
                                        .module('myApp')
                                        .utility('StringUtility', StringUtility)

                                    function StringUtility() {
                                        return {
                                            isString: isString,
                                            isEmpty: isEmpty,
                                            queryStringToJson: queryStringToJson
                                        }

                                        function isString(data) {
                                            return typeof data === 'string'
                                        }

                                        function isEmpty(data) {
                                            return isString(data) && data.length < 1
                                        }

                                        function queryStringToJson(queryString) {
                                            return $.parseJSON('{"' + queryString.replace(/&/g, '","').replace(/=/g, '":"') + '"}');
                                        }
                                    }

                                })()
                            
                                (function () {

                                    aventador
                                        .module('myApp')
                                        .utility('UsersUtility', UsersUtility)

                                    function UsersUtility(StringUtility) {
                                        return {
                                            validateUser: validateUser
                                        }

                                        function validateUser(user) {
                                            var errors = [];

                                            if (StringUtility.isEmpty(user.firstname)) {
                                                errors.push('first name must not be empty.')
                                            }

                                            if (!StringUtility.isString(user.firstname)) {
                                                errors.push('first name must be a string.')
                                            }

                                            if (StringUtility.isEmpty(user.lastname)) {
                                                errors.push('last name must not be empty.')
                                            }

                                            if (!StringUtility.isString(user.lastname)) {
                                                errors.push('last name must be a string.')
                                            }

                                            if (StringUtility.isEmpty(user.password)) {
                                                errors.push('password must not be empty.')
                                            } else {
                                                if (StringUtility.isEmpty(user.passwordconfirm)) {
                                                    errors.push('confirm password must not be empty.')
                                                } else if(user.passwordconfirm !== user.password) {
                                                    errors.push('password does not match.')
                                                }
                                            }

                                            return errors
                                        }
                                    }

                                })()
                            
                                (function () {

                                    var myApp = aventador.module('myApp'), // create app
                                        $page = $('#users-page'),
                                        $registrationForm = $page.find('form#registration-form'),
                                        $usersTable = $page.find('table.users-table tbody'),
                                        $errors = $page.find('.errors')
                                        $errorList = $errors.find('ul.error-list')

                                    /*===== EVENT HANDLERS =====*/

                                    $registrationForm.on('submit', function (e) {
                                        e.preventDefault()

                                        var $this = $(this),
                                            StringUtility = myApp.getUtility('StringUtility'),
                                            UsersHandler = myApp.getHandler('UsersHandler'),
                                            data = StringUtility.queryStringToJson($this.serialize())

                                        UsersHandler.register(data, function(response) {
                                            if (response.success) {
                                                $.publish('users/add', [response.data.user])
                                            } else if (response.errors.length) {
                                                $.publish('users/add/errors', [response.errors])
                                            }
                                        })
                                    })


                                    /*===== PUB/SUB =====*/

                                    function addUser(_, user) {
                                        // Skip the first argument (event object) but log the name and other args.
                                        $errors.hide()
                                        $errorList.html('')

                                        $usersTable.append(
                                                '' +
                                                                '' + user.getId() + '' +
                                                                '' + user.getFirstName() + '' +
                                                                '' + user.getLastName() + '' +
                                                                '' + user.getEmail() + '' +
                                                                ''
                                            )
                                    }

                                    function addUserErrors(_, errors) {
                                        $errors.show()
                                        $errorList.html('')

                                        errors.forEach(function(value, index) {
                                            $errorList.append('
  • ' + value + '
  • ') }) } $.subscribe('users/add', addUser); $.subscribe('users/add/errors', addUserErrors); })()