骨干关系变量父

时间:2015-01-28 19:23:07

标签: javascript backbone.js polymorphism polymorphic-associations backbone-relational

我希望使用骨干关系正确构建我的骨干模型,并想知道这是否是正确的方法。我希望有一个模型Reviewable,可以拥有Photo或Video的父级。基本上每张照片或视频都可以有很多评论。

enter image description here

这是构建这个的正确方法吗?

Photo = Backbone.RelationalModel.extend({
    relations: [
        {
            type: Backbone.HasOne,
            key: 'reviewable',
            relatedModel: 'Reviewable',
            reverseRelation: 
            {
                key: 'parent',
                includeInJSON: 'id'
            }
        }
    ]
});

Video = Backbone.RelationalModel.extend({
    relations: [
        {
            type: Backbone.HasOne,
            key: 'reviewable',
            relatedModel: 'Reviewable',
            reverseRelation: 
            {
                key: 'parent',
                includeInJSON: 'id'
            }
        }
    ]
});

Reviewable = Backbone.RelationalModel.extend({
    relations: [
    {
        type: Backbone.HasMany,
        key: 'reviews',
        relatedModel: 'Review',
        collectionType: 'ReviewCollection',
        reverseRelation: {
            key: 'parent',
            includeInJSON: 'id'
        }
    }
    ]
});

Review = Backbone.RelationalModel.extend({
});

ReviewCollection = Backbone.Collection.extend({
    model: Review
});

修改

Photo = Backbone.RelationalModel.extend({
});

Video = Backbone.RelationalModel.extend({
});

Reviewable = Backbone.RelationalModel.extend({
     subModelTypes: {
        'photo': 'Photo',
        'video': 'Video'
    },
    relations: [
    {
        type: Backbone.HasMany,
        key: 'reviews',
        relatedModel: 'Review',
        collectionType: 'ReviewCollection',
        reverseRelation: {
            key: 'parent',
            includeInJSON: 'id'
        }
    }
    ]
});

Review = Backbone.RelationalModel.extend({
});

ReviewCollection = Backbone.Collection.extend({
    model: Review
});

0 个答案:

没有答案