diff --git a/README.md b/README.md index ea2fca5b..45f369a4 100644 --- a/README.md +++ b/README.md @@ -72,8 +72,10 @@ The server needs a custom model binder that will receive data loading options fr To reach the controller from the client side, use the `DevExpress.data.AspNet.createStore` method. It accepts an object with the following fields. -- `key` - the key property; -- `loadUrl` - the URL used to load data; +- `key` - the key property; +- `allUrl` - the URL used for all operations unless overridden by loadUrl, byKeyUrl, insertUrl etc.; +- `loadUrl` - the URL used to load data; +- `byKeyUrl` - a URL to override the default behaviour when requesting a record by key. See below for rationale - `loadParams` - additional parameters that should be passed to `loadUrl`; - `updateUrl` - the URL used to update data; - `insertUrl` - the URL used to insert data; @@ -89,6 +91,10 @@ To reach the controller from the client side, use the `DevExpress.data.AspNet.cr You can find an example [here](https://github.com/DevExpress/DevExtreme.AspNet.Data/blob/master/net/Sample/Views/Home/Index.cshtml). +The byKeyUrl allows the store to specify a different URL for requests than the default, which uses loadUrl. This is useful when the loadURL implements additional logic that is unnecessary for the byKey function, or when they return different result sets - e.g. when the loadURL returns a Dto with miniimal fields to populate a list, whereas the byKeyUrl returns the full object. + +If myProductStore isset to http://www.example.com/api/products, then the function myProductStore.byKey(5) will issue the request http://www.example.com/api/products/5 instead of the default where the request would be http://www.example.com/api/products with a filter ['ProductId','=','5']. + DevExtreme ASP.NET MVC Controls call the `DevExpress.data.AspNet.createStore` method internally. To configure the parameters, use the lambda expression of the `DataSource()` method. ```Razor diff --git a/js/dx.aspnet.data.d.ts b/js/dx.aspnet.data.d.ts index d67c1db0..30b657c0 100644 --- a/js/dx.aspnet.data.d.ts +++ b/js/dx.aspnet.data.d.ts @@ -1,7 +1,10 @@ import CustomStore from "devextreme/data/custom_store"; interface Options { - key?: string|Array, + key?: string|Array, + + allUrl?: string, + byKeyUrl?: string, loadUrl?: string, loadParams?: Object, diff --git a/js/dx.aspnet.data.js b/js/dx.aspnet.data.js index 4a278931..2bce2331 100644 --- a/js/dx.aspnet.data.js +++ b/js/dx.aspnet.data.js @@ -34,11 +34,12 @@ function createStoreConfig(options) { var keyExpr = options.key, - loadUrl = options.loadUrl, + byKeyUrl = options.byKeyUrl || options.allUrl, + loadUrl = options.loadUrl || options.allUrl, loadParams = options.loadParams, - updateUrl = options.updateUrl, - insertUrl = options.insertUrl, - deleteUrl = options.deleteUrl, + updateUrl = options.updateUrl || options.allUrl, + insertUrl = options.insertUrl || options.allUrl, + deleteUrl = options.deleteUrl || options.allUrl, onBeforeSend = options.onBeforeSend; function send(operation, requiresKey, ajaxSettings, customSuccessHandler) { @@ -132,6 +133,18 @@ return result; } + function getByKeyProperties(keyValue) + { + if (byKeyUrl) return { + url: byKeyUrl + '/' + keyValue, + data: loadOptionsToActionParams() + }; + else return { + url: loadUrl, + data: loadOptionsToActionParams({ filter: filterByKey(keyValue) }) + } + } + return { key: keyExpr, @@ -168,16 +181,15 @@ }, byKey: function(key) { + var keyProps = getByKeyProperties(key); return send( "load", true, - { - url: loadUrl, - data: loadOptionsToActionParams({ filter: filterByKey(key) }) - }, + keyProps, function(d, res) { processLoadResponse(d, res, function(res) { - return [ res.data[0] ]; + if (byKeyUrl) return [ res ]; + else return [ res.data[0] ]; }); } );