复合模型(模型中的模型),还是模型之间的手动外键关联?

发布于 2024-12-07 11:01:31 字数 753 浏览 0 评论 0原文

我有两个模型,我想将一个模型“嵌套”在另一个模型中。有没有办法用 Sencha Touch 做到这一点?我的模型如下所示:

Ext.regModel('OuterModel', {
    fields: ['name']
});

var outerStore = new Ext.data.Store({
    model: 'OuterModel',
    data: [
        {name: 'item1'},
        {name: 'item2'}
    ]
});


Ext.regModel('InnerModel', {
    fields: ['a', 'b']
});
var innerStore = new Ext.data.Store({
    model: 'InnerModel',
    data: [
        {a: 1, b: 5},
        {a: 2, b: 5},
        {a: 3, b: 3}
    ]
});

每个 OuterModel 需要一个关联的 InnerModel,并且我希望能够拥有一个内部模型的字段-我可以从中创建 Ext.List 的模型。我可以将 outerName 添加到 InnerModel 并查询 outerName,但这感觉很草率,需要手动管理关联。

手动解决方案是我唯一的选择,还是我可以将一个模型作为另一个模型的字段?

I have two models, and I want to "nest" one within the other. Is there a way to do this with Sencha Touch? My models look like this:

Ext.regModel('OuterModel', {
    fields: ['name']
});

var outerStore = new Ext.data.Store({
    model: 'OuterModel',
    data: [
        {name: 'item1'},
        {name: 'item2'}
    ]
});


Ext.regModel('InnerModel', {
    fields: ['a', 'b']
});
var innerStore = new Ext.data.Store({
    model: 'InnerModel',
    data: [
        {a: 1, b: 5},
        {a: 2, b: 5},
        {a: 3, b: 3}
    ]
});

Each OuterModel needs an associated InnerModel, and I would love to just be able to have a field that was an inner-model that I could create Ext.Lists from. I could add outerName to InnerModel and query on outerName, but this feels sloppy, manually managing the association.

Is the manual solution my only option, or can I make a model a field of another model?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑梦风尘 2024-12-14 11:01:31

模型可以通过belongsTo 和hasMany 关联与其他模型建立关联。例如,假设我们正在编写一个处理用户、帖子和评论的博客管理应用程序。我们可以这样表达这些模型之间的关系:

  Ext.regModel('Post', {
    fields: ['id', 'user_id'],

    belongsTo: 'User',
    hasMany  : {model: 'Comment', name: 'comments'}
 });

Ext.regModel('Comment', {
    fields: ['id', 'user_id', 'post_id'],

    belongsTo: 'Post'
 });

Ext.regModel('User', {
    fields: ['id'],

    hasMany: [
        'Post',
        {model: 'Comment', name: 'comments'}
    ]
 });

请参阅文档 Ext.data.BelongsToAssociationExt.data.HasManyAssociation 了解有关关联的使用和配置的详细信息。请注意,关联也可以这样指定:

Ext.regModel('User', {
    fields: ['id'],

    associations: [
        {type: 'hasMany', model: 'Post',    name: 'posts'},
        {type: 'hasMany', model: 'Comment', name: 'comments'}
    ]
});

Models can have associations with other Models via belongsTo and hasMany associations. For example, let's say we're writing a blog administration application which deals with Users, Posts and Comments. We can express the relationships between these models like this:

  Ext.regModel('Post', {
    fields: ['id', 'user_id'],

    belongsTo: 'User',
    hasMany  : {model: 'Comment', name: 'comments'}
 });

Ext.regModel('Comment', {
    fields: ['id', 'user_id', 'post_id'],

    belongsTo: 'Post'
 });

Ext.regModel('User', {
    fields: ['id'],

    hasMany: [
        'Post',
        {model: 'Comment', name: 'comments'}
    ]
 });

See the docs for Ext.data.BelongsToAssociation and Ext.data.HasManyAssociation for details on the usage and configuration of associations. Note that associations can also be specified like this:

Ext.regModel('User', {
    fields: ['id'],

    associations: [
        {type: 'hasMany', model: 'Post',    name: 'posts'},
        {type: 'hasMany', model: 'Comment', name: 'comments'}
    ]
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文