2014-12-08 61 views
0
用戶提供的日期

所以,我已經得到了這個模式:存儲在貓鼬

var imageSchema = new Schema({ 
    caption: {type: String, required: true}, 
    url: {type: String, required: true} 
}); 


var EventSchema = new Schema({ 

    name: {type: String, required: true}, 
    date: {type: Date, required: true}, 
    time: {type: String, required: true}, 
    location: {type: String, required: true}, 
    description: { type: String, required: false }, 
    image: {type: String, required: false}, 
    images: [imageSchema] 


}); 

請求是通過locomotive.js處理,並創造新的記錄控制器動作看起來是這樣的:

EventController.create = function() { 
    if(preScreen.screen.bind(this)("event", "create")) { 
    this.elements = modelHelper.loadValues.bind(this)(); 

    this.saveMessage = "Save"; 
    this.strings = strings; 

    if(this.req.method && this.req.method == "POST") 
    { 
     this._createEvent(); 
    } else { 
     this.render(); 
    } 
    } else { 
    this.redirect(this.urlFor({controller: "dashboard", action: "error"})); 
    } 
}; 

這是一個相當標準的動作控制器;主要是調用一個輸入視圖,或者當用POST頭接收時處理_create。

的_createEvent功能如下:

EventController._createEvent = function() { 
    if(!(this.elements)) this.elements = require('../templates/Event/elements')(); 
    if(!(this.event)) this.event = new Event(); 

    modelHelper.populate.bind(this)(this.elements, "event", function() { 
    modelHelper.save.bind(this)("event", this._confirm.bind(this), "create"); 
    }.bind(this)); 
}; 

對於我的模型封裝了所有的輸入在模板模式。與其在這個框架上花費大量時間(我一旦完成小小的調整,我正在開發開源項目),我會說這個模板實際上包含了模式中每個路徑的一個元素,並提供了一些客戶端這些模板對象的詳細信息(錯誤消息,標籤等)由一個相當不可知的modelHelper對象使用。有效地,modelHelper.populate做的是檢查元素中每個對象的「type」屬性,併爲適當的輸入類型調用處理程序。

日期類型的處理程序是:

case "date" : 
    this[record][field.name] = strings.exists(this.param(field)) ? 
      strings.trim(this.param(field.name)) : null; 

    break; 

雖然我也試過strings.trim(Date.parse(this.param(field.name))從用戶字符串獲得UTC時間戳。

我已經能夠驗證用戶輸入的日期字符串中使用日期語法分析程序中的console.log返回一個有效的UTC時間戳。

當modelHelper.save()調用時運行通過這些模板對象,使用拾取的值創建關聯數組從解析器中傳遞給Record.save()。

其中大部分已經過徹底測試,正在生產中使用,但這是我第一種使用date.now()作爲默認值以外的日期的場景。

爲了使mongodb/mongoose驅動程序將日期推入日期類型,日期解析器的正確正文是什麼?

回答