|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en" class="docs"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Formats - Mavo Documentation</title> |
| 6 | + <link rel="stylesheet" href="https://get.mavo.io/mavo.css" /> |
| 7 | + <link rel="stylesheet" href="../../css/style.css" /> |
| 8 | + <link rel="icon" href="/favicon.ico"> |
| 9 | + |
| 10 | + <link rel='stylesheet' href='/docs/style.css'> |
| 11 | + |
| 12 | + <script src="https://get.mavo.io/mavo.js"></script> |
| 13 | +</head> |
| 14 | + |
| 15 | + |
| 16 | +<body class="language-markup"> |
| 17 | + |
| 18 | +<header> |
| 19 | + <h1 class="logo"> |
| 20 | + <a href="/"><img src="../../logo.svg" alt="Mavo"></a> |
| 21 | + </h1> |
| 22 | + <nav> |
| 23 | + <a href="../../docs/primer">Get started</a> |
| 24 | + <a href="../../docs">Docs</a> |
| 25 | + <a href="../../demos">Demos</a> |
| 26 | + <a href="../../faq">FAQ</a> |
| 27 | + <a href="https://plugins.mavo.io">Plugins</a> |
| 28 | + <a href="../../get">Get Mavo</a> |
| 29 | +</nav> |
| 30 | + |
| 31 | +</header> |
| 32 | + |
| 33 | + |
| 34 | +<h2>Formats</h2> |
| 35 | + |
| 36 | +<section> |
| 37 | + <p> |
| 38 | + By default, Mavo saves and reads data in JSON. While you don’t need to understand JSON to use Mavo, you need to use a different file format at some point. |
| 39 | + This is exactly what <code>mv-format</code> is for. |
| 40 | + </p> |
| 41 | +</section> |
| 42 | + |
| 43 | +<section> |
| 44 | + <h1>Basic usage</h1> |
| 45 | + |
| 46 | + <p>You can select a file format in one of two ways:</p> |
| 47 | + |
| 48 | + <ol> |
| 49 | + <li><em>Explicitly</em>, by using the <code>mv-format</code> attribute on your Mavo root. Its value is a keyword, depending on the format, e.g. <code>text</code>, <code>markdown</code>, <code>csv</code> etc.</li> |
| 50 | + <li><em>Implicitly</em>, depending on the extension of your file in <code>mv-storage</code>. E.g. if you use a URL to a CSV file, Mavo will automatically use the CSV format.</li> |
| 51 | + </ol> |
| 52 | + |
| 53 | + <p>Often, you need to specify a different format for the <code>mv-storage</code>, <code>mv-source</code>, and <code>mv-init</code> attributes. |
| 54 | + You can do this by using the <code>mv-storage-format</code>, <code>mv-source-format</code>, and <code>mv-init-format</code> attributes respectively.</p> |
| 55 | +</section> |
| 56 | + |
| 57 | +<section> |
| 58 | + <h1>Available formats</h1> |
| 59 | + |
| 60 | + <p>Mavo comes with some formats out of the box, but you can add more via <a href="https://plugins.mavo.io/?tag=Format">Plugins</a>. |
| 61 | + Read the documenation of the format you’re using carefully, because formats may come with restrictions about the structure of your properties. |
| 62 | + These restrictions are only about which properties will be used for displaying and saving data. If you have more properties in your Mavo, they will just be ignored. |
| 63 | + For example, you can have additional properties that are read-only or contain expressions. |
| 64 | +</p> |
| 65 | + |
| 66 | + <section> |
| 67 | + <h1>CSV</h1> |
| 68 | + |
| 69 | + <p>Uses the popular <a href="http://papaparse.com/">PapaParse</a> library for reading and writing CSV, fetched dynamically via <a href="https://cdnjs.com/">cdnjs</a>.</p> |
| 70 | + |
| 71 | + <table> |
| 72 | + <tr> |
| 73 | + <th>Keyword</th> |
| 74 | + <td><code>csv</code></td> |
| 75 | + </tr> |
| 76 | + <tr> |
| 77 | + <th>File extensions</th> |
| 78 | + <td>*.csv, *.tsv</td> |
| 79 | + </tr> |
| 80 | + <tr> |
| 81 | + <th>Restrictions</th> |
| 82 | + <td>Only one collection, property names inside collection must correspond to headers in CSV file.</td> |
| 83 | + </tr> |
| 84 | + </table> |
| 85 | + </section> |
| 86 | + |
| 87 | + <section> |
| 88 | + <h1>Text</h1> |
| 89 | + |
| 90 | + <table> |
| 91 | + <tr> |
| 92 | + <th>Keyword</th> |
| 93 | + <td><code>text</code></td> |
| 94 | + </tr> |
| 95 | + <tr> |
| 96 | + <th>File extensions</th> |
| 97 | + <td>*.txt</td> |
| 98 | + </tr> |
| 99 | + <tr> |
| 100 | + <th>Restrictions</th> |
| 101 | + <td>Only one property.</td> |
| 102 | + </tr> |
| 103 | + </table> |
| 104 | + </section> |
| 105 | +</section> |
| 106 | + |
| 107 | +<section class="language-javascript"> |
| 108 | + <h1>Advanced: Creating your own formats</h1> |
| 109 | + |
| 110 | + <p class="notice warning">This section requires an understanding of JavaScript.</p> |
| 111 | + |
| 112 | + <p>To create your own formats, all you need is to create a class that extends from <code>Mavo.Formats.Base</code>. |
| 113 | + For information on how the <code>$.Class()</code> class helper works, look in <a href="http://blissfuljs.com/docs.html#fn-Class">Bliss’ documentation</a>.</p> |
| 114 | + |
| 115 | + <pre><code> |
| 116 | + Mavo.MyFormat = $.Class({ |
| 117 | + // Mandatory. You may instead extend another format, e.g. Mavo.Formats.Text |
| 118 | + extends: Mavo.Formats.Base, |
| 119 | + |
| 120 | + // Initialization code (optional) |
| 121 | + constructor: function(backend) { |
| 122 | + // ... |
| 123 | + }, |
| 124 | + |
| 125 | + static: { |
| 126 | + // File extensions |
| 127 | + extensions: [".foo", ".bar"], |
| 128 | + |
| 129 | + // Any dependencies (optional) |
| 130 | + dependencies: [{ |
| 131 | + test: () => "CoolLibrary" in window, // is the library already loaded? |
| 132 | + url: "URL/to/library" // URL to fetch if the library is not loaded |
| 133 | + }], |
| 134 | + |
| 135 | + // must specify this if you’re using dependencies |
| 136 | + ready: Mavo.Formats.Base.ready, |
| 137 | + |
| 138 | + // Mandatory: Convert a string to an object for rendering |
| 139 | + // serialized: The string, as it was read from the remote file |
| 140 | + // me: Instance of Mavo.MyFormat, optional |
| 141 | + parse: function (serialized, me) { |
| 142 | + // ... |
| 143 | + }, |
| 144 | + |
| 145 | + // Mandatory: Convert an object to a string for saving |
| 146 | + // data: The object |
| 147 | + // me: Instance of Mavo.MyFormat, optional |
| 148 | + stringify: function (data, me) { |
| 149 | + // ... |
| 150 | + } |
| 151 | + } |
| 152 | + }); |
| 153 | + </code></pre> |
| 154 | + |
| 155 | + <p>You can see examples of format declarations in <a href="https://github.com/mavoweb/mavo/blob/master/src/formats.js">src/formats.js</a></p> |
| 156 | +</section> |
| 157 | + |
| 158 | +<footer> |
| 159 | + <p> |
| 160 | + Made with ♥ by <a href="http://lea.verou.me">Lea Verou</a> and <a href="https://github.com/mavoweb/mavo/graphs/contributors">all these awesome people</a>. |
| 161 | + Proudly hosted by <a href="https://www.netlify.com" class="netlify">Netlify</a>. |
| 162 | + </p> |
| 163 | + <article> |
| 164 | + <h1>Explore</h1> |
| 165 | + |
| 166 | + <ul> |
| 167 | + <li><a href="/docs/primer">Get started</a></li> |
| 168 | + <li><a href="/docs">Docs</a></li> |
| 169 | + <li><a href="/demos">Demos</a></li> |
| 170 | + <li><a href="/faq">FAQ</a></li> |
| 171 | + <li><a href="https://plugins.mavo.io">Plugins</a></li> |
| 172 | + <li><a href="/get">Get Mavo</a></li> |
| 173 | + <li><a href="https://test.mavo.io">Testsuite</a></li> |
| 174 | + <li><a href="/credits">Credits</a></li> |
| 175 | + </ul> |
| 176 | + </article> |
| 177 | + <article> |
| 178 | + <h1>Social Mavo</h1> |
| 179 | + |
| 180 | + <ul> |
| 181 | + <li><a href="https://twitter.com/mavoweb">Follow @mavoweb on Twitter</a></li> |
| 182 | + <li><a href="https://gitter.im/mavoweb">Chat with us on Gitter</a></li> |
| 183 | + <li><a href="https://github.com/mavoweb/mavo">Github</a></li> |
| 184 | + </ul> |
| 185 | + </article> |
| 186 | + |
| 187 | + <a href="http://mit.edu" class="logo"><img src="../../img/logo-mit.svg" alt="MIT" /></a> |
| 188 | + <a href="http://csail.mit.edu" class="logo"><img src="../../img/logo-csail.svg" alt="CSAIL" /></a> |
| 189 | +</footer> |
| 190 | + |
| 191 | + |
| 192 | +<script src="../../js/prism.js"></script> |
| 193 | +<script src="/sitewide.js"></script> |
| 194 | + |
| 195 | + |
| 196 | +<script src="/js/docs.js"></script> |
| 197 | + |
| 198 | + |
| 199 | +</body> |
| 200 | +</html> |
0 commit comments