Reputation: 2307
I want to Use Kendo Scheduler in my MVC Application. But in that the Problem is with Scheduled task fetching. Because that is not on Server Side. I have also done Google, but still not getting any sugegstion on How to use Server Side Kendo Grid Currently I am refereing Kendo Scheduler from:: Kendo Scheduler Demo
But that is on Client Side(i.e. it reads all the data from database).
I want Server side Scheduler because of following reasons:
If Database is having Millions of Schedule (i.e. Pervious Date + Current Date + Next Few months) then in that case I want the information of just Current Months/Week/Day
for that I need Server side Kendo Scheduler.
Upvotes: 1
Views: 1194
Reputation: 22054
Server side scheduler is easy. Client configuration:
$("#scheduler").kendoScheduler({
// highlightning just the parts important for server-side sourcing
dataSource: {
batch: true,
transport: {
read: {
url: "/Services/kendo-scheduler/read",
dataType: "json",
contentType: "application/json; charset=utf-8",
type: "POST"
},
update: {
url: "/Services/kendo-scheduler/update",
dataType: "json",
contentType: "application/json; charset=utf-8",
type: "POST"
},
create: {
url: "/Services/kendo-scheduler/create",
dataType: "json",
contentType: "application/json; charset=utf-8",
type: "POST"
},
destroy: {
url: "/Services/kendo-scheduler/destroy",
dataType: "json",
contentType: "application/json; charset=utf-8",
type: "POST"
},
parameterMap: function (options, operation) {
var result = options;
if (operation === "read") {
var scheduler = $("#scheduler").data("kendoScheduler");
var result = {
start: scheduler.view().startDate(),
end: scheduler.view().endDate()
};
return kendo.stringify(result);
}
return kendo.stringify(options);
}
},
serverFiltering: true,
schema: {
data: "items",
model: {
id: "taskId",
fields: {
taskId: { from: "TaskID", type: "String" },
title: { from: "Title", defaultValue: "No title", validation: { required: true } },
start: { type: "date", from: "Start" },
end: { type: "date", from: "End" },
startTimezone: { from: "StartTimezone" },
endTimezone: { from: "EndTimezone" },
description: { from: "Description" },
recurrenceId: { from: "RecurrenceID", type: "Number" },
recurrenceRule: { from: "RecurrenceRule" },
recurrenceException: { from: "RecurrenceException" },
ownerId: { from: "OwnerID", defaultValue: 1 },
isAllDay: { type: "boolean", from: "IsAllDay" }
}
}
}
}
});
Server side (I'm using WCF but any rest API will do):
Contract:
[ServiceContract]
public interface ITaskService
{
[OperationContract]
[WebInvoke(UriTemplate = "/read", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[Description("Return list of all events")]
KendoTaskCollectionResponse Read(DateRange range);
[OperationContract]
[WebInvoke(UriTemplate = "/destroy", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[Description("Delete an event")]
KendoTaskCollectionResponse Destroy(KendoTaskCollectionRequest model);
[OperationContract]
[WebInvoke(UriTemplate = "/create", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[Description("Create an event")]
KendoTaskCollectionResponse Create(KendoTaskCollectionRequest model);
[OperationContract]
[WebInvoke(UriTemplate = "/update", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[Description("Update an event")]
KendoTaskCollectionResponse Update(KendoTaskCollectionRequest model);
}
[DataContract]
public class ErrorResponse : IErrorResponse
{
[DataMember]
public virtual string Error { get; set; }
[DataMember]
public virtual string ErrorUID { get; set; }
[DataMember]
public virtual bool IsSuccess { get; set; }
}
[DataContract]
public class KendoTaskCollectionRequest
{
[DataMember(Name="models")]
public IList<KendoTask> Models { get; set; }
}
[DataContract]
public class KendoTaskCollectionResponse : ErrorResponse, IClientResponse<KendoTask>
{
public KendoTaskCollectionResponse()
{
Items = new List<KendoTask>();
}
public KendoTaskCollectionResponse(IList<KendoTask> list)
{
Items = new List<KendoTask>(list);
}
[DataMember(Name="items")]
public IList<KendoTask> Items { get; set; }
}
public interface IClientResponse<T> : IErrorResponse
{
IList<T> Items { get; set; }
}
[DataContract]
public class KendoTask
{
[DataMember]
public string TaskID { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public string Start { get; set; }
[DataMember]
public string StartTimezone { get; set; }
[DataMember]
public string End { get; set; }
[DataMember]
public string EndTimezone { get; set; }
[DataMember]
public string RecurrenceRule { get; set; }
[DataMember]
public int? RecurrenceID { get; set; }
[DataMember]
public string RecurrenceException { get; set; }
[DataMember]
public bool IsAllDay { get; set; }
[DataMember]
public int? OwnerID { get; set; }
}
[DataContract]
public class DateRange
{
[DataMember(Name = "start")]
public string Start { get; set; }
[DataMember(Name = "end")]
public string End { get; set; }
}
Implementation of contract:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TaskService : ITaskService
{
protected T SafeExecute<T>(Func<T> action) where T : IErrorResponse, new()
{
try
{
return action.Invoke();
}
catch (Exception e)
{
// publish the error somewhere - logs
var response = new T();
// this is just for development, on prod
// you'd want to hide this error
response.Error = e.ToString();
return response;
}
}
public KendoTaskCollectionResponse Read(DateRange range)
{
return SafeExecute<KendoTaskCollectionResponse>(() =>
{
var start = DateTime.Parse(range.Start, CultureInfo.InvariantCulture);
var end = DateTime.Parse(range.End, CultureInfo.InvariantCulture);
// lookup to db based on start and end
var appointments = ...;
return new KendoTaskCollectionResponse(appointments);
});
}
public KendoTaskCollectionResponse Destroy(KendoTaskCollectionRequest model)
{
return SafeExecute<KendoTaskCollectionResponse>(() =>
{
var result = new List<KendoTask>();
if (model.Models != null)
{
foreach (var task in model.Models)
{
// delete from db here
result.Add(task);
}
}
return new KendoTaskCollectionResponse(result);
});
}
public KendoTaskCollectionResponse Create(KendoTaskCollectionRequest model)
{
return SafeExecute<KendoTaskCollectionResponse>(() =>
{
var result = new List<KendoTask>();
if (model.Models != null)
{
foreach (var task in model.Models)
{
// insert to db here, don't forget to set task.TaskID
result.Add(task);
}
}
return new KendoTaskCollectionResponse(result);
});
}
public KendoTaskCollectionResponse Update(KendoTaskCollectionRequest model)
{
return SafeExecute<KendoTaskCollectionResponse>(() =>
{
var result = new List<KendoTask>();
if (model.Models != null)
{
foreach (var task in model.Models)
{
// update db here
result.Add(task);
}
}
return new KendoTaskCollectionResponse(result);
});
}
}
Running the service (in Global.asax.cs):
void RegisterRoutes(RouteCollection routes) {
routes.Add(new ServiceRoute("Services/kendo-scheduler", new WebServiceHostFactory(), typeof(TaskService)));
}
protected void Application_Start(Object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
Upvotes: 1