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

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

3 Answers 158 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
mike
Top achievements
Rank 1
mike asked on 16 Apr 2013, 09:19 PM

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!


3 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 18 Apr 2013, 11:33 AM
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!
0
Nathan
Top achievements
Rank 2
answered on 29 May 2013, 04:39 PM
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?
0
Alexander Valchev
Telerik team
answered on 31 May 2013, 08:03 AM
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!
Tags
MVVM
Asked by
mike
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Nathan
Top achievements
Rank 2
Share this question
or