使用Compound.js更新控制器中的属性

时间:2013-03-22 19:27:42

标签: node.js compoundjs

我尝试使用Compound.js创建博客。我有两个表,“Category”和“Post”,每个表都有date属性,但是当我生成表格时,我在每个“新”视图中都有一个带有日期属性输入的表单...

我想在创建日期属性时自动更新日期属性。

我该怎么做?

我的谢马:

var Category = describe('Category', function () {
    property('name', String);
    property('date', Date);
    set('restPath', pathTo.categories);
});

var Post = describe('Post', function () {
    property('title', String);
    property('content', String);
    property('published', Boolean);
    property('date', Date);
    set('restPath', pathTo.posts);
});

Category.hasMany(Post, {as: 'posts', foreignKey: 'categoryId'});

我的模特:

module.exports = function (compound, Category) {
  // define Category here
};

我的控制器:

action('new', function () {
    this.title = 'New category';
    this.category = new Category;
    render();
});

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

1 个答案:

答案 0 :(得分:0)

如果您希望默认设置日期,可以在Schema中这样设置:

var Category = describe('Category', function () {
    property('name', String);
    property('date', Date, {default: new Date});
    set('restPath', pathTo.categories);
});

如果您不希望用户看到日期字段,您可以从视图标记中删除它,因为数据对象已经预先填充。