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

HierarchicalDataSource with odata, $expand and __count

1 Answer 177 Views
Hierarchical Data Source
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 30 Jul 2013, 02:22 PM
Hi All,

I am getting this error.

Cannot read property '__count' of undefined

I have read up on this and the solution was to make sure that this statement is part of the model.

                    total: function (data) {
                        return data["odata.count"];
                    },

Fiddler returns the correct information 

  "odata.metadata":"<url>/%24metadata#categories","odata.count":"1","value":[
    {
      "SubCategories":[
        {
          "Id":1,"Name":"Skateboards","CategoryId":1,"Tracking":{
            "CreatedBy":"tester","CreatedOn":"2013-07-29T14:48:25.987","ModifiedBy":"tester","ModifiedOn":"2013-07-29T14:48:25.987","Timestamp":"AAAAAAAACAk="
          }
        }
      ],"Id":1,"Name":"Sports","Tracking":{
        "CreatedBy":"tester","CreatedOn":"2013-07-29T14:48:25.987","ModifiedBy":"tester","ModifiedOn":"2013-07-29T14:48:25.987","Timestamp":"AAAAAAAACAY="
      }
    }

Here is the data source configuration.  

new kendo.data.HierarchicalDataSource({
            type: "odata",
            transport: {
              read: {
                  url: resourceurl + "/categories",
                  data: {
                      $expand:"SubCategories"
                  },
                  dataType: "json"
              }  
            },
            schema: {
                model: {
                    id: "Id",
                    hasChildren: true,
                    children: "SubCategories",
                    data: function (data) {
                        if (data.value) {
                            return data.value;
                        }
                        delete data["odata.metadata"];
                        return [data];
                    },
                    total: function (data) {
                        return data["odata.count"];
                    },
                    errors: function (data) {
                    }                    
                }
            }
        });

Any ideas?

Regards

Richard...

1 Answer, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 1
answered on 31 Jul 2013, 04:24 PM
Well what more can be said then me messing up the schema section and then it will not work!

This is how it should be.  You will notice that the data, total and error properties need to be ad the schema level and I had them in the model level!

schema: {
    model: {
          id: "CategoryId",
         hasChildren: true,
        children:"SubCategories"
   },
   data: function (data) {
         if (data.value) {
             return data.value;
        }
        delete data["odata.metadata"];
       return [data];
  },
  total: function (data) {
     return data["odata.count"];
  },
  errors: function (data) {
  }
}

Regards

Richard...
Tags
Hierarchical Data Source
Asked by
Richard
Top achievements
Rank 1
Answers by
Richard
Top achievements
Rank 1
Share this question
or