2013-03-22 24 views
1

我嘗試使用Compound.js創建一個博客。我有兩個表,「類別」和「發佈」,在每個我有日期屬性,但是當我生成表格時,我得到了每個「新」視圖中的日期屬性的輸入...使用Compound.js更新控制器中的屬性

我想在創建時自動更新控制器中的日期屬性。

我該如何製作?

我的瑪:

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

回答

0

如果你正在尋找設置默認的日期,你可以這麼做Schema像這樣:

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

如果您不希望用戶看到日期字段,則可以將其從視圖標記中刪除,因爲數據對象已經預填充。

相關問題