Skip to content

Instantly share code, notes, and snippets.

@suissa
Created March 2, 2017 15:47
Show Gist options
  • Save suissa/c327ef53041994e7cb496f98977dec0b to your computer and use it in GitHub Desktop.
Save suissa/c327ef53041994e7cb496f98977dec0b to your computer and use it in GitHub Desktop.
Exemplo de uma função em um controller em ng 1.6
function ListController ( Service, $mdDialog ) {
const vm = this;
vm.list = true;
vm.MODULE_NAME = MODULE_NAME;
vm.PATH = MODULE_NAME.toLowerCase();
const hiddenFields= [
'active'
, 'password'
, 'automaticLogin'
, '__v'
, 'created_at'
, '$$hashKey'
]
const toObject = ( acc, cur ) => Object.assign( {}, acc, cur )
const toFinalObject = ( arr ) => arr.reduce ( toObject, {} )
const removeFieldsFromSchema = ( hiddenFields ) =>
( obj ) => ! hiddenFields.includes( obj.name )
const removeHiddenFields = ( hiddenFields ) => ( obj ) => {
const alloweds = ( field ) => ! hiddenFields.includes( field )
const allowedFields = Object.keys( obj ).filter( alloweds )
return allowedFields.map( field => ( { [field] : obj[field] } ))
}
const success = ( list ) =>
vm.list = list.data
.map( removeHiddenFields( hiddenFields ) )
.map( toFinalObject )
const successSchema = ( caught ) => {
vm.SCHEMA = caught.data
.filter( removeFieldsFromSchema( hiddenFields ) )
return Service.list()
}
Service.getSchema()
.then( successSchema )
.then( success )
.catch( error )
vm.editDialog = function ( ev, obj ) {
ev.preventDefault();
$mdDialog.show({
controller: EditDialogController,
controllerAs: 'vm',
templateUrl: 'app/modules/User/views/form.html',
parent: angular.element(document.body),
targetEvent: ev,
clickOutsideToClose: true,
fullscreen: false,// Only for -xs, -sm breakpoints.
locals: { obj }
})
.then(function(answer) {
console.log('then', answer);
}, function() {
console.log('finish')
});
};
}
ListController.$inject = [`${MODULE_NAME}Service`, `$mdDialog`];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment