populating an observable with with single object as opposed to a collection of objects

4 posts, 0 answers
  1. mike
    mike avatar
    1 posts
    Member since:
    Jul 2011

    Posted 16 Apr 2013 Link to this post


    i am populating multiple observables and binding them to grids without much problem.  for example i have code like this that i am using to populate a grid:
         var viewModel = kendo.observable({
                partsSource: new kendo.data.DataSource({
                    transport: {
                        read: {
                            url: "/api/Parts",
                            dataType: "json"
                        }
                    }
                })
            });

    this is calling a service that returns IEnumerable<Part>.  if i only want to return a specific part i call my service like this /api/Parts/7 and i know from fiddler that it returns that single part but i can't get it populate my observable.

    i guess the issue is that the service now returns only <Part> and not the enumerable.   in fiddler it looks identical to the data used in the examples that seem to work flawlessly but i can't seem to find the proper syntax with remote data.

    any ideas?  thanks!


  2. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 18 Apr 2013 Link to this post

    Hi Mike,

    I am afraid that the provided information is not sufficient enough in order to determine where exactly the problem comes from.

    Generally speaking, the DataSource works only with arrays. In other words even if you want to populate it with a single record, it should be wrapped inside an array. For example:
    [{ "foo": "foo", "bar": "bar" }]

    I hope this information will help. In case you need further assistance, please provide sample of the server response so I can examine it.

    Kind regards,
    Alexander Valchev
    the Telerik team
    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. Nathan
    Nathan avatar
    39 posts
    Member since:
    Sep 2011

    Posted 29 May 2013 Link to this post

    His request is quite sensible.

    If I want to send one record I have to have an array of one, by adding pointless square brackets around the record.

    Could the Kendo.Datasource not check if it starts with square brackets and assume an array of one if there aren't any?
  5. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 31 May 2013 Link to this post

    Hi Nathan,

    The DataSource component operates with arrays of objects. It is not possible to bypass this behaviour and assume that the single object which is received is an array.

    A possible workaround is to define the schema.data property as a function and wrap the response (object) inside an array.
    schema: {
      data: function(response) {
        //check if the response is single object
        return [response]; //wrap the single object inside an array
      }
    }


    Regards,
    Alexander Valchev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top
Kendo UI is VS 2017 Ready