1

I try to create a blog using Compound.js. I have two tables, "Category" and "Post", in each I have the date property, but when I generate the tables, I got a form with an input for the date property in each "new" view...

I wanna update the date propperty automatically in my controller when this is created.

How can I make?

My shema:

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'});

My model:

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

My controller:

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);
                }
            });
        });
    });
});
tonymx227
  • 5,293
  • 16
  • 48
  • 91

1 Answers1

0

If you're looking to set the date by default you can do so in the Schema like so:

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

If you don't want the user to see the date field you can remove it from the view markup since the data object is already pre-populated.

nopuck4you
  • 1,730
  • 14
  • 20