Thank you for your guidance Eric. I am still a bit confused though.
To my understanding, the .toRx().subscribe( ... ) function is meant to RECEIVE messages and the .next() function is meant to BROADCAST messages
In this plnkr (
http://plnkr.co/edit/MT3xOB?p=info ) , you invoke the .toRx().subscribe( ... ) function from a
data object that seems to be defined/derived originally from the template:
@Component({
selector : 'child-cmp',
template : '',
inputs : ['data']
})
class ChildCmp {
afterViewInit() {
this.data.toRx().subscribe((data) => {
console.log('New data has arrived!', data);
});
}
}
In this plnkr (
http://plnkr.co/edit/rNdInA?p=preview ) , you invoke the .toRx().subscribe( ... ) function from an
evt object and its
emitter function (originating from Service injected into the component's constructor)
@Component({
selector : 'parent-cmp',
template : ''
})
class ParentCmp {
constructor(evt: EventService) {
evt.emitter.subscribe((data) =>
console.log("I'm the parent cmp and I got this data", data));
}
}
Is is possible for the BROADCAST to take place in a function of the Service itself while at the same time, is it possible for the Component to RECEIVE the message without relying upon a returned Service object or Template data object to chain its .toRX().subscribe( ... ) function invokation?
import {Injectable, EventEmitter} from 'angular2/angular2';
@Injectable()
export class DataService {
items:Array<any>;
dispatcher: EventEmitter = new EventEmitter();
constructor() {
this.items = [
{ name: 'AAAA' },
{ name: 'BBBB' },
{ name: 'CCCC' }
];
}
getItems() {
return this.items;
}
sendItems() {
this.dispatcher.next( this.items );
}
}
export var DATA_BINDINGS: Array<any> = [
DataService
];
@Component({
selector: 'rabble'
})
@View({
...
})
export class Rabble {
items : Array<any>;
constructor( public dataService : DataService) {
console.log('this.routeParam', this.dataService.getItems());
}
afterViewInit() {
this.data.toRx().subscribe((data) => {
console.log('New data has arrived!', data);
});
}
handleClick() {
this.dataService.sendItems();
}
}
Also, what is the correct EventEmitter syntax to use to subscribe to a particular "channel" as we did in angular 1 $Broadcast and $on?
$scope.$broadcast('myCustomEvent', {
someProp: 'Sending you an Object!' // send whatever you want
});
$scope.$on('myCustomEvent', function (event, data) {
console.log(data); // 'Data to send'
});