2013-03-22 3 views
1

Compound.js를 사용하여 블로그를 만들려고합니다. 두 개의 테이블, "Category"및 "Post", 각 날짜 속성이 있지만 테이블을 생성 할 때 각 "새"보기에서 날짜 속성에 대한 입력 가진 양식을 ...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

당신이 그렇게에서 할 수있는 기본적으로 날짜를 설정하고자하는 경우 스키마는 다음과 같습니다 :

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

사용자가 날짜 필드를 보지 못하도록하려는 경우 데이터 객체가 이미 미리 채워져 있기 때문에보기 마크 업에서 제거 할 수 있습니다.

관련 문제