Custom DataSource transport

3 posts, 1 answers
  1. Marcin
    Marcin avatar
    31 posts
    Member since:
    Jul 2013

    Posted 18 Dec 2015 Link to this post

    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. Answer
    Alex Gyoshev
    Admin
    Alex Gyoshev avatar
    2500 posts

    Posted 22 Dec 2015 Link to this post

    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!
     
  3. Kendo UI is VS 2017 Ready
  4. Marcin
    Marcin avatar
    31 posts
    Member since:
    Jul 2013

    Posted 22 Dec 2015 in reply to Alex Gyoshev Link to this post

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

     

    Best regards

    Marcin Danek

Back to Top