描述与CompoundJS的关系

时间:2013-05-28 06:43:19

标签: node.js compoundjs

我对CompoundJS和模型关系有疑问。我尝试在Creation和Photo之间建立关系,因为我能够在Photo表中看到“creationId”。但是,现在我想在我用来展示所有创作的视图中获取每个创作的所有照片。可能吗?我该怎么办?

我的模特:

var Creation = describe('Creation', function () {
    property('title', String);
    property('description', Text);
    property('thumbnail', Text);
    property('moodboard', String);
    property('date', Date);
    property('location', String);
    set('restPath', pathTo.creations);
});

var Photo = describe('Photo', function () {
    property('name', String);
    property('file', String);
    set('restPath', pathTo.photos);
});

Creation.hasMany(Photo, {as: 'photos', foreignKey: 'creationId'});
Photo.belongsTo(Creation, {as: 'creation', foreignKey: 'creationId'});

我的创作控制器:

action(function index() {
    this.title = 'Creations index';
    Creation.all(function (err, creations) {
        console.log(creations);
        switch (params.format) {
            case "json":
                send({code: 200, data: creations});
                break;
            default:
                render({
                    creations: creations
                });
        }
    });
});

当我添加照片时:

    Photo.create(req.body.Photo, function (err, photo) {
        respondTo(function (format) {
            format.json(function () {
                if (err) {
                    send({code: 500, error: photo && photo.errors || err});
                } else {
                    send({code: 200, data: photo.toObject()});
                }
            });
            format.html(function () {
                if (err) {
                    flash('error', 'Photo can not be created');
                    render('new', {
                        photo: photo,
                        title: 'New photo',
                        layout: 'photos'
                    });
                } else {
                    flash('info', 'Photo created');
                    redirect(path_to.photos);
                }
            });
        });
    });
});

0 个答案:

没有答案