【问题标题】:Kendo UI Scheduler not binding resources properlyKendo UI Scheduler 没有正确绑定资源
【发布时间】:2014-07-14 14:38:57
【问题描述】:

我无法正确填充我的 KendoUI 调度程序资源。我还没有找到一个很好的例子来说明如何将嵌套的 JSON 对象(在本例中为屏幕截图中列出的任务和用户)绑定到获取的资源。

此外,在运行 Fiddler 时,似乎下面列出的资源甚至都没有从 Web 服务中获取。文档中的某些细节非常不清楚,我很难找到问题所在。

计划实际上拉回了所有 SchedulerEvents(下面获取的 JSON)并向我显示正确绑定到计划的事件,只是它们没有附加资源。现在我所有的 web 服务调用都在 localhost 之外运行,所以现在不需要 JSONP,因为我的服务调用都在调用“localhost”。

我确实通过 Telerik 的文档找到了类似于我正在尝试对数据进行投影 here 所做的事情,但我还没有找到一个好的解决方案。

已编辑:使用单独的数据源代码更新了我的问题

已编辑 2:我还应该提到,自从发布此内容后,JSON 屏幕截图不再完全准确,因为我使用了 ScheduledTaskId 和 UserId(我的任务的两个识别属性和我试图添加到约会中的用户资源)并直接从我的 SchedulerEvent 类中提供该属性以避免嵌套对象。

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" } 
                }
            }
        }
    }
});

数据源

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 返回

【问题讨论】:

  • 您需要在加载调度程序之前检索资源,因为在视图中呈现调度程序需要资源。如果您可以获取资源值,那么最好的选择是在控制器中检索它们,然后通过 ViewBag 或 ViewData 传递给 View。
  • 如果在构建 kendoScheduler 时无法获取和绑定它,那么拥有客户端数据源的意义何在?
  • 为了进一步简化,我尝试创建单独的 DataSource 对象,然后尝试在创建后获取它们并将它们分配给 Scheduler 对象。我将新添加的“DataSources”代码放在 KendoScheduler 初始化之上,但它仍然没有工作......更新了我的问题
  • 如果您希望从数据源绑定中获取资源,那么实现此目的的最佳选择是在资源数据源请求端创建调度程序。因此,当调度程序初始化时,它具有所有必需的值(即资源列表)。有关更多信息,请参阅下面的答案。

标签: javascript json kendo-ui telerik


【解决方案1】:

问题

资源对象位于 dataSource 对象内部,因为我认为它应该是每个文档中的内容

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
        }
    ]
}

回答

只需将资源数组移到数据源之外...按预期工作而无需任何显式 .fetch() 语句

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: { 

}

【讨论】:

    【解决方案2】:

    数据源 + 调度器创建

    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"
                    }
                }
            }
        }
    });
    

    任务数据源声明。

    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" } 
                    }
                }
            }
        }
    });
    }
    

    开始从用户源获取数据源,然后是任务源,最后创建调度程序。

    userDataSource.fetch();
    

    我以前从未尝试过,但我认为上面的方法可以满足您的要求。

    【讨论】:

    • 它成功调用了所有读取的 URL,但仍然创建了调度程序而没有附加必要的资源。这是非常令人失望的这种方法不能使用。我将尝试获取我的所有资源并通过 ViewBag 传递它,看看它是如何工作的。我认为它不起作用的唯一其他原因是由于架构映射上的绑定问题,但我不知道如何开始调试它并且控制台没有记录任何错误。
    【解决方案3】:

    我认为您的问题是具有约束力的案例。在资源对象的数据源中,您有一个模型定义,从您的 JSON 数据中获取项目并将其转换为模型:

    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"
                    }
                }
            }
        }
    });
    

    在您的调度程序绑定中,您将 dataTextField 和 dataValueField 设置为:

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

    请注意,您的模型是 displayName 和 userId,但您的资源绑定是 DisplayName 和 UserId - 当您绑定到调度程序资源集合时,数据已转换为您的模型架构, 大小写很重要。

    如果您更正了大小写,那应该可以解决您的问题。您还需要确保在创建调度程序对象之前填充两个数据源。

    【讨论】:

    • 我今天早上试试这个!我可能已经尝试过了,因为我也尝试了很多其他方法。谢谢!
    • 没有没有用。我最近的代码更改了这些属性以匹配模型的架构,但仍然无法正确绑定。
    • 您是否看到“资源”部分出现在弹出窗口中,只是没有被选中?并且下拉列表是否包含您正在绑定的资源,还是为空?
    猜你喜欢
    • 1970-01-01
    • 2018-04-07
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多