0

My data source:

 var asGridDataSource = new kendo.data.DataSource({
    transport: {
        read: {
            url: function (options) {
                DataService.Securities()
                    .done(function (secs) {
                        // notify the data source that the request succeeded
                        options.success(secs);
                    })
                    .fail(function (secs, testStatus, err) {
                        // notify the data source that the request failed
                        options.error(stats);
                    });
            },
            data: function () {
                return {
                    id: $($searchCriteria).val(),
                    searchBy: $(searchByDDL).val(), 
                };
            }
        }
        ...

And the DataServices.Securities function:

    Securities = function () {
    return $.ajax({
        url: "/api/securities"
    });
};

If I replace the read url function with a simple url string (url: "/api/securities"), the additional data is added properly. However, when I do it as above, I get two Http requests:

/api/securities
/?id=5&searchBy=3

instead of a single request that should look like this:

/api/securities?id=5&searchBy=3

The only Kendo documentation I can find has an example of how to add data like this using the url string as described above, but not when using a function. Why am I not getting a single request?


Edit:

After working with this a bit more, I think I understand. I started out with this:

var asGridDataSource = new kendo.data.DataSource({
    transport: {
        read: {
            url: "/api/securities",
            data: function () {
                return {
                    id: $($searchCriteria).val(),
                    searchBy: $(searchByDDL).val()
                };
            }
        }
     }
});

Doing this, and only this, results in a request like this: /api/securities?id=6&searchBy=3. I wanted to modify the above to use a function for the url instead of a hardcoded string. This is what the very top part of the original question is an attempt to do. However, the specification of the data function does not work. In the hardcoded url example, Kendo knows to get the id and searchBy from the page and append it to the url. But it has no way of knowing what to do with that same data when I use a function for the url. Or this is my guess.

Scott
  • 2,456
  • 3
  • 32
  • 54

1 Answers1

0

I think what you want to be doing is make transport.read a function because you want to manually retrieve the data, not transport.read.url, which is for returning a url string when using the built-in transport:

transport: {
    read: function (options) {
        var params = {
            id: $($searchCriteria).val(),
            searchBy: $(searchByDDL).val(),
        };
        DataService.Securities(params)
            .done(function (secs) {
            // notify the data source that the request succeeded
            options.success(secs);
        })
            .fail(function (secs, testStatus, err) {
            // notify the data source that the request failed
            options.error(stats);
        });
    }
}

To add optional parameters to the Securities method, you could do something along the lines of this:

DataService.Securities = function (params) {
    var options = {
        url: "/api/securities"
    };

    if (params) {
        options.data = params;
    }

    return $.ajax(options);
};
Lars Höppner
  • 18,252
  • 2
  • 45
  • 73
  • Doing it this way, I don't even get the query string params. I must say I thought you had it though. I tried it this way using parameterMap instead of data and got the same results. – Scott May 30 '14 at 18:15
  • if you want to send parameters, you need to add them in your DataService.Securities method; the data method only works if you used the built-in transport; check the data param in the documentation: http://api.jquery.com/jquery.ajax/ – Lars Höppner May 30 '14 at 18:17
  • Well, yes, I do want to send params. That's the whole point. To be able to add them when needed, as here, but to be able to use the DataService.Securities without any whenever that may be desired. – Scott May 30 '14 at 18:33
  • create an optional parameter to DataService.Securities and then send them using the ajax call when they're passed – Lars Höppner May 30 '14 at 18:41
  • Yes, but I want a standalone set of ajax calls that know the specific uri needed for securities, but don't need to know anything else. The idea is that any app could include this service, but wouldn't be saddled with methods that they will never use, they simply add data for, say, a certain security if that's what is needed or specify nothing at all if they want everything. – Scott May 30 '14 at 21:31
  • That's why I said to make it an optional parameter. I added a code example; the request it returns would send parameters or not, depending on whether you pass something in. If that doesn't solve the problem, then I probably don't understand what you're trying to do. – Lars Höppner May 30 '14 at 21:46
  • I essentially want to do what the code example does, I just didn't want to do it that way. Again, if I use url: "/api/securities" along with data: it works, but if I use the same data with a method call, the data doesn't get added to "/api/securities". Maybe it's just not possible. – Scott Jun 01 '14 at 06:16
  • I thought you wanted a method that makes an ajax request to a URL and can either make that request with url params or without, depending on what the caller needs. That's what you have in my example: you can call DataService.Securities() to make a plain request, or DataService.Securities(myParameters) if you need a custom request with parameters. I don't understand what you mean with 'if I use the same data with a method call, the data doesn't get added to "/api/securities"' – Lars Höppner Jun 01 '14 at 11:48
  • Yes, you understand perfectly, and your solution works great. See my edit above if interested. Thanks. – Scott Jun 02 '14 at 02:15