Currently utilizing ionic platform, I've developed a unique "Notification" directive that is responsible for preprocessing to identify the type of notification ('alert', 'message' or 'important message') based on an object provided within an attribute. Within the template of this directive, there's another directive called "Update Notification" which gets invoked when the user slides the item to the left - triggering an update modal with a form whose model is passed via the attribute "notification."
The primary issue I'm encountering lies in the fact that the template of "Update Notification" triggers showUpdateNotification()
, but it appears to be invoking the scope functions of the "Notification" directive instead of its own defined scope.
I aim to establish the update functionality exclusively within the "Update Notification" directive so that it can be accessed elsewhere in the application with a different template. However, I'm currently grappling with resolving the scope conflict.
Your assistance is greatly appreciated!
Directive for Notification:
.directive("notification", function($rootScope, BibleService, FirebaseLoginService){
return{
restrict: 'AE',
scope:{
type: '@?type',
message: '=?message'
},
replace: true,
template: "<div ng-include='contentUrl'></div>",
link: function(scope, element, attrs){
scope.contentUrl = 'templates/directives/' + attrs.file + '.html';
attrs.$observe("file",function(v){
scope.contentUrl = 'templates/directives/' + v + '.html';
});
},
controller: function($scope, $rootScope){
//something interesting here.
$scope.hidden = false;
$scope.leaderImage = "";
$scope.notification = "";
$scope.canEdit = false;
if($scope.message){
$scope.leaderImage = $scope.message.leader.image;
if($scope.message.important){
$scope.type ="important"
}else{
$scope.type = "message"
}
if(FirebaseLoginService.loggedUser().id === $scope.message.leader.id){
$scope.canEdit = true;
}else{
$scope.canEdit = false;
};
}
if($scope.type === "alert" && $rootScope.pastDue){
$scope.title = "ATTENTION:"
$scope.notification = $rootScope.pastDueNotification;
}else if($scope.type === "message"){
if($scope.message){
$scope.title = $scope.message.title;
$scope.notification = $scope.message.text;
}
}else{
if($scope.message){
$scope.title = $scope.message.title;
$scope.notification = $scope.message.text;
}
}
$scope.closeNotification = function(){
$scope.hidden = true;
}
}
};
});
Template for Notification:
<ion-list show-delete="false" can-swipe="true">
<ion-item ng-if="type === 'alert'" ng-hide="hidden" class="item item-icon-left notification-alert item-text-wrap" ng-href="#/app/billing">
<i class="icon ion-alert-circled light"></i>
<h2 class="light"><b>{{title}}</b></h2><hr>
<p><span class="light"><b>{{notification}}</b></span></p>
<ion-option-button class="button-calm" ng-click="closeNotification()">
Close
</ion-option-button>
</ion-item>
<ion-item ng-if="type === 'important'" ng-hide="hidden" class="item item-icon-left notification-important item-text-wrap">
<i class="icon ion-android-hand royal"></i>
<div class="row">
<div class="col-80">
<h2 class="light"><b>Important Message: </b></h2>
<hr><p ng-if="title"><span class="royal"><b>{{title}}</b></span></p>
<p class="small"><span class="light"><b>{{notification}}</b></span></p>
{{message}}
</div>
<div class="col-20">
<img class="notification-thumb" ng-src="{{leaderImage}}" alt="">
</div>
</div>
<update-notification notification="message" file="updateNotificationOption"></update-notification>
<ion-option-button class="button-calm" ng-click="closeNotification()">
Close
</ion-option-button>
</ion-item>
<ion-item ng-if="type === 'message'" ng-hide="hidden" class="item item-icon-left notification-message item-text-wrap">
<i class="icon ion-paper-airplane dark"></i>
<div class="row">
<div class="col-80">
<h2>Message:</h2>
<hr><p ng-if="title"><b>{{title}}</b></p>
<p class="small">{{notification}}</p>
{{message}}
</div>
<div class="col-20">
<img class="notification-thumb" ng-src="{{leaderImage}}" alt="">
</div>
</div>
<update-notification notification="message" file="updateNotificationOption"></update-notification>
<ion-option-button class="button-calm" ng-click="closeNotification()">
Close
</ion-option-button>
</ion-item>
Directive for Update Notification:
.directive("updateNotification", function($rootScope, $localStorage, $timeout, $ionicListDelegate, ChurchService){
return{
restrict: 'E',
scope:{
notification: '='
},
replace: true,
template: "<div ng-include='contentUrl'></div>",
link: function(scope, element, attrs){
scope.contentUrl = 'templates/directives/' + attrs.file + '.html';
attrs.$observe("file",function(v){
scope.contentUrl = 'templates/directives/' + v + '.html';
});
},
controller: function($scope, $ionicModal, $location){
var ctrl = this;
$scope.notice = $scope.notification;
var church = {};
ChurchService.getbyLeader($localStorage.seedUser.leadershipID).then(function(ch){
church = ch;
});
$scope.updateNotification = function (data) {
$rootScope.show('Updating notification...');
if(church.notifications){
for (var i=0; i<church.notifications.length; i++){
var note = church.notifications[i];
if(note.date === data.date){
note = data;
}
}
}
ChurchService.update(church).then(function(){
$scope.closeUpdateNotification();
$location.path('/app/church/'+data.id);
$rootScope.hide();
}, ctrl.updateNotificationFailure);
};
ctrl.updateNotificationFailure = function(error) {
console.log('POST ERROR:', error);
$rootScope.notify("The Notification wasn't updated. Please try again later.")
};
$ionicModal.fromTemplateUrl('templates/updateNotification.html',{
scope: $scope
}).then(function(modal){
$scope.updateNotificationModal = modal;
});
// Open the update modal
$scope.showUpdateNotification = function() {
$scope.updateNotificationModal.show();
$ionicListDelegate.closeOptionButtons();
//console.log($scope.prayerObj);
};
// Triggered in the update modal to close it
$scope.closeUpdateNotification = function() {
$scope.updateNotificationModal.hide();
};
}
};
});
Template for Update Notification:
<ion-option-button class="button-balanced" ng-click="showUpdateNotification()">
Edit
</ion-option-button>
Visit this Plunker (apologies for the poor translation of Sass styling)