Skip to content

Commit

Permalink
@leplatrem review.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémy HUBSCHER committed Dec 9, 2015
1 parent 51f1bc5 commit 41d6733
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
48 changes: 35 additions & 13 deletions calendar.js
@@ -1,6 +1,8 @@
$(document).ready(function() {
// Mozilla demo server (flushed every day)
var server = "https://kinto.dev.mozaws.net/v1";
var bucket_id = "default";
var collection_id = "kinto_demo_calendar"

// Pusher app key
var pusher_key = "01a9feaaf9ebb120d1a6";
Expand All @@ -9,16 +11,15 @@ $(document).ready(function() {
var authorization = "Basic " + btoa("public:notsecret");

// Kinto client with sync options.
var kinto = new Kinto({remote: server, headers: {Authorization: authorization}});
var kinto = new Kinto({remote: server, bucket: bucket_id,
headers: {Authorization: authorization}});

// Local store in IndexedDB.
var store = kinto.collection("kinto_demo_calendar");

// Live changes.
var pusher = new Pusher(pusher_key, {
encrypted: true
});
var store = kinto.collection(collection_id);

// Setup live-sync!
getBucketId()
.then(setupLiveSync);

//
// Initialize fullCalendar
Expand Down Expand Up @@ -172,11 +173,32 @@ $(document).ready(function() {
});
}

// This is the default bucket id for the ``public:notsecret`` user on kinto.dev
var channelName = '94b660c6-68f3-83c4-97fa-8e8ac4715204-kinto_demo_calendar-record';
var channel = pusher.subscribe(channelName);
channel.bind_all(function() {
syncServer();
});
// Live changes.
function getBucketId() {
// When using the `default` bucket, we should resolve its real id
// to be able to listen to notifications.
if (bucket_id != "default")
return Promise.resolve(bucket_id);

return fetch(server + '/', {headers: {Authorization: authorization}})
.then(function (result) {
return result.json();
})
.then(function (result) {
return result.user.bucket;
});
}

function setupLiveSync(bucket_id) {
var pusher = new Pusher(pusher_key, {
encrypted: true
});

var channelName = `${bucket_id}-${collection_id}-record`;
var channel = pusher.subscribe(channelName);
channel.bind_all(function() {
syncServer();
});
}

});
2 changes: 1 addition & 1 deletion index.html
Expand Up @@ -8,7 +8,7 @@
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.2/moment.min.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.1.0/fullcalendar.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.1.0/fullcalendar.js"></script>
<script src="https://js.pusher.com/3.0/pusher.min.js"></script>
<script src="//js.pusher.com/3.0/pusher.min.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
Expand Down

0 comments on commit 41d6733

Please sign in to comment.