Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Pebble.addEventListener('ready', function() {
- console.log('PebbleKit JS ready!');
- // An hour ahead
- var date = new Date();
- date.setHours(date.getHours() + 1);
- // Create the pin
- var pin = {
- "id": "pin-" + Math.round((Math.random() * 100000)),
- "time": date.toISOString(),
- "layout": {
- "type": "genericPin",
- "title": "Example Pin",
- "tinyIcon": "system://images/SCHEDULED_EVENT"
- }
- };
- console.log('Inserting pin in the future: ' + JSON.stringify(pin));
- insertUserPin(pin, function(responseText) {
- console.log('Result: ' + responseText);
- });
- });
- /******************************* timeline lib *********************************/
- // The timeline public URL root
- var API_URL_ROOT = 'https://timeline-api.getpebble.com/';
- /**
- * Send a request to the Pebble public web timeline API.
- * @param pin The JSON pin to insert. Must contain 'id' field.
- * @param type The type of request, either PUT or DELETE.
- * @param callback The callback to receive the responseText after the request has completed.
- */
- function timelineRequest(pin, type, callback) {
- // User or shared?
- var url = API_URL_ROOT + 'v1/user/pins/' + pin.id;
- // Create XHR
- var xhr = new XMLHttpRequest();
- xhr.onload = function () {
- console.log('timeline: response received: ' + this.responseText);
- callback(this.responseText);
- };
- xhr.open(type, url);
- // Get token
- Pebble.getTimelineToken(function(token) {
- // Add headers
- xhr.setRequestHeader('Content-Type', 'application/json');
- xhr.setRequestHeader('X-User-Token', '' + token);
- // Send
- xhr.send(JSON.stringify(pin));
- console.log('timeline: request sent.');
- }, function(error) { console.log('timeline: error getting timeline token: ' + error); });
- }
- /**
- * Insert a pin into the timeline for this user.
- * @param pin The JSON pin to insert.
- * @param callback The callback to receive the responseText after the request has completed.
- */
- function insertUserPin(pin, callback) {
- timelineRequest(pin, 'PUT', callback);
- }
- /**
- * Delete a pin from the timeline for this user.
- * @param pin The JSON pin to delete.
- * @param callback The callback to receive the responseText after the request has completed.
- */
- function deleteUserPin(pin, callback) {
- timelineRequest(pin, 'DELETE', callback);
- }
- /***************************** end timeline lib *******************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement