I may have figured this out now, not sure how to sensibly post code here. In essence, I add GET parameters to the CRUD URL, and update those later on "onNavigation".
e.g. <ej-schedule id="YourSchedule" .... current-date="@DateTime.Now" current-view="Month" enable-load-on-demand="true" navigation="onNavigation">
<e-datamanager url="Schedule/GetData" crud-url="Schedule/[email protected]&currentView=month" adaptor="UrlAdaptor" cross-domain="true"></e-datamanager>
One would need to include currentDate and currentView because a Crud operation could happen before any navigation occured.
Finally, we need
function onNavigation(args) {
var scheduler = $("#YourSchedule").data("ejSchedule");
console.log("onNavigation");
scheduler._dataManager.dataSource.crudUrl = "Schedule/Batch?CurrentDate=" + args.currentDate.toISOString() + "¤tView=" + args.currentView;
}
Now the Controller Batch method can look like this
public JsonResult Batch([FromBody] EditParams param, [FromQuery] ScheduleGetDataRequest request)
where ScheduleGetDataRequest is the same as for getData with load-on-demand=true; it simply contains
public DateTime CurrentDate { get; set; }
public string CurrentView { get; set; }
public string CurrentAction { get; set; }
Maybe this helps someone.
|
<ej-schedule id="Schedule1" width="100%" height="525px" enable-load-on-demand="true" action-complete="onCreate" current-date="new DateTime(2014, 12, 5)">
<e-appointment-settings apply-time-offset="false" id="Id" subject='"Subject"' start-time='"StartTime"' end-time='"EndTime"' all-day='"AllDay"' recurrence='"Recurrence"' recurrence-rule='"RecurrenceRule"'>
<e-datamanager id="myData" url="Home/GetData" crud-url="Home/Batch" adaptor="UrlAdaptor"></e-datamanager>
</e-appointment-settings>
</ej-schedule>
@section scripts{
<script>
function onCreate(args) {
var customAdaptor = new ej.UrlAdaptor().extend({
beforeSend: function (request, settings) {
var schObj = $("#Schedule1").data("ejSchedule");
currentDate = schObj.model.currentDate;
currentView = schObj.model.currentView;
settings.setRequestHeader("CurrentDate", currentDate);
settings.setRequestHeader("CurrentView", currentView);
}
});
this.model.appointmentSettings.dataSource.adaptor = new customAdaptor();
}
</script>
} |
|
public List<DefaultSchedule> Batch([FromBody] EditParams param, [FromHeader(Name = "CurrentDate")] DateTime currentDate, [FromHeader(Name = "CurrentView")] DateTime currentView)
{
if (param.action == "insert" || (param.action == "batch" && (param.added.Count>0))) // this block of code will execute while inserting the appointments
{
DefaultSchedule appoint = new DefaultSchedule();
object result;
if (param.action == "insert")
{
var value = param.value;
foreach (var fieldName in value.GetType().GetProperties())
{
var newName = fieldName.ToString().Split(null);
if (newName[1] == "Id") result = (_context.DefaultSchedule.ToList().Count > 0 ? _context.DefaultSchedule.ToList().Max(p => p.Id) : 1) + 1;
else if (newName[1] == "StartTime" || newName[1] == "EndTime") result = Convert.ToDateTime(fieldName.GetValue(value));
else result = fieldName.GetValue(value);
fieldName.SetValue(appoint, result);
}
_context.DefaultSchedule.Add(appoint);
}
|
Hi Nevitha,
thank you very much for the update. I didn't realise that GetData is called after Batch, this solves the problem without any changes. It might be useful to add this piece of information to your documentation?
Also, the controller sample in the documentation is specified as
public JsonResult Batch([FromBody] EditParams param)
and in fact returns a list of appointments (from the documentation: return List<ScheduleDatabase> appointments = _context.ScheduleDatabase.Take(500).ToList())
In fact, it could return void and save a call to the database, since GetData takes care of that.
Thanks for pointing this out.
Just for anyone following this thread, my assumptions weren't entirely correct. The batch method does indeed have to return a result, otherwise the Schedule component will run into a lot of xyz is undefined Javascript errors.
The way I have done this now is to only return what was changed in the dbContext.
public JsonResult Batch([FromBody] EditParams param)
{
switch (param.Action)
{
case "insert":
appointment = _manipulateAppointment(param.Value);
_context.ScheduleDatabase.Add(appointment);
break;
[...]
_context.SaveChanges();
return Json(_context.ScheduleDatabase.Local.ToList());
This appears to be working fine.