|
| 1 | +# App Portability |
| 2 | + |
| 3 | +With Jasonette, the apps become completely portable because the entire logic can be expressed as JSON, which can be sent to, stored, and received from anywhere. |
| 4 | + |
| 5 | +<br> |
| 6 | + |
| 7 | +Since portability is one of Jasonette's main benefits, we are developing many different ways of storing, transmitting, and receiving JSON, and planning on supporting many useful protocols. |
| 8 | + |
| 9 | +<br> |
| 10 | + |
| 11 | +One of those efforts is local JSON. |
| 12 | + |
| 13 | +<br> |
| 14 | + |
| 15 | +# Local |
| 16 | + |
| 17 | +There are currently several ways Jasonette supports local JSON, each of which is unique in its own way. |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +1. [Data-uri](#1-data-uri) |
| 22 | +2. [Store JSON locally on the app](#2-file-url) |
| 23 | +3. [Offline caching](#3-offline-caching) |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## 1. Data-uri |
| 28 | + |
| 29 | +Jasonette supports [data-uri](https://en.wikipedia.org/wiki/Data_URI_scheme). This means you can store the entire content within the URL itself. It's not always practical but there are several cases where you can get neat effects using this approach. |
| 30 | + |
| 31 | +Try entering a data-url instead of http or https based url. It should work. |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## 2. File URL |
| 36 | + |
| 37 | +Another URL scheme Jasonette supports is local file scheme (`file://`). |
| 38 | + |
| 39 | +Instead of using `http` or `https`, you can refer directly to your local file bundled up with the app. There are currently two ways to use the file URL scheme: |
| 40 | + |
| 41 | +<br> |
| 42 | + |
| 43 | +### Using File URL Scheme |
| 44 | + |
| 45 | +<br> |
| 46 | + |
| 47 | +> **A. Home URL ** |
| 48 | +> |
| 49 | +> Instead of setting a remote URL you can point it to a local JSON file. |
| 50 | +
|
| 51 | +<br> |
| 52 | + |
| 53 | +> Here's what it looks like on Android: |
| 54 | +> |
| 55 | +> <br> |
| 56 | +> |
| 57 | +><img src='../images/android_home_local_url.png' class='large'> |
| 58 | +
|
| 59 | +<br> |
| 60 | + |
| 61 | +> Here's what it looks like on iOS: |
| 62 | +
|
| 63 | +><img src='../images/ios_home_local_url.png' class='large'> |
| 64 | +
|
| 65 | +--- |
| 66 | + |
| 67 | +> **B. HREF** |
| 68 | +
|
| 69 | +>You can also `href` into local JSON URLs: |
| 70 | +``` |
| 71 | + { |
| 72 | + "type": "label", |
| 73 | + "text": "Go", |
| 74 | + "href": { |
| 75 | + "url": "file://demo.json" |
| 76 | + } |
| 77 | + } |
| 78 | +``` |
| 79 | + |
| 80 | +<br> |
| 81 | + |
| 82 | +### Storing files locally |
| 83 | + |
| 84 | +To utilize this feature, you first need to store files under the right folders. Let me show you how: |
| 85 | + |
| 86 | +<br> |
| 87 | + |
| 88 | +**iOS File URL** |
| 89 | + |
| 90 | +1. Open XCode, go to `Jasonette > Core > file://` from the sidebar, and add your files there by drag and dropping. |
| 91 | +2. Access using `file://your_filename.json` |
| 92 | + |
| 93 | +<br> |
| 94 | + |
| 95 | +<img src='../images/ios_local_file.png' class='large'> |
| 96 | + |
| 97 | +<br> |
| 98 | + |
| 99 | +** Android File URL** |
| 100 | + |
| 101 | +1. Open Android Studio, go to `app > assets > file`. Copy and paste your files there. |
| 102 | +2. Access using `file://your_filename.json` |
| 103 | + |
| 104 | +<br> |
| 105 | + |
| 106 | +<img src='../images/android_local_file.png' class='large'> |
| 107 | + |
| 108 | +<br> |
| 109 | + |
| 110 | +**(Disclaimer: The file url scheme is currently limited to the home URL and `href`s. It doesn't include mixins, requires, network.request. If you would like to add these features to Jasonette, feel free to send a pull request and contribute!)** |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## 3. Offline Caching |
| 115 | + |
| 116 | +You can have best of both worlds (Stream the app on demand to keep it up-to-date all the time, as well as have the app logic cached locally so it loads instantly) by using the offline caching feature. |
| 117 | + |
| 118 | +<br> |
| 119 | + |
| 120 | +### How offline caching works |
| 121 | + |
| 122 | +1. The first time the app loads, Jasonette fetches the JSON from your server. |
| 123 | + |
| 124 | +2. If you specify that you want to use offline caching, Jasonette will cache the entire JSON for that view. |
| 125 | + |
| 126 | +3. Next time you open the view, Jasonette will load immediately from the offline cached version. |
| 127 | + |
| 128 | +4. But it doesn't stop there, Jaasonette checks to see if the network is available, and if it is, it re-fetches the JSON and updates the view. The trick is **step C** comes first, so it will ONLY update if the network is available. Otherwise you'll still have your offline cached version of your app. |
| 129 | + |
| 130 | +<br> |
| 131 | + |
| 132 | +### How to use |
| 133 | + |
| 134 | +Offline caching is managed on a per-view basis. All you need to do to enable is put `"offline": "true"` under `$jason.head`, like this: |
| 135 | + |
| 136 | + { |
| 137 | + "$jason": { |
| 138 | + "head": { |
| 139 | + "title": "offline test", |
| 140 | + "offline": "true", |
| 141 | + ... |
| 142 | + } |
| 143 | + |
0 commit comments