This is a migrated thread and some comments may be shown as answers.

Custom DataSource transport

2 Answers 169 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Marcin
Top achievements
Rank 1
Marcin asked on 18 Dec 2015, 09:19 AM

Hi,

 I'm trying to load data using custom read function in DataSource transport. I have to use the angular service due to usage of angular $http interceptors providing the user authentication mechanism.

 The problem start when I try to use the controller's "MyService" field in the read function, as shown bellow:

 export class CountersController {

constructor(private $scope: ng.IScope,
            private $q: ng.IQService,
            myService: LonginusCountersApp.Services.IMyService) {
            this.MyService = myService;
}

    private MyService: LonginusCountersApp.Services.IMyService;

public counterGroups = new kendo.data.HierarchicalDataSource({
            transport: {
                read: function (options) {
                    this.MyService.GetCounters().then((data: ng.IHttpPromiseCallbackArg<{}>) => {});
}
}
});
}

 

The problem is, that on Runtime i get error, that I try to call GetCountes() on undefined or null object (MyService).

It there any way to pass the service reference to the read function? 

I followed the response to the thread: http://www.telerik.com/forums/unable-make-treeview-work-with-angulajs-service

but it seems not to work, or at least not to work with TypeScript.

Do You have any idea, how to achieve this?

 

Best regards

Marcin Danek

2 Answers, 1 is accepted

Sort by
0
Accepted
Alex Gyoshev
Telerik team
answered on 22 Dec 2015, 08:28 AM

Hello Marcin,

You can bind the context of the read handler, so that it has a reference to the service:

      read: (function (options) {
          this.MyService.GetCounters().then((data: ng.IHttpPromiseCallbackArg<{}>) => {});
      }).bind(this)

Make sure that you call options.success / options.error when the promise is resolved / rejected, so that the DataSource is notified of the change.

Regards,
Alex Gyoshev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Marcin
Top achievements
Rank 1
answered on 22 Dec 2015, 09:39 AM

Thank You very much. That was, what I was looking for.

 

Best regards

Marcin Danek

Tags
TreeView
Asked by
Marcin
Top achievements
Rank 1
Answers by
Alex Gyoshev
Telerik team
Marcin
Top achievements
Rank 1
Share this question
or