Skip to content

Instantly share code, notes, and snippets.

@hikalkan
Last active August 29, 2015 14:26
Show Gist options
  • Save hikalkan/ee18a1c0810dea194bb9 to your computer and use it in GitHub Desktop.
Save hikalkan/ee18a1c0810dea194bb9 to your computer and use it in GitHub Desktop.
jTable ABP adapter
(function ($) {
//Reference to base object members
var base = {
_create: $.hik.jtable.prototype._create
};
$.fn.createAndLoadJtable = function (opts) {
$(this).jtable(opts.tableOptions);
$(this).jtable("load", opts.postParameters);
};
$.extend(true, $.hik.jtable.prototype, {
//Override _create function to change actions according to Abp system
_create: function () {
var self = this;
base._create.apply(self, arguments);
if (self.options.actions.listAction) {
self._adaptListActionforAbp();
}
if (self.options.actions.createAction) {
self._adaptCreateActionforAbp();
}
if (self.options.actions.deleteAction) {
self._adaptDeleteActionforAbp();
}
},
//LIST ACTION ADAPTER
_adaptListActionforAbp: function () {
var self = this;
var originalListAction = self.options.actions.listAction;
self.options.actions.listAction = function (postData, jtParams) {
return $.Deferred(function ($dfd) {
var input = $.extend({}, postData, {
skipCount: jtParams.jtStartIndex,
maxResultCount: jtParams.jtPageSize,
sorting: jtParams.jtSorting
});
originalListAction.method(input)
.done(function (data) {
$dfd.resolve({
"Result": "OK",
"Records": data[originalListAction.recordsField],
"TotalRecordCount": data.totalCount //TODO: Make an Interface to standardize totalCount
});
})
.fail(function (error) {
self._handlerForFailOnAbpRequest($dfd, error);
});
});
};
},
_adaptCreateActionforAbp: function () {
var self = this;
var originalCreateAction = self.options.actions.createAction;
self.options.actions.createAction = function (postData) {
return $.Deferred(function ($dfd) {
var input = $.extend({}, postData);
originalCreateAction.method(input)
.done(function (data) {
$dfd.resolve({
"Result": "OK",
"Record": data[originalCreateAction.recordField]
});
})
.fail(function (error) {
self._handlerForFailOnAbpRequest($dfd, error);
});
});
};
},
_adaptUpdateActionforAbp: function () {
var self = this;
var originalUpdateAction = self.options.actions.updateAction;
self.options.actions.updateAction = function (postData) {
return $.Deferred(function ($dfd) {
var input = $.extend({}, postData);
originalUpdateAction.method(input)
.done(function (data) {
var result = { "Result": "OK" };
if (originalUpdateAction.recordField) {
result.Record = data[originalUpdateAction.recordField];
}
$dfd.resolve(result);
})
.fail(function (error) {
self._handlerForFailOnAbpRequest($dfd, error);
});
});
};
},
//DELETE ACTION ADAPTER
_adaptDeleteActionforAbp: function () {
var self = this;
var originalDeleteAction = self.options.actions.deleteAction;
self.options.actions.deleteAction = function (postData) {
return $.Deferred(function ($dfd) {
var input = $.extend({}, postData);
originalDeleteAction.method(input)
.done(function () {
$dfd.resolve({
"Result": "OK"
});
})
.fail(function (error) {
self._handlerForFailOnAbpRequest($dfd, error);
});
});
};
},
_handlerForFailOnAbpRequest: function ($dfd, error) {
if (error && error.message) {
$dfd.resolve({
Result: "ERROR",
Message: error.message
});
} else {
$dfd.reject(error);
}
},
//Disable showing error messages
_showError: function (message) {
//do nothing since Abp handles error messages!
}
});
$.extend(true, $.hik.jtable.prototype.options, {
pageList: "minimal"
});
})(jQuery);
public class GetScenariosInput : IPagedResultRequest, ISortedResultRequest, IShouldNormalize
{
public int MaxResultCount { get; set; }
public int SkipCount { get; set; }
public string Sorting { get; set; }
public bool? IsActive { get; set; }
public GetScenariosInput()
{
MaxResultCount = Int32.MaxValue;
Sorting = "Name ASC";
}
public void Normalize()
{
this.NormalizeSortingDirections(sortingDirection => sortingDirection.ToPascalCase());
}
}
$table.jtable({
paging: true,
sorting: true,
multiSorting: true,
defaultSorting: 'name ASC',
actions: {
listAction: {
method: abp.services.callsystem.scenario.getScenarioList,
recordsField: 'scenarioList'
}
},
fields: {
...
}
});
public GetScenarioListOutput GetScenarioList(GetScenariosInput input)
{
var query = _scenarioRepository.GetAll();
if (input.IsActive.HasValue)
{
query = query.Where(s => s.IsActive == input.IsActive.Value);
}
var totalCount = query.Count();
var scenarios = query
.OrderBy(input.Sorting)
.Skip(input.SkipCount)
.Take(input.MaxResultCount)
.ToList();
return new GetScenarioListOutput
{
ScenarioList = Mapper.Map<List<ScenarioDto>>(scenarios),
TotalCount = totalCount
};
}
@hikalkan
Copy link
Author

This adapter (js) not very completed but well tested and works for listAction.
Actually, I improved this adapter a bit more using PagedResultOutput as return value to standardize return objects. I'll publish it in a short time. But this works for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment