|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head lang="en"> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>js-data-firebase & Riot</title> |
| 6 | + <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"> |
| 7 | + <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/default.min.css"> |
| 8 | + <link href="./example.css" rel="stylesheet"> |
| 9 | + <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script> |
| 10 | + <script src="https://cdn.firebase.com/js/client/2.1.2/firebase.js"></script> |
| 11 | + <script src="https://github.com/js-data/js-data/releases/download/2.0.0-rc.1/js-data.min.js"></script> |
| 12 | + <script src="https://github.com/js-data/js-data-firebase/releases/download/2.0.0-rc.1/js-data-firebase.min.js"></script> |
| 13 | + <script src="//cdn.jsdelivr.net/g/riot@2.2(riot.min.js+compiler.min.js)"></script> |
| 14 | + <script src="./example.tag" type="riot/tag"></script> |
| 15 | +</head> |
| 16 | +<body> |
| 17 | +<div class="example-masthead"> |
| 18 | + <div class="container"> |
| 19 | + <nav class="example-nav"> |
| 20 | + <a class="example-nav-item" href="http://www.js-data.io">js-data.io</a> |
| 21 | + <a class="example-nav-item" href="http://www.js-data.io/docs/dsfirebaseadapter">API Documentation</a> |
| 22 | + <a class="example-nav-item" href="https://github.com/js-data/js-data-firebase">GitHub</a> |
| 23 | + <a class="example-nav-item" href="https://groups.io/org/groupsio/jsdata">Mailing List</a> |
| 24 | + <a class="example-nav-item" href="https://github.com/js-data/js-data-firebase/issues">Issues</a> |
| 25 | + <a class="example-nav-item" href="../angular">js-data-firebase & Angular</a> |
| 26 | + <a class="example-nav-item" href="../react">js-data-firebase & React</a> |
| 27 | + <a class="example-nav-item active" href="#">js-data-firebase & Riot</a> |
| 28 | + </nav> |
| 29 | + </div> |
| 30 | +</div> |
| 31 | +<div class="container" style="margin-top: 100px"> |
| 32 | + <div class="col-sm-5"> |
| 33 | + <h2 class="page-header">js-data-firebase & Riot example</h2> |
| 34 | + <example></example> |
| 35 | + </div> |
| 36 | + <div> |
| 37 | +<pre><code class="javascript" style="overflow-x: auto"> |
| 38 | + // RiotJs: https://muut.com/riotjs/ |
| 39 | + var self = this |
| 40 | + self.userNameToAdd = '' |
| 41 | + var store = new JSData.DS() |
| 42 | + var firebaseAdapter = new DSFirebaseAdapter({ |
| 43 | + basePath: 'https://js-data-firebase.firebaseio.com' |
| 44 | + }) |
| 45 | + store.registerAdapter('firebase', firebaseAdapter, { default: true }) |
| 46 | + |
| 47 | + var UserStore = store.defineResource({ |
| 48 | + name: 'user', |
| 49 | + afterInject: function () { |
| 50 | + UserStore.emit('change') |
| 51 | + }, |
| 52 | + afterEject: function () { |
| 53 | + UserStore.emit('change') |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + for (var resourceName in store.definitions) { |
| 58 | + var Resource = store.definitions[resourceName] |
| 59 | + var ref = firebaseAdapter.ref.child(Resource.endpoint) |
| 60 | + // Inject items into the store when they're added to Firebase |
| 61 | + // Update items in the store when they're modified in Firebase |
| 62 | + ref.on('child_changed', function (dataSnapshot) { |
| 63 | + var data = dataSnapshot.val() |
| 64 | + if (data[Resource.idAttribute]) { |
| 65 | + Resource.inject(data) |
| 66 | + } |
| 67 | + }) |
| 68 | + // Eject items from the store when they're removed from Firebase |
| 69 | + ref.on('child_removed', function (dataSnapshot) { |
| 70 | + var data = dataSnapshot.val() |
| 71 | + if (data[Resource.idAttribute]) { |
| 72 | + Resource.eject(data[Resource.idAttribute]) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + UserStore.on('change', function() { |
| 78 | + self.users = UserStore.getAll() |
| 79 | + self.update() |
| 80 | + }) |
| 81 | + |
| 82 | + self.on('mount', function() { |
| 83 | + // Pull the initial list of users from Firebase |
| 84 | + UserStore.findAll() |
| 85 | + }) |
| 86 | + |
| 87 | + edit(e) { |
| 88 | + self.userNameToAdd = e.target.value |
| 89 | + } |
| 90 | + |
| 91 | + createUser() { |
| 92 | + if (self.userNameToAdd) { |
| 93 | + UserStore.create({ name: self.userNameToAdd }).then(function () { |
| 94 | + self.userNameToAdd = self.userNameInput.value = '' |
| 95 | + }) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + removeUser(e) { |
| 100 | + UserStore.destroy(e.item.id) |
| 101 | + } |
| 102 | +</code></pre> |
| 103 | + </div> |
| 104 | +</div> |
| 105 | +<script> |
| 106 | +riot.mount('*') |
| 107 | +hljs.initHighlightingOnLoad(); |
| 108 | +</script> |
| 109 | +</body> |
| 110 | +</html> |
0 commit comments