4

I read the documentation but found nothing related to setting parameters in dataSource urls. Is it possible to achieve that?

Thx in advance.

OnaBai
  • 40,767
  • 6
  • 96
  • 125
Isilmë O.
  • 1,668
  • 3
  • 23
  • 35

3 Answers3

13

Yes, it is possible. The urls defined in the DataSource.transport might be a function. This function receives (for update) as first argument the data being updated (the model) and returns the string that should be used as URL.

Composing the URL for what you want to do is:

var ds = new kendo.data.DataSource({
    transport: {
        read: {
            url: function () {
                return 'read';
            }
        },
        update: {
            url : function (item) {
                return 'update/' + item.id;
            }
        }
    }
});
OnaBai
  • 40,767
  • 6
  • 96
  • 125
  • Tested. Correct answer and very concise. Thanks. :) – Isilmë O. Aug 28 '14 at 22:56
  • Indeed, this works great but only if `batch=false` in your datasource (which is by default). When you enable batch operations, the `item` argument will be a collection of models - which'll not map to a single RESTful service. – Juliën Dec 08 '14 at 13:09
  • 1
    Can you explain this bit more. I did not get, how I pass the 'item' to this function. – NoughT Jul 15 '15 at 03:52
  • If you're having a problem where item.id is undefined, try adding `id: "id"` to your Model. – kleptog May 31 '16 at 11:36
0

The answer seems to be vague on 'item.'

Just note that 'item' is an object. In fact anything passed in to read has to be an object, that's what Kendo expects. If you pass anything else into read, like a string, it will convert it into an object which isn't what you want. So, the solution is as follows:

    _viewModel: kendo.observable({
        items: new kendo.data.DataSource({
            transport: {
                read: {
                    url: function (args) {
                        var urlParm = '?take=' + 1 + '&skip=0&page=1&pageSize=' + 1;
                        return CGI_ISD._base + 'api/executionsummary/executiondetails/' + args.msgId + urlParm;
                    },
                    dataType: "json"
                },
            },
            schema: {
                data: function (response) {
                    return response.AggregateData.Data;
                }
            }
        }),
    }),
    _reload: function (msgId) {
        this._viewModel.items.read({msgId: msgId});
    }
spyder1329
  • 656
  • 5
  • 15
-1

Short answer:

Nope.

Long answer:

Parameters are passed either inline with the url parameter of the transport object...

var id = 'abc123';

var ds = new kendo.data.DataSource({
  transport: {
    read: {
      url: 'api/employees?id=' + id
    }
  }
});

...or they are passed in the data parameter of the transport object.

var id = 'abc123';

var ds = new kendo.data.DataSource({
  transport: {
    read: {
      url: 'api/employees',
      data: {
        id: id;
      }
    }
  }
});

or

var id = 'abc123';

var ds = new kendo.data.DataSource({
  transport: {
    read: {
      url: 'api/employees',
      data: function () {
        return { id : id };
      }
    }
  }
});
Brett
  • 4,268
  • 1
  • 13
  • 28