Last active
October 22, 2016 17:42
-
-
Save quieterkali/8e3d17b4c844b3da3a695ceca2ee8043 to your computer and use it in GitHub Desktop.
ASYNC
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
controllers.controller('ListaImoveisCtrl', function($rootScope, $scope, $ionicPlatform, $location, $timeout, $ionicLoading, $cordovaSQLite, $stateParams, imoveisService) { | |
$scope.$on('$ionicView.enter', function () { | |
imoveisService.getImoveis(imoveisService.formData).success(function (imoveis) { | |
if (!imoveis || !imoveis.length) { | |
throw new Error('Algo deu errado ao tentar carregar informações de imóveis.'); | |
} | |
$scope.imoveis = imoveis | |
if ($stateParams.idAtendimento == undefined) { | |
imoveis.forEach(function (imovel) { | |
imovel.atrelado = false | |
imovel.desatrelado = false | |
}) | |
} else { | |
var query = 'SELECT * FROM imovel_relacionado_atendimento WHERE id_atendimento IN (?)' | |
$cordovaSQLite.execute(brokersDB, query, [ | |
$scope.idAtendimento, | |
imoveis.map(function (i) { return "'" + i.id + "'" }).join(',') | |
]) | |
.then(function (res) { | |
if (!!res.rows && !!res.rows.length) { | |
res.rows.forEach(function (row) { | |
imoveis.forEach(function(imovel, index) { | |
if (row.id_imovel == imovel.id) { | |
imovel.desatrelado = true | |
imovel.atrelado = false | |
} | |
}) | |
}) | |
} | |
}); | |
} | |
$scope.imoveis = imoveis | |
}); | |
}); | |
$scope.goToImovel = function(imovel) { | |
$location.path('app/dados_imovel/' + imovel.id); | |
}; | |
$scope.voltarMenu = function() { | |
$location.path('app/inicio'); | |
}; | |
$scope.setBackground = function(imovel){ | |
if (imovel.foto != null) { | |
return imovel.foto; | |
} | |
imovel.foto = "img/no-image.png"; | |
return imovel.foto; | |
} | |
$scope.relacionamentoImovel = function(imovel) { | |
var query = "SELECT * FROM imovel_relacionado_atendimento WHERE id_atendimento = ? AND id_imovel = ?"; | |
$cordovaSQLite.execute(brokersDB, query, [$scope.idAtendimento, imovel.id]) | |
.then(function(res) { | |
console.log(res.rows.length); | |
if (res.rows.length < 1) { | |
var query2 = "INSERT INTO imovel_relacionado_atendimento (id_atendimento, id_imovel) VALUES (?,?)"; | |
$cordovaSQLite.execute(brokersDB, query2, [$rootScope.idAtendimento, imovel.id]) | |
.then(function(res2) { | |
$scope.imoveis[i].desatrelado = true; | |
$scope.imoveis[i].atrelado = false; | |
console.log(res2); | |
}); | |
} | |
}); | |
} | |
$scope.tirarRelacionamentoImovel = function(imovel) { | |
var query = "SELECT * FROM imovel_relacionado_atendimento WHERE id_atendimento = ? AND id_imovel = ?"; | |
$cordovaSQLite.execute(brokersDB, query, [$scope.idAtendimento, imovel.id]) | |
.then(function(res) { | |
console.log(res.rows.length); | |
if (res.rows.length < 1) { | |
var query2 = "DELETE FROM imovel_relacionado_atendimento WHERE id_atendimento = ? AND id_imovel = ?"; | |
$cordovaSQLite.execute(brokersDB, query2, [$rootScope.idAtendimento, imovel.id]) | |
.then(function(res2) { | |
$scope.imoveis[i].desatrelado = false; | |
$scope.imoveis[i].atrelado = true; | |
console.log(res2); | |
}); | |
} | |
}); | |
} | |
}); | |
$scope.$on('$ionicView.enter', function() { | |
imoveisService.getImoveis(imoveisService.formData) | |
.success(function(data){ | |
$scope.imoveis = data; | |
if ($stateParams.idAtendimento == undefined || $stateParams.idAtendimento == "") { | |
$scope.imoveis.forEach(function (imovel) { | |
imovel.atrelado = false | |
imovel.desatrelado = false | |
}) | |
}else { | |
$scope.imoveis.forEach(function (imovel) { | |
imovel.atrelado = true; | |
imovel.desatrelado = false; | |
}); | |
var query = "SELECT * FROM imovel_relacionado_atendimento WHERE id_atendimento = ?"; | |
$cordovaSQLite.execute(brokersDB, query, [$stateParams.idAtendimento]) | |
.then(function (res) { | |
if (!!res.rows && !!res.rows.length) { | |
console.log(!!res.rows); | |
res.rows.forEach(function (row) { | |
$scope.imoveis.forEach(function(imovel, index) { | |
if (row.id_imovel == imovel.id) { | |
imovel.desatrelado = true | |
imovel.atrelado = false | |
} | |
}) | |
}) | |
} | |
}); | |
// .then(function(res){ | |
// if (res.rows.length > 0) { | |
// for (var i = 0; i < res.rows.length; i++) { | |
// for (var j = 0; j < $scope.imoveis.length; j++) { | |
// if (res.rows[j].id_imovel == $scope.imoveis[i].id){ | |
// $scope.imoveis[i].desatrelado = true; | |
// $scope.imoveis[i].atrelado = false; | |
// break; | |
// } | |
// } | |
// } | |
// } | |
// }); | |
} | |
}); | |
}); | |
controllers.controller('ListaImoveisCtrl', function($rootScope, $scope, $ionicPlatform, $location, $timeout, $ionicLoading, $cordovaSQLite, $stateParams, imoveisService) { | |
// Acesso ao servico de imoveis | |
$scope.$on('$ionicView.enter', function() { | |
imoveisService.getImoveis(imoveisService.formData) | |
.success(function(data){ | |
$scope.imoveis = data; | |
if ($stateParams.idAtendimento == undefined || $stateParams.idAtendimento == "") { | |
$scope.imoveis.forEach(function (imovel) { | |
imovel.atrelado = false | |
imovel.desatrelado = false | |
}) | |
}else { | |
$scope.imoveis.forEach(function (imovel) { | |
imovel.atrelado = true; | |
imovel.desatrelado = false; | |
}); | |
var query = "SELECT * FROM imovel_relacionado_atendimento WHERE id_atendimento = ?"; | |
$cordovaSQLite.execute(brokersDB, query, [$stateParams.idAtendimento]) | |
.then(function(res){ | |
if (res.rows.length > 0) { | |
for (var i = 0; i < res.rows.length; i++) { | |
for (var j = 0; j < $scope.imoveis.length; j++) { | |
if (res.rows[j].id_imovel == $scope.imoveis[i].id){ | |
$scope.imoveis[i].desatrelado = true; | |
$scope.imoveis[i].atrelado = false; | |
break; | |
} | |
} | |
} | |
} | |
}); | |
} | |
}); | |
}); | |
$scope.goToImovel = function(imovel) { | |
if ($stateParams.idAtendimento != undefined && $stateParams.idAtendimento != "") { | |
$location.path('app/dados_imovel/' + imovel.id + '/' + $stateParams.idAtendimento); | |
}else { | |
$location.path('app/dados_imovel/' + imovel.id); | |
} | |
}; | |
$scope.voltarMenu = function() { | |
$location.path('app/inicio'); | |
}; | |
$scope.setBackground = function(imovel){ | |
if (imovel.foto != null) { | |
return imovel.foto; | |
} | |
imovel.foto = "img/no-image.png"; | |
return imovel.foto; | |
} | |
$scope.relacionamentoImovel = function(imovel) { | |
var query = "SELECT * FROM imovel_relacionado_atendimento WHERE id_atendimento = ? AND id_imovel = ?"; | |
$cordovaSQLite.execute(brokersDB, query, [$stateParams.idAtendimento, imovel.id]) | |
.then(function(res) { | |
console.log(res.rows.length); | |
if (res.rows.length < 1) { | |
var query2 = "INSERT INTO imovel_relacionado_atendimento (id_atendimento, id_imovel) VALUES (?,?)"; | |
$cordovaSQLite.execute(brokersDB, query2, [$stateParams.idAtendimento, imovel.id]) | |
.then(function(res2) { | |
imovel.desatrelado = true; | |
imovel.atrelado = false; | |
console.log(res2); | |
}); | |
} | |
}); | |
} | |
$scope.tirarRelacionamentoImovel = function(imovel) { | |
var query = "SELECT * FROM imovel_relacionado_atendimento WHERE id_atendimento = ? AND id_imovel = ?"; | |
$cordovaSQLite.execute(brokersDB, query, [$stateParams.idAtendimento, imovel.id]) | |
.then(function(res) { | |
if (res.rows.length < 1) { | |
var query2 = "DELETE FROM imovel_relacionado_atendimento WHERE id_atendimento = ? AND id_imovel = ?"; | |
$cordovaSQLite.execute(brokersDB, query2, [$stateParams.idAtendimento, imovel.id]) | |
.then(function(res2) { | |
$scope.imoveis[i].desatrelado = false; | |
$scope.imoveis[i].atrelado = true; | |
console.log(res2); | |
}); | |
} | |
}); | |
} | |
$scope.redirectToImovelList = function(){ | |
if ($stateParams.idAtendimento != undefined && $stateParams.idAtendimento != "") { | |
$location.path('app/filtro_imovel/' + $stateParams.idAtendimento); | |
}else { | |
$location.path('app/filtro_imovel/'); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment