I'm trying to create a simple directive in Angular 2 so I can use jQuery UI Sortables on my page.
Currently I'm stuck at the point where I want to let the sortable directive know what function it should call on the page Component. I want this because you can register functions on events in the sortable plugin. I want the directive to call that function on the page's Component.
I currently have this:
@Directive({
selector: '[sortable]'
})
export class SortableDirective {
stopSortingEvent: any;
constructor(el: ElementRef) {
this.stopSortingEvent = el.nativeElement.getAttribute('stopSortingEvent');
if(this.stopSortingEvent) {
options['stop'] = this.stopSortingEvent; // This should be a reference to a function in the page Component.
}
$(el.nativeElement).sortable(options).disableSelection();
}
}
So the idea is that I create a HTML element with the sorting attribute on it. Including an attribute with stop-sorting-event="functionOnComponent()".
That should be a function on the current page's component. But obviously this it doesn't work like this. My directive doesn't know it has to try and call it on another component.
So how can I do this? Is there a way to get a reference to my page component?
Update
I think the suggestion of pixelbits is great, using the events with the @Output annotations. But, it seems tricky to get it work in combination with jQueryUI. It seems like jQueryUI can't read the property of my directive.
Here is a demo to get a better idea of what I mean: http://plnkr.co/edit/Hfc5vxuMLW6yQwr4qW2W?p=preview