Skip to content

Instantly share code, notes, and snippets.

@hanssens
Created March 26, 2015 14:14
Show Gist options
  • Save hanssens/a34b08ad9dbf9741561a to your computer and use it in GitHub Desktop.
Save hanssens/a34b08ad9dbf9741561a to your computer and use it in GitHub Desktop.
app.data.js | extensions for kendo datasources and data operations
/**
* Provides data interoptability, such as datasources and raw enum values.
*
* USAGE
* 1. Include the file as <script src="app.data.js"></script>
* 2. Call it globally using, like for example:
*
* var pong = api.data.ping();
*
* REMARKS
* Has a dependency on the following assemblies:
* - app.js (and its dependencies)
* - jQuery
* - Kendo UI (DataSource)
*/
(function (data, $, undefined) {
"use strict";
/**
* Name : app.data.ApiDataSource
* Description: Initializes a new instance of a Kendo DataSource, ideally suited for communicating with a Web API.
* @endpoint The uri of the endpoint, incl. trailing slash, like for example: "/api/devices/".
* @config [Optional] provide the datasource's configuration. If not provided uses 'api.data.ApiDataSourceDefaultConfig' as default.
*/
data.ApiDataSource = function (endpoint, config) {
var that = this;
var baseUrl = endpoint;
// determine if a specific configuration has been explicitely provided
if (!config) {
// ... if not, use the default configuration
app.log("Initialized datasource for '" + endpoint + "', with *default* config");
config = this.ApiDataSourceDefaultConfig(baseUrl);
}
return new kendo.data.DataSource(config);
},
/**
* Name : app.data.ApiDataSourceDefaultConfig
* Description: returns the default datasource configuration and can ideally be used with an overload to configure the ApiDataSource
* @endpoint The uri of the endpoint, incl. trailing slash, like for example: "/api/devices/".
*/
data.ApiDataSourceDefaultConfig = function (endpoint) {
if (!endpoint) {
throw "'api.data.ApiDataSourceDefaultConfig' requires an endpoint as a parameter.";
}
var baseUrl = endpoint;
var defaultConfiguration = {
batch: false,
schema: {
model: {
id: "Id"
}
},
transport: {
read: {
url: baseUrl,
dataType: "json",
type: "GET"
},
update: {
// note: 'update' requires both a parametermap as well as an ID
url: function (options) {
return baseUrl + options.Id;
},
dataType: "json",
type: "PUT"
},
create: {
url: baseUrl,
dataType: "json",
type: "POST"
},
destroy: {
// note: destroy requires a custom function - a HTTPDELETE cannot be processed through parameterMap
url: function (options) {
return baseUrl + options.Id;
},
dataType: "json",
type: "DELETE"
},
parameterMap: function (options, operation) {
if (options) {
// when 'options' are provided, just return them
// as raw JSON object. This means without casting
// them using kendo.stringify(options)
return options;
}
return null;
}
}
}
return defaultConfiguration;
},
data.ping = function () {
return "pong";
}
}(window.app.data = window.app.data || {}, jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment