A wrapper implementation for any heavily used item is always a good practice. Whatever is not written by us and used at a lot of places should be wrapped within specific functionality to keep it future proof and easily changeable. This also encourages DRY principle to keep our common setting at a central place.
Kendo UI items are enormous in configuration, one of an issue I find people keep repeating codes for Kendo Grid configuration. They have built very flexible system to have any configuration, but in most of the cases, we do not need all of those complicated configuration. We would try to see a simpler configuration of same. The actual core implementation is bit complex, but we do not have to bother about it once done since the focus is just on usage only.
I recommend doing this practice for as simple as jQuery events, form handling or as simple as any notification system. This just won't make things simple but makes codes much more manageable, easy understand, read or open for future enhancements.
The core implementation:GridWrapper.ts
Usage
Once a common code is completed, usage is much more straightforward. In one go everything can be set. Along with such a small setting, we can utilize complex one through callbacks or include more properties.
This is just an example of coding practice, but this kind of approach shall be used at any places where ever we are relying heavily on single type of usage.
Happy coding!
Kendo UI items are enormous in configuration, one of an issue I find people keep repeating codes for Kendo Grid configuration. They have built very flexible system to have any configuration, but in most of the cases, we do not need all of those complicated configuration. We would try to see a simpler configuration of same. The actual core implementation is bit complex, but we do not have to bother about it once done since the focus is just on usage only.
I recommend doing this practice for as simple as jQuery events, form handling or as simple as any notification system. This just won't make things simple but makes codes much more manageable, easy understand, read or open for future enhancements.
The core implementation:GridWrapper.ts
import * as _ from "lodash";
class GridWrapper {
constructor() {
}
InitGrid(gridInitOption: {
GridSelector: string;
GridOption?: (opt: kendo.ui.GridOptions) => void;
DataSource?: (dsOption: kendo.data.DataSourceOptions) => void;
GridColumns: IAppKendoColumn[];
DataSourceUrl?: IDataSourceUrlOption;
Width?: number;
}): kendo.ui.Grid {
let baseUrl = ''; // TODO: Base URL ideally should be done on creation of instance
// Default setting for Kendo Grid.
let gridOptions: kendo.ui.GridOptions = {
columns: gridInitOption.GridColumns,
sortable: true,
noRecords: {
template: 'No records found'
},
pageable: {
refresh: true,
buttonCount: 5
},
};
// Callback for grid option, in case further customization by callee
typeof gridInitOption.GridOption == 'function' && gridInitOption.GridOption(gridOptions);
// DataSource configuration
let dataSource = this.GetDefaultDataSource();
this.ConfigureColumnDataSource(gridInitOption.GridColumns, dataSource);
this.ConfigureTransport(gridInitOption.DataSourceUrl, dataSource);
// Callback for data source, in case if further change required.
typeof gridInitOption.DataSource == 'function' && gridInitOption.DataSource(dataSource);
gridOptions.dataSource = new kendo.data.DataSource(dataSource);
let grid = $(gridInitOption.GridSelector).kendoGrid(gridOptions);
return grid.data('kendoGrid');
}
private GetDefaultDataSource(): kendo.data.DataSourceOptions {
// Default setting for Kendo DataSource
return {
// This is for Kendo MVC but can be removed in case of Kendo UI
type: (() => {
if (kendo.data.transports['aspnetmvc-ajax']) {
return 'aspnetmvc-ajax';
} else {
throw new Error('The kendo.aspnetmvc.min.js script is not included.');
}
})(),
transport: {},
serverPaging: true,
serverSorting: true,
pageSize: 10,
schema: {
data: 'Data',
total: 'Total',
errors: 'Errors',
model: {
fields: {}
}
},
error: (e) => {
// Handling error as requied.
this.ParseModelStateError(e.xhr,
(errMessage, propName) => alert(errMessage)); // TODO: Hook with notification system
}
};
}
GetDataItem(gridSelector: string, evt) {
var dataItem = $(gridSelector).data('kendoGrid').dataItem($(evt.currentTarget).closest("tr"));
return dataItem;
}
private ConfigureTransport(urlOption: IDataSourceUrlOption, datasource: kendo.data.DataSourceOptions) {
if (urlOption == undefined) {
return;
}
// Datasource setting for all URLs required.
datasource.transport.read = typeof urlOption.read == 'string' ?
{
url: baseUrl + urlOption.read,
type: 'POST'
} : urlOption.read;
datasource.transport.create = typeof urlOption.create == 'string' ?
{
url: baseUrl + urlOption.create,
type: 'POST'
} : urlOption.create;
datasource.transport.update = typeof urlOption.update == 'string' ?
{
url: baseUrl + urlOption.update,
type: 'POST'
} : urlOption.update;
datasource.transport.destroy = typeof urlOption.destroy == 'string' ?
{
url: baseUrl + urlOption.destroy,
type: 'DELETE'
} : urlOption.destroy;
}
private ConfigureColumnDataSource(columns: IAppKendoColumn[], dataSource: kendo.data.DataSourceOptions) {
_.forEach(columns, col => {
if (_.has(col, 'field') && _.has(col, 'dataSet')) {
let fieldName = col.field;
let dsFieldOption = col.dataSet;
dataSource.schema.model.fields[fieldName] = dsFieldOption;
// Primary key
let primaryKetSet = false;
// Customization of as per our need to make our life easier.
// this is direct setting based on IAppKendoColumn for data source setting.
if (!primaryKetSet && _.has(dsFieldOption, 'isPrimaryKey')) {
primaryKetSet = true;
dataSource.schema.model.id = fieldName;
delete dsFieldOption.isPrimaryKey;
}
// Default sort
let defaultSortDone = false;
// Default sort settings
if (!defaultSortDone && _.has(dsFieldOption, 'defaultSortOrder')) {
defaultSortDone = true;
dataSource.sort = { field: fieldName, dir: dsFieldOption.defaultSortOrder };
delete dsFieldOption.defaultSortOrder;
}
delete col.dataSet;
}
});
}
ParseModelStateError(data: JQuery.jqXHR<any>, eachErrorCallback: (message: string, propName: string) => void) {
if (data == undefined || data.responseJSON == undefined) {
return;
}
var message = '';
// Error setting initialization.
var propStrings = Object.keys(data.responseJSON);
$.each(propStrings, (errIndex, propString) => {
var propErrors = data.responseJSON[propString];
$.each(propErrors, (errMsgIndex, propError) => {
message += propError;
});
message += '\n';
eachErrorCallback(message, propString);
message = '';
});
}
}
interface IAppKendoColumn
extends kendo.ui.GridColumn {
dataSet?: IDataSetOption;
command?: any;
}
interface IDataSourceUrlOption {
read: kendo.data.DataSourceTransportRead | string;
create?: kendo.data.DataSourceTransportCreate | string;
destroy?: kendo.data.DataSourceTransportDestroy | string;
update?: kendo.data.DataSourceTransportUpdate | string;
}
interface IDataSetOption {
isPrimaryKey?: boolean;
defaultSortOrder?: SortOrder;
type?: DataType;
editable?: boolean;
validation?: any;
defaultValue?: any;
}
enum SortOrder {
asc = 'asc',
desc = 'desc'
}
enum DataType {
Number = 'number',
String = 'string',
Boolean = 'boolean',
Date = 'date'
}
export { KendoGrid, IAppKendoColumn, SortOrder, DataType, IDataSetOption }
Usage
Once a common code is completed, usage is much more straightforward. In one go everything can be set. Along with such a small setting, we can utilize complex one through callbacks or include more properties.
var grid = new Grid(); // In my case I had used DI, but you can use direct method call
grid.InitGrid({
GridSelector: '#UserGrid',
GridColumns: [
{
field: 'Id', title: 'User Id', hidden: true,
dataSet: { isPrimaryKey: true, type: DataType.Number }
},
{
field: 'UserName', title: 'User', dataSet: { type: DataType.String }
},
{
field: 'EmailId', title: 'Email', dataSet: { type: DataType.String }
},
{
field: 'IsActive', title: 'Is User Active',
dataSet: { type: DataType.Boolean, defaultSortOrder: SortOrder.desc }
},
],
Url: 'users/list',
});
This is just an example of coding practice, but this kind of approach shall be used at any places where ever we are relying heavily on single type of usage.
Happy coding!
Comments
Post a Comment