Front End Development

Day 2
Pascal Rettig / CTO at Fundraise.com / @cykod
http://bitly.com/bss-frontend2

2-Day schedule - Day 2

Client side templating

Rendering with Mustache

OOP In Javascript: JSON

Good: easy. Bad: each object separate

OOP In Javascript: Constructor + Prototype

Good: fast, no memory leak. Bad: no private methods, closure

OOP In Javascript: Constructor

Good: closure, data-hiding. Bad: slower, more memory

Debugging JavaScript

Front-end / Back-end Architectures

  1. HTML Links and Forms - little to no JavaScript, no client side rendering

  2. Ajax - Some JavaScript, still server-side rendering

  3. JavaScript MVC - Generally only client side rendering

  4. Hybrid - Both client and server-side rendering

Which is best?

It depends on the application.

Backbone.js is a JavaScript MVC-ish framework

  1. Provides a way to structure and organize your JavaScript

  2. Makes it easier to build larger JavaScript applictions

  3. Has a hard dependency on underscore.js

  4. Has a nice, documented code-base at Backbonejs.org

  5. Canonical ToDo example

Rendering with Mustache self-contained

Kinvey

Facebook

First: Facebook Javascript SDK

Facebook has a number of useful API's, we'll be using two:

  1. Facebook Javscript SDK

    A way of interfacing with Facebook entirely via Javascript (Yay!)

  2. Facebook Graph API

    Their main API for accessing and posting data

  3. All require we make a new facebook application

Facebook Javascript API Basics

Api Setup (After creating an application here: https://developers.facebook.com/apps

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({
    appId  : 'YOUR APP ID',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true, // parse XFBML
    channelUrl : 'http://WWW.MYDOMAIN.COM/channel.html', // channel.html file
    oauth  : true // enable OAuth 2.0
  });
</script>

Channel.html file:

 <script src="http://connect.facebook.net/en_US/all.js"></script>

Facebook Javascript API Basics #2

Then, we can force a login:

<script>
FB.login(function(response) {
   if (response.authResponse) {
     console.log('Welcome!  Fetching your information.... ');
     alert("Ur Logged in!");
   } else {
     console.log('User cancelled login or did not fully authorize.');
   }
 }, {scope: 'email'});
</script>

after we're logged in we can call the Graph API:

<script>
     FB.api('/me', function(response) {
       console.log('Good to see you, ' + response.name + '.');
     });
</script>

Graph API

  1. The preferred method to pull data in and out of facebook

  2. Reasonably well documented: Graph API

  3. Everything has a graph id - either a numeric ID or a name: http://graph.facebook.com/pascalrettig or 'http://graph.facebook.com/678578882

Before we go too much further...

Let's play around with the Facebook Graph Api via
Graph API Explorer

Let's connect With Facebook

Let's grab some more data, and the print it out

We can access the data variable in the console via: $("iframe")[0].contentWindow.responseData

Project