Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Uncomplexify your app with Polymer Starter Kit

Rob Dodson
August 09, 2015

Uncomplexify your app with Polymer Starter Kit

In this day and age, just getting started with a new project is a huge process. What build tools should you use? How will you deal with offline caching? What about storing data and user authentication? On the Polymer team, we've been working on a set of tools and scaffolds to make starting your next project pain free. In this session I'll introduce you to Polymer Starter Kit, and demonstrate how to combine it with amazing services like Firebase, and new standards like Service Worker for offline storage and sync. With these new tools, we're going to remove complexity from your dev environment and get you back on track building your next great idea.

Rob Dodson

August 09, 2015
Tweet

More Decks by Rob Dodson

Other Decks in Technology

Transcript

  1. when presented with an abundance of choice, you can feel

    increasingly unsure of whether yours is the right one - Addy Osmani
  2. // While there is only one cache in this example,

    the same logic will handle the case where // there are multiple versioned caches. var expectedCacheNames = Object.keys(CURRENT_CACHES).map(function(key) { return CURRENT_CACHES[key]; }); event.waitUntil( caches.keys().then(function(cacheNames) { return Promise.all( cacheNames.map(function(cacheName) { if (expectedCacheNames.indexOf(cacheName) == -1) { // If this cache name isn't present in the array of "expected" cache names, then delete it. console.log('Deleting out of date cache:', cacheName); return caches.delete(cacheName); } }) ); }) ); }); // This sample illustrates an aggressive approach to caching, in which every valid response is
  3. Service Worker caching Take your app offline with ease <link

    rel="import" href="platinum-sw-elements.html"> <platinum-sw> <platinum-sw-cache default-cache-strategy="networkFirst" precache='["image.jpg", "results.json", "page.html"]'> </platinum-sw-cache> </platinum-sw>
  4. index.html <body> <template is="dom-bind" id="app"> … </template> </body> var app

    = document.querySelector(‘#app’); app.todos = [ { todo: ‘Go for a jog’, isComplete: false }, { todo: ‘Get some lunch’, isComplete: true }. … ]; app.js Our single source of truth
  5. todo-item.html <dom-module id="todo-item"> <template> </template> <script> Polymer({ is: 'todo-item' });

    </script> </dom-module> <link rel=“import” href=“../polymer/polymer.html”>
  6. todo-item.html <dom-module id="todo-item"> <template> </template> <script> Polymer({ is: 'todo-item' });

    </script> </dom-module> Local DOM is the DOM an elements is in charge of creating and managing <link rel=“import” href=“../polymer/polymer.html”>
  7. todo-item.html <dom-module id="todo-item"> <template> <paper-checkbox checked></paper-checkbox> <span>…</span> </template> <script> Polymer({

    is: 'todo-item' }); </script> </dom-module> <link rel=“import” href=“../polymer/polymer.html”> <link rel=“import” href=“../paper-checkbox/paper-checkbox.html”>
  8. todo-item.html <dom-module id="todo-item"> <template> <paper-checkbox checked></paper-checkbox> <span>…</span> </template> <script> Polymer({

    is: 'todo-item' }); </script> </dom-module> <link rel=“import” href=“../polymer/polymer.html”> <link rel=“import” href=“../paper-checkbox/paper-checkbox.html”> Import additional dependencies
  9. todo-item.html <dom-module id="todo-item"> <template> <paper-checkbox checked></paper-checkbox> <span>…</span> </template> <script> Polymer({

    is: 'todo-item' }); </script> </dom-module> <link rel=“import” href=“../polymer/polymer.html”> <link rel=“import” href=“../paper-checkbox/paper-checkbox.html”> Add ‘em to your template
  10. todo-item.html <dom-module id="todo-item"> <template> <paper-checkbox checked></paper-checkbox> <span>…</span> </template> <script> Polymer({

    is: 'todo-item' }); </script> </dom-module> <link rel=“import” href=“../polymer/polymer.html”> <link rel=“import” href=“../paper-checkbox/paper-checkbox.html”>
  11. todo-item.html <dom-module id="todo-item"> <template> <paper-checkbox checked></paper-checkbox> <span>…</span> </template> <script> Polymer({

    is: 'todo-item' }); </script> </dom-module> <link rel=“import” href=“../polymer/polymer.html”> <link rel=“import” href=“../paper-checkbox/paper-checkbox.html”> These values should be passed in
  12. todo-item.html <dom-module id="todo-item"> <template> <paper-checkbox checked></paper-checkbox> <span>…</span> </template> <script> Polymer({

    is: 'todo-item', properties: { todo: String, isComplete: { type: Boolean, default: false } } }); </script> </dom-module> Properties!
  13. todo-item.html <dom-module id="todo-item"> <template> <paper-checkbox checked="[[isComplete]]"></paper-checkbox> <span>[[todo]]</span> </template> <script> Polymer({

    is: 'todo-item', properties: { todo: String, isComplete: { type: Boolean, default: false } } }); </script> </dom-module> Bindings are one-way*
  14. todo-item.html <dom-module id="todo-item"> <template> <paper-checkbox checked="[[isComplete]]"></paper-checkbox> <span>[[todo]]</span> </template> <script> Polymer({

    is: 'todo-item', properties: { todo: String, isComplete: { type: Boolean, default: false } } }); </script> </dom-module> Bindings are one-way* * two-way also supported
  15. todo-item.html <dom-module id="todo-item"> <template> <paper-checkbox checked="[[isComplete]]" on-tap="_notifyCheckedToggled"></paper-checkbox> <span>[[todo]]</span> </template> <script>

    Polymer({ is: 'todo-item', properties: {…}, _notifyCheckedToggled: function() { this.fire(‘todo-item-checked-toggled’); } }); </script> </dom-module> Wire-up handlers declaratively
  16. Data flows in via one-way bindings Changes flow out via

    events User interacts with the element
  17. Data flows in via one-way bindings Changes flow out via

    events Application state is modified User interacts with the element
  18. Data flows in via one-way bindings Changes flow out via

    events Application state is modified User interacts with the element
  19. Finish talk so we can play Go to the grocery

    store Take the recycling out Book a flight for upcoming Contemplate world domina Steal all of Addy’s ideas <todo-list> A collection of components
  20. todo-list.html <dom-module id="todo-list"> <template> <template is="dom-repeat" items="[[todos]]"> </template> </template> <script>

    Polymer({ is: 'todo-list', properties: { todos: Array } }); </script> </dom-module> Stamp out DOM, for each item
  21. todo-list.html <dom-module id="todo-list"> <template> <template is="dom-repeat" items="[[todos]]"> <todo-item todo=“[[item.todo]]" is-complete="[[item.isComplete]]">

    </todo-item> </template> </template> <script> Polymer({ is: 'todo-list', properties: { todos: Array } }); </script> </dom-module>
  22. todo-list.html <dom-module id="todo-list"> <template> <template is="dom-repeat" items="[[todos]]"> <todo-item todo=“[[item.todo]]" is-complete="[[item.isComplete]]">

    </todo-item> </template> </template> <script> Polymer({ is: 'todo-list', properties: { todos: Array } }); </script> </dom-module> Pass data on to todo-items
  23. Go for a jog Work on slide deck Upgrade Firebase

    account Buy some coffee Grab some lunch Close some GitHub bugs
  24. index.html <body> <template is="dom-bind" id="app"> … </template> </body> var app

    = document.querySelector(‘#app’); app.todos = [ { todo: ‘Go for a jog’, isComplete: false }, { todo: ‘Get some lunch’, isComplete: true }. … ]; app.js Our single source of truth
  25. index.html <body> <template is="dom-bind" id="app"> <todo-list todos="[[todos]]"></todo-list> </template> </body> var

    app = document.querySelector(‘#app’); app.todos = [ { todo: ‘Go for a jog’, isComplete: false }, { todo: ‘Get some lunch’, isComplete: true }. … ]; app.js Our single source of truth
  26. // Write some data ref.set({ name: ‘Rob Dodson’ }); //

    Push Array-like data ref.push({ isComplete: false, todo: ‘A new todo!’ }); app.js // Create a connection to Firebase var ref = new Firebase(‘https://<YOUR-FIREBASE-APP>.firebaseio.com');
  27. index.html <body> <template is="dom-bind" id="app"> <todo-list todos="[[todos]]"></todo-list> </template> </body> var

    app = document.querySelector(‘#app’); app.todos = [ { todo: ‘Go for a jog’, isComplete: false }, { todo: ‘Get some lunch’, isComplete: true }. … ]; app.js Our single source of truth
  28. app.js this.ref.on('value', this.renderTodos.bind(this)); app.renderTodos = function(snapshot) { this.todos = [];

    snapshot.forEach(function(childSnapshot) { var item = childSnapshot.val(); this.push('todos', item); }.bind(this)); };
  29. { "rules": { "users": { "$uid": { ".write": "auth.uid ===

    $uid", ".read": "auth.uid === $uid" } } } }
  30. app.js // Authenticate a user with Google Sign in ref.authWithOAuthPopup('google',

    function(error, authData) { console.log(authData); });
  31. app.js // Check to see if the user is already

    signed in var authData = this.ref.getAuth();
  32. app.js // Check to see if the user is already

    signed in var authData = this.ref.getAuth(); if (authData) { // Get the user’s todo list this.userRef = this.ref.child(‘users/‘ + authData.uid); } else { // Show sign in screen this.$.signInDialog.open(); }
  33. code_slides.txt For format coloring, visit j.mp/iohighlighter in Safari
 and copy

    + paste into Keynote If your code snippet is over 6 lines, use a full- page slide of code in this style code_slides.txt protected void onTryUpdate(int reason) throws RetryException { // Do some awesome stuff int foo = 15; publishArtwork(new Artwork.Builder() .title(photo.name) .imageUri(Uri.parse(photo.image_url)) .viewIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://500px.com/photo/" + photo.id))) .build()); scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS); }