I'm using AngularJs 1.5. I am using ng-route to create a SPA. For 2
templates I have used the new Angular component as the template let's call
them 'employee' and 'company'. Within each route there is a sub component
that searches the company database call it companySrch. How the sub
component works is the companySrch component is loaded in either 'company'
or 'employee' when the user want's to search the company database within
either the employee or company. When the user selects a record in the
search results, the record is broadcast through a factory service with a
registered RXJS observable as follows:
companySrch.getRecordFn = function(result){
companySrch.output={id:result.cs_id, type:result.type,name:result.name,
active: result.active};
//BROADCAST RECORD SELECTED via companySrchService
companySrch.companySrchService.recordBroadcast(companySrch.output);
companySrch.navService.nav.navExt = false;
}
Then if the user is in employee or company template, that record is passed
through the template's subscription to the RXJS event. Example of snippets
from each controller of the employee and company respectively:
*EMPLOYEE CONTROLLER SNIPPET*
var employee = this;
employee.companySrchService = companySrchService;
employee.companySrchService.subscribe(function(obj) {
//GET COMPANY RECORD UPON BROADCAST
employee.submit[0].cs_name = obj.name;
employee.submit[0].cs_type = obj.type;
employee.submit[0].fk_cs_id = obj.id;
});
*COMPANY CONTROLLER SNIPPET*
var company = this;
company.companySrchService = companySrchService;
company.companySrchService.subscribe(function(obj) {
//GET COMPANY RECORD UPON BROADCAST
company.getRecordFn(obj.id);
});
The issue I have when I am in the employee template and a companySrch
record is broadcast the subscribed event also is fired in the 'company' on
the assumption I have previously loaded the company template. As such this
causing an error as the company component have been removed from the DOM
when the component was switched to employee as it still trying to run the
subscription function. The same thing happens when I do it in the reverse
(load employee, then company and perform companySrch).
Is there any way I can prevent the template that is not in view or removed
from the DOM from listening to the subrsiption broadcast??...I still want
the subject to be broadcasting. Or is there a way to break the component
reference to companySrchService when the respective component is destroyed
??
Here is my factory service too:
app.factory('companySrchService', ['$http', function($http){
var companySrchSubject = new Rx.ReplaySubject();//rxjs var to subsribed to
return{
//DEFINE SUBCRIBE SERVICE IN THIS CASE COMPANYSRCHSUBJECT
subscribe:function(subscription){
companySrchSubject.subscribe(subscription);
},
//BROADCAST WHEN RECORD SELECTED IN SEARCH
recordBroadcast:function(record){
companySrchSubject.onNext(record);
}
}
}])
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.