Last active
August 29, 2015 14:07
-
-
Save oieioi/c7d70d56cf6983335d33 to your computer and use it in GitHub Desktop.
Convert Backbone.Collection to CSV String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConvertCSV extends Backbone.Collection | |
makeCSVStr: (headers) -> | |
body = @map (model) -> | |
headers.map((header)-> | |
'"' + "#{model.get(header)}".replace(/"/g, '""') + '"').join "," | |
[headers.map((header)-> "\"#{header}\"").join ','].concat(body).join('\n') | |
con = new ConvertCSV [ | |
{hoge: "\"", fuga: 4, bar: 0} | |
{hoge: "", fuga: 3, bar: 0} | |
{hoge: " \n \r\n 1", fuga: 2, bar: 0} | |
{hoge: 4, fuga: 1, bar: 0} | |
] | |
console.log con.makeCSVStr ['hoge', 'fuga'] | |
console.log con.makeCSVStr ['fuga', 'hoge'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment