Last active
May 26, 2020 20:55
-
-
Save alexalannunes/21c4de546ccb5552a0fde8e1682c2aa7 to your computer and use it in GitHub Desktop.
Tratar model (angularjs) no qual os campos precisam ser enviados, mesmo sento nulls ou [ ]. Ideal para métodos GET ou DELETE
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 _treat_model_form() { | |
let model = angular.copy(vm.filter); | |
for (let m in model) { | |
const value = model[m]; | |
model[m] = | |
value == null || value.length == 0 | |
? "" | |
: Array.isArray(value) | |
? value.join(",") // para metodos GET|DELETE | |
: value; | |
} | |
return model; | |
} | |
// or | |
function _treat_model() { | |
let model = angular.copy(vm.filter); | |
for (let key in model) { | |
model[key] = model[key] ? model[key] : ""; | |
} | |
return model; | |
} | |
// exemplo: | |
// model = { id: 1, services: [], channels: null, users: [1,2,3], status: ['started, 'ended'] } | |
// _treat_model_form() | |
// { id: 1, services: '', channels: '', users: '1,2,3', status: 'started,ended' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment