Basically i have a factory
angular.module('app').factory('gService',gService);
function gService($filter, $window) {
function confirmDialog(message, success, fail) {
var confirmMessage = navigator.notification.confirm(
message,
onConfirm,
'',
[$filter('translate')('OK'), $filter('translate')('CANCEL')]
);
function onConfirm(index) {
return index === 1 ? success() : fail();
}
return confirmMessage;
}
}
I want to check condition outside this factory, if the functions are executed or not
if(gService.confirmDialog.onConfirm){
}
this does not work. How do i check function which is executed in angular?
6 Answers 6
EMIT & BROADCAST
If you check onConfirm event that it will controll is onConfirm function defined on gService.confirmDialog object where the statement wroten. It is not async and promised job.
if(gService.confirmDialog.onConfirm){
}
You need to notify your listeners first. After that listen that event to do your job.
You can broadcast or emit an event to scopes that waiting for onConfirm event.
angular.module('app').factory('gService',gService);
function gService($rootScope, $filter, $window) {
function confirmDialog(message, success, fail) {
var confirmMessage = navigator.notification.confirm(
message,
onConfirm,
'',
[$filter('translate')('OK'), $filter('translate')('CANCEL')]
);
function onConfirm(index) {
var result = index === 1 ? success() : fail();
$rootScope.$emit('onConfirm', result);
//or
//$rootScope.$broadcast('onConfirm', result); -> this goes downwards to all child scopes. Emit is upwarded.
}
return confirmMessage;
}
}
After that you should check if onConfirm event is triggered. This does the controll you need.
function onConfirmFunction( result ){ //You will get the success or fail methods result here... };
$rootScope.$on('onConfirm', onConfirmFunction);
Comments
Why don't you do this.
angular.module('app').factory('gService',gService);
function gService($filter, $window) {
var obj = {
confirmDialog : confirmDialog,
onConfirm : onConfirm,
}
obj.executed = false;
function confirmDialog(message, success, fail) {
var confirmMessage = navigator.notification.confirm(
message,
onConfirm,
'',
[$filter('translate')('OK'), $filter('translate')('CANCEL')]
);
function onConfirm(index) {
if(index===1){
//I dont know you want function or returned value of function but you can use success() as well
obj.executed = success;
return success();
}else{
obj.executed = fail;
return fail();
}
}
return confirmMessage;
}
return obj;
}
you can check now..
if(gService.executed){
}
and I dont know if you forgot but you haven't returned anything from factory.
Comments
Try the following:
angular.module('app').factory('gService', gService);
function gService($filter, $window) {
var observers = [];
function observeNotify(callback) {
observers.push(callback);
}
function confirmDialog(message, success, fail) {
var confirmMessage = navigator.notification.confirm(
message,
onConfirm,
'',
[$filter('translate')('OK'), $filter('translate')('CANCEL')],
);
function onConfirm(index) {
var condition = index === 1;
observers.forEach(function(observer) {
observer(condition);
});
return condition ? success() : fail();
}
return confirmMessage;
}
}
//Outside of the factory
gService.observeNotify(function(condition){
console.log(condition);
});
This way you can register functions, that will be called during the execution of the onConfirm function
Comments
Is this what you are expecting? I am not sure what is the problem. Can you explain more detail?
<!doctype html>
<html lang="en" ng-app="app">
<head>
<meta charset="utf-8">
<title>How AngularJS Works?</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js"></script>
</head>
<body ng-controller="appCtrl">
<p>Welcome to BooKart, we have collection of {{1 + ' Million'}} books.</p>
<script>
var app = angular.module('app', []);
app.factory('gService', gService);
function gService($filter, $window) {
function confirmDialog(message, success, fail) {
alert("called");
var confirmMessage = navigator.notification.confirm(
message,
onConfirm,
'', [$filter('translate')('OK'), $filter('translate')('CANCEL')]
);
function onConfirm(index) {
return index === 1 ? success() : fail();
}
return confirmMessage;
}
return {
confirmDialog:confirmDialog
}
}
app.controller("appCtrl", function ($scope, gService) {
$scope.callFun = function () {
if (gService.confirmDialog("", "","").onConfirm) {
}
};
$scope.callFun();
});
</script>
</body>
</html>
2 Comments
gService.confirmDialog.onConfirm i'm not able to check.. This is what i'm looking for.Convert onComfirm to a promise and do something after the promise is resolved.
function onConfirm(index) {
return $q(function (resolve, reject) {
index === 1 ? resolve(true) : reject(false);
});
}
$scope.callFun = function () {
gService.confirmDialog("", "","").onConfirm()
.then(function(sucess) {
successMethod();
}, function(error) {
console.error('oh no');
});
Comments
basically you are not returning (reference to the code you shared) anything from the function gService (implementation of your factory), since its a factory you have to return something. your inner function confirmDialog return an object, but the outer function gServive dosen't returns anything. The way you want to execute it, you have to return either confirmDialog callback and have to execute in the injecting end like
if(gServive(<..args...>).onConfirm())
and in case toy are returning a object then
return {confirmDialog: confirmDialog(...<args>...)}
and then in the injecting end
if(gService.confirmDialog.onConfirm)
Note: That your onConfirm is a function, by calling it, it will return something, so it has to ve invoked with (...), it is actually sounds like an event attacher function which will take a callback and fires on complete, but your code dosen't says that. in that case you can maintain a promise, and in onConfirm call take a callback as argument, and then pass it to then of that promise, and whenever you filled its time to confirm, just resolve that promise.
Comments
Explore related questions
See similar questions with these tags.
gService.confirmDialog.onConfirm