简体   繁体   English

使用Kendo ui数据源获取多个级别的对象?

[英]Fetching multiple levels of objects with kendo ui datasource?

I'm very new to developing mobile applications with telerik appbuilder. 我对使用Telerik AppBuilder开发移动应用程序非常陌生。 There are some things I have a hard time to understand with fetching data from Everlive. 从Everlive获取数据有些事情我很难理解。

Lets consider a simple example. 让我们考虑一个简单的例子。 Lets say I have Blog Posts and Comments that belong to those Posts. 可以说我有属于这些帖子的Blog帖子和评论。 And both Posts and Comments are made by Users. 并且帖子和评论都是由用户发布的。

In one view I want to present the Post with corresponding Comments and I also need the Username of the User who posted the Comment (Comment table only contains userId). 在一个视图中,我想为帖子提供相应的评论,并且我还需要发布评论的用户的用户名(评论表仅包含userId)。

Both the Post and the Comments are easy to fetch since I have the id of the Post. 帖子和评论都很容易获取,因为我有帖子的ID。 But how do I fetch the corresponding user for each Comment? 但是,如何为每个评论获取相应的用户?

The FriendsApp example does something very similar but it uses this line to get the user: FriendsApp示例执行的操作非常相似,但是它使用此行来获取用户:

var user = $.grep(app.Users.users(), function (e) {
   return e.Id === userId;
})[0];

This fetches all users and filters them client side? 这会获取所有用户并过滤客户端吗? I guess this is okay if you have like 10 users. 我想如果您有10个用户,这是可以的。 But what if you have a million users? 但是,如果您有100万用户,该怎么办?

I am guessing that the FriendsApp uses this way of resolving the relations just for keeping the simplicity of the sample. 我猜想,FriendsApp使用这种解决关系的方法只是为了保持示例的简单性。 Everlive offers a far more meaningful toolset for resolving relation fields called Expand. Everlive为解析关系字段提供了一个更有意义的工具集,称为Expand。 You can explore the REST API here: 您可以在此处探索REST API:

http://docs.telerik.com/platform/backend-services/development/rest-api/relations/simple-expanding http://docs.telerik.com/platform/backend-services/development/rest-api/relations/simple-expanding

or the JS SDK function here: 或JS SDK函数:

http://docs.telerik.com/platform/backend-services/development/javascript-sdk/relations/simple-expanding . http://docs.telerik.com/platform/backend-services/development/javascript-sdk/relations/simple-expanding

As the Friends app uses the Kendo UI data source component you can send an Expand header with the request. 当Friends应用程序使用Kendo UI数据源组件时,您可以发送带有请求的Expand标头。 The following configuration of the data source will return the DisplayName of the user in each Activity/Comments entity: 数据源的以下配置将返回每个Activity / Comments实体中用户的DisplayName:

var expandObject = { 
    "CreatedBy": { 
        "ReturnAs": "User", 
        "SingleField": "DisplayName" 
    } 
};

var dataSource = new kendo.data.DataSource({ 
    type: "everlive", 
    transport: { 
        typeName: 'Activities', // replace this with Comments 
        read: { 
            beforeSend: function (xhr) { 
                xhr.setRequestHeader("X-Everlive-Expand", JSON.stringify(expandObject)) 
            }, 
        } 
    }, 
    schema: { 
        model: { 
            id: Everlive.idField 
        } 
    } 
}); 

dataSource.fetch(function (data) { 
    console.log(data.items); 
}); 

Same could be applied for resolving the comments for each Blog post. 可以将相同的内容用于解决每个Blog帖子的评论。 Given the Friends data schema you will need to use the External Relation resolver of the Everlive API. 给定Friends数据模式后,您将需要使用Everlive API的外部关系解析器。 Note that it is available only in a GetById scenario, eg when retrieving an Activity by Id, you can resolve the Comments that point to this activity, which is generally very handy in master-detail views. 请注意,它仅在GetById方案中可用,例如,按ID检索活动时,您可以解析指向该活动的注释,这在主从视图中通常非常方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM