dgarbacz
dgarbacz

Reputation: 962

Kendo UI Scheduler not binding resources properly

I'm having trouble trying to get my KendoUI Scheduler resources to populate correctly. I haven't found a good example of how to bind a nested JSON object (in this case Task and User listed in the screenshot) to resources that are fetched.

Also while running Fiddler, it seems like the resources listed below aren't even fetched from the web service. The documentation is super unclear on some of the specifics and I'm having a hard time finding the problem.

The schedule actually pulls back all the SchedulerEvents (the JSON fetched below) and shows me events bound to the Schedule properly, just they have no resources attached. Right now all of my webservice calls are running off localhost so JSONP shouldn't be required yet as my service calls are all calling "localhost".

I did find something similar to what I'm trying to do with projection of data here via Telerik's documentation, but I haven't been able to figure out a good solution yet.

Edited : updated my question with individual DataSource code

Edited 2 : I should also mention since posting this, the JSON screenshot isn't entirely accurate anymore as I've taken ScheduledTaskId and UserId (the two identifying properties of my Task and User resources I'm trying to add to appointments) and made that property available directly from my SchedulerEvent class to avoid nesting objects.

Javascript

//Datasources code has been moved to here. Explicitly fetching data and assigning to Resources datasource properties

$("#scheduler").kendoScheduler({
    date: new Date("@DateTime.Now.ToShortDateString()"),
    timezone: "Etc/UTC",
    views: [
        "day",
        { type: "week", selected: true },
        "month",
        "agenda"
    ],
    dataSource: {

        batch: true,

        transport: {
            read: {
                url: "ServiceURI/Schedule/Get",
                dataType: "json"
            },
            update: {
                url: "",
                dataType: "json"
            },
            create: {
                url: "",
                dataType: "json"
            },
            destroy: {
                url: "",
                dataType: "json"
            },
            parameterMap: function (options, operation) {
                if (operation !== "read" && options.models) {
                    return { models: kendo.stringify(options.models) };
                }
            }
        },

        resources: [
            {
                field: "userId",
                title: "User",
                dataTextField: "displayName",
                dataValueField: "userId",
                dataSource: userDataSource
            }
            {
                field: "scheduledTaskId",
                title: "Task",
                dataTextField: "taskName",
                dataValueField: "scheduledTaskId",
                dataSource: taskDataSource
            }
        ],
        schema: {

            model: {
                id: "shiftId",
                fields: {
                    shiftId: {
                        from: "ShiftId",
                        type: "number"
                    },
                    title: {
                        from: "Title",
                        validation: { required: true } 
                    },
                    start: {
                        from: "Start",
                        type: "date",
                        validation: { required: true } 
                    },
                    end: {
                        from: "End",
                        type: "date",
                        validation: { required: true }
                    },
                    scheduledTaskId: {
                        from: "ScheduledTaskId",
                        type: "number"
                    },
                    userId: {
                        from: "UserId",
                        type: "number"
                    },
                    description: { 
                        from: "Description"
                    },
                    recurrenceId: { from: "RecurrenceID" },
                    recurrenceRule: { from: "RecurrenceRule" },
                    recurrenceException: { from: "RecurrenceException" },
                    isAllDay: { type: "boolean", from: "IsAllDay" } 
                }
            }
        }
    }
});

Datasources

var userDataSource = new kendo.data.DataSource({
    transport: {
        read: {
            //This should be a customized list of users, or all users fetched from the datastore
            url: "ServiceURI/UserProfile/Get/",
            dataType: "json"
        },
    },
    schema: {
        model: {
            id: "userId",
            fields: {
                userId: {
                    from: "UserId",
                    type: "number"
                },
                displayName: {
                    from: "DisplayName"
                }
            }
        }
    }
});
userDataSource.fetch();

var taskDataSource = new kendo.data.DataSource({

    transport: {
        read: {
            //This should be the entire list of tasks fetched from the datastore
            url: "ServiceURI/ScheduledTask/Get?companyId=1",
            dataType: "json"
        }
    },
    schema: {
        model: {
            id: "scheduledTaskId",
            fields: {
                scheduledTaskId: {
                    from: "ScheduledTaskId",
                    type: "number"
                },
                taskName: {
                    from: "TaskName"
                }
            }
        }
    }

});
taskDataSource.fetch();

JSON returned

JSON

Upvotes: 1

Views: 4668

Answers (3)

dgarbacz
dgarbacz

Reputation: 962

Problem

The resources object is inside of the dataSource object as I thought it was supposed to be per the documentation

views: [
    "day",
    { type: "week", selected: true },
    "month",
    "agenda"
],
dataSource: {
    resources: [
        {
            field: "userId",
            title: "User",
            dataTextField: "displayName",
            dataValueField: "userId",
            dataSource: userDataSource
        }
        {
            field: "scheduledTaskId",
            title: "Task",
            dataTextField: "taskName",
            dataValueField: "scheduledTaskId",
            dataSource: taskDataSource
        }
    ]
}

Answer

Just need to move the resources array outside of the DataSource... works as expected without any explicit .fetch() statements either

views: [
    "day",
    { type: "week", selected: true },
    "month",
    "agenda"
],
resources: [
    {
        field: "userId",
        title: "User",
        dataTextField: "displayName",
        dataValueField: "userId",
        dataSource: userDataSource
    }
    {
        field: "scheduledTaskId",
        title: "Task",
        dataTextField: "taskName",
        dataValueField: "scheduledTaskId",
        dataSource: taskDataSource
    }
],
dataSource: { 

}

Upvotes: 1

Obsidian Phoenix
Obsidian Phoenix

Reputation: 4155

I think your issue is binding Case. In your Datasources for the resource Objects, you have a model definition, taking items from your JSON data and transforming it into a Model:

var userDataSource = new kendo.data.DataSource({
    transport: {
        read: {
            //This should be a customized list of users, or all users fetched from the datastore
            url: "ServiceURI/UserProfile/Get/",
            dataType: "json"
        },
    },
    schema: {
        model: {
            id: "userId",
            fields: {
                userId: {
                    from: "UserId",
                    type: "number"
                },
                displayName: {
                    from: "DisplayName"
                }
            }
        }
    }
});

In your scheduler Binding, you are setting the dataTextField and dataValueField to:

{
    field: "userId",
    title: "User",
    dataTextField: "DisplayName",
    dataValueField: "UserId",
    dataSource: userDataSource
}

Notice that your model is displayName and userId, but your resource Bindings are DisplayName and UserId - By the time you are binding into the Scheduler resource collection, the data has been transformed into your model schema, and casing is important.

If you correct the casing, that should solve your issue. You will also need to ensure that both Datasources are populated prior to creating the Scheduler object.

Upvotes: 0

D_Learning
D_Learning

Reputation: 1023

DataSource + Scheduler Creation

var userDataSource = new kendo.data.DataSource({
    transport: {
        read: {
            //This should be a customized list of users, or all users fetched from the datastore
            url: "ServiceURI/UserProfile/Get/",
            dataType: "json"
        },
    },
    requestEnd: function(e) {
        //Start Fetching Task Data Source
        taskDataSource.fetch();
    },
    schema: {
        model: {
            id: "userId",
            fields: {
                userId: {
                    from: "UserId",
                    type: "number"
                },
                displayName: {
                    from: "DisplayName"
                }
            }
        }
    }
});

Task DataSource declaration.

var taskDataSource = new kendo.data.DataSource({

    transport: {
        read: {
            //This should be the entire list of tasks fetched from the datastore
            url: "ServiceURI/ScheduledTask/Get?companyId=1",
            dataType: "json"
        }
    },
    requestEnd: function(e) {
        //Once Task and User DataSource have been fetched, create the Scheduler
        createScheduler(e);
    },
    schema: {
        model: {
            id: "scheduledTaskId",
            fields: {
                scheduledTaskId: {
                    from: "ScheduledTaskId",
                    type: "number"
                },
                taskName: {
                    from: "TaskName"
                }
            }
        }
    }

});

function createScheduler(e){

  $("#scheduler").kendoScheduler({
      date: new Date("@DateTime.Now.ToShortDateString()"),
      timezone: "Etc/UTC",
      views: [
        "day",
        { type: "week", selected: true },
        "month",
        "agenda"
    ],
    dataSource: {

        batch: true,

        transport: {
            read: {
                url: "ServiceURI/Schedule/Get",
                dataType: "json"
            },
            update: {
                url: "",
                dataType: "json"
            },
            create: {
                url: "",
                dataType: "json"
            },
            destroy: {
                url: "",
                dataType: "json"
            },
            parameterMap: function (options, operation) {
                if (operation !== "read" && options.models) {
                    return { models: kendo.stringify(options.models) };
                }
            }
        },

        resources: [
            {
                field: "userId",
                title: "User",
                dataTextField: "DisplayName",
                dataValueField: "UserId",
                dataSource: userDataSource
            }
            {
                field: "scheduledTaskId",
                title: "Task",
                dataTextField: "TaskName",
                dataValueField: "ScheduledTaskId",
                dataSource: taskDataSource
            }
        ],
        schema: {

            model: {
                id: "shiftId",
                fields: {
                    shiftId: {
                        from: "ShiftId",
                        type: "number"
                    },
                    title: {
                        from: "Title",
                        validation: { required: true } 
                    },
                    start: {
                        from: "Start",
                        type: "date",
                        validation: { required: true } 
                    },
                    end: {
                        from: "End",
                        type: "date",
                        validation: { required: true }
                    },
                    scheduledTaskId: {
                        from: "ScheduledTaskId",
                        type: "number"
                    },
                    userId: {
                        from: "UserId",
                        type: "number"
                    },
                    description: { 
                        from: "Description"
                    },
                    recurrenceId: { from: "RecurrenceID" },
                    recurrenceRule: { from: "RecurrenceRule" },
                    recurrenceException: { from: "RecurrenceException" },
                    isAllDay: { type: "boolean", from: "IsAllDay" } 
                }
            }
        }
    }
});
}

Start the DataSources fetching from User Source then Task Source and finally creation of the Scheduler.

userDataSource.fetch();

I have never tried this before, but I think above would work as per your requirement.

Upvotes: 0

Related Questions