problem with cache.readRange(..) when using collection indices in OData

Mar 29, 2012 at 6:53 PM

Hi,

I'm successfully using DataJS caching when calling multi-item OData queries in my app.  However, I'm facing problems when calling OData queries which use object indices.  Here's what I mean:

  code:  cache.readRange(0, 1).then(successHandler, failHandler);

results:  GET http://localhost/service/gw/CONTACTDETAIL/CONTACTCollection(0000000063)?$skip=0&$top=50 400 (Bad Request)

As you can see, DataJS is adding the $skip and $top query parameters to the call (for paging, presumably).  However, these query parameters are incompatible with the collection index (0000000063) syntax apparently.

Is there any way to prevent DataJS from adding these query parameters? Also, is there any way to get readRange() to return all results instead of having to specify paging parameters?

Thanks,

Dave

Mar 31, 2012 at 1:07 AM
Edited Mar 31, 2012 at 1:07 AM

Hi Dave, 

   The cache out of the box uses a very simple data provider that assumes the uri used always point to an entity collection.. thus it always adds $skip and $top query parameters.  To change this behavior, you can specify your own custom data source object for the cache.  In order to do this, you have to first create an object that has the following contract:

var myDataSource = {
    count : function (success, error) {
            /// <summary>Gets the number of items in the collection.</summary>
            /// <param name="success" type="Function">Success callback with the item count.</param>
            /// <param name="error" type="Function">Error callback.</param>
            /// <returns type="Object">Request object with an abort method./<param>
    },

    read : function (index, count, success, error) {
            /// <summary>Gets a number of consecutive items from the collection.</summary>
            /// <param name="index" type="Number">Zero-based index of the items to retrieve.</param>
            /// <param name="count" type="Number">Number of items to retrieve.</param>
            /// <param name="success" type="Function">Success callback with the requested items.</param>
            /// <param name="error" type="Function">Error callback.</param>
            /// <returns type="Object">Request object with an abort method./<param>
    }
}

your custom data source can then query the uri that you need with the help of the OData functions, jquery, or any other mechanism you use to request the data from the endpoint.

When you create your cache, you can pass this object as the source property of the options object:

var cache = datajs.createDataCache({ name: "sessions", source: myDataSource }); 

As for getting the complete collection, doing  cache.readRange(0, 0); should do the trick.

Regards,

Alex Trigo.