0

I am trying to retrieve dates from database in a recordset and print dates in mm/dd/yy mm:ss format in the view. Is it possible to intercept data in the model while retrieval and do formatting there instead of looping over recordset in the controller and reformatting dates in the controller before sending it to the view.

Is there a way in handlebars to format dates in the view without writing a helper function ?

Please help.

Thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Me Unagi
  • 615
  • 2
  • 7
  • 18

1 Answers1

1

To format all my data, I created a service "utils" and pass it formatting libraries like moment.js. Your question asked if the handlebars could do this, and I don't know, but this method gives you the freedom to utilize any library. Reference http://sailsjs.org/#/documentation/concepts/Services

this "utilService" has a few libraries that help with formatting.

// utilService.js - in api/services
var changeCase = require("change-case"),
    moment = require("moment"),
    numeral = require("numeral");    

module.exports = {
    numeral : numeral, 
    moment : moment, 
    changeCase : changeCase
}

then I can use it in my view pages as utilService.moment(DATE).format('YYYY-MM-DD')

Meeker
  • 5,979
  • 2
  • 20
  • 38
  • Thank you for the answer. How do you use it in controller ? Say a find method returns recordset obj array, how do you format data of one field in the recordset without looping it over and before sending it to a view ? – Me Unagi Oct 21 '14 at 07:04
  • The exact same way. You must make sure services are globalized http://sailsjs.org/#/documentation/concepts/Globals and then you just use utilService.moment(DATE).format('YYYY-MM-DD') – Meeker Oct 21 '14 at 14:55