我想在我的MVC应用程序中使用Kendo Scheduler。但问题在于调度任务获取。因为这不在服务器端。我也做过谷歌,但仍然没有得到任何关于如何使用服务器端剑道网格的建议目前我正在参考Kendo Scheduler来自::Kendo SchedulerDemo
但这是在客户端(即,它从数据库中读取所有数据)。
我想要服务器端调度程序,因为以下原因:
如果数据库有数百万时间表(即变态日期+当前日期+接下来的几个月),那么在这种情况下,我只需要Current Months/Week/Day
的信息,我需要服务器端的剑道时间表。
服务器端调度程序很简单。客户端配置:
$("#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" }
}
}
}
}
});
服务器端(我使用WCF,但任何其他API都可以):
合同:
[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; }
}
合同执行:
[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);
});
}
}
运行服务(在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);
}