Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Ember Analytics Mini Demo</title>
  <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/2.1.0/normalize.css">
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v1.2.1.js"></script>
  <script src="http://builds.emberjs.com/tags/v1.4.0/ember.js"></script>
</head>
<body>
  <script type="text/x-handlebars">
    <h2>Ember Analytics Mini Demo</h2>
    <nav>
      {{link-to 'Home' 'index'}} |
      {{link-to 'About' 'about'}} |
      {{link-to 'Products' 'products'}} |
      {{link-to 'Help' 'help'}}
    </nav>
    {{outlet}}
  </script>
  <script type="text/x-handlebars" data-template-name="index">
    <h2>home</h2>
  </script>
  <script type="text/x-handlebars" data-template-name="about">
    <h2>about</h2>
    
    <nav>
      {{link-to 'Him' 'about.him'}} |
      {{link-to 'Her' 'about.her'}}
    </nav>
    
    {{outlet}}
  </script>
  
  <script type="text/x-handlebars" data-template-name="about/him">
    <h2>about/him</h2>
    
    <button {{action 'track' 'like him'}}>Like Him</button>
  </script>
  
  <script type="text/x-handlebars" data-template-name="about/her">
    <h2>about/her</h2>
    
    <button {{action 'track' 'like her'}}>Like Her</button>
  </script>
  
  <script type="text/x-handlebars" data-template-name="products">
    <h2>showing a random product...</h2>
    
    {{outlet}}
  </script>
  
  <script type="text/x-handlebars" data-template-name="products/product">
    <p class="product">{{color}} {{name}}</p>
  </script>
  
  <script type="text/x-handlebars" data-template-name="help">
    <h2>help</h2>
  </script>
  <script type="text/x-handlebars" data-template-name="loading">
    <p><i>Loading...</i></p>
  </script>
  
  <script type="text/x-handlebars" data-template-name="error">
    <h2 class="error">Error!</h2>
  </script>
</body>
</html>
 
/* Put your CSS here */
html, body {
    margin: 20px;
}
.product {
  text-align: center;
  border: 3px dashed gold;
  padding: 1em 0;
}
.error {
  color: red;
}
 
/*=== Setup ===*/
App = Ember.Application.create();
App.Router.map(function() {
  this.resource('about', function() {
    this.route('him');
    this.route('her');
  });
  this.resource('products', function() {
    this.route('product', { path: '/:color/:name' })
  });
  this.resource('help');
});
/*=== Model/Fixture data ===*/
function Product(color, name) {
  this.color = color;
  this.name = name;
}
Product.FIXTURES = [];
Product.COLOR = "blue yellow cyan purple orange red black white brown teal green gold chartreuse".w();
Product.NAME = "apple pizza sandwich xbox cellphone book waffle skateboard guitar mansion yacht toothbrush ferrari taco suit basketball macbook necklace basket".w();
Product.COLOR.forEach(function(color) {
  Product.NAME.forEach(function(name) {
    Product.FIXTURES.push(new Product(color, name));
  });
});
Product.findByType = function(color, name) {
  return Product.FIXTURES.filter(function(product) {
    return product.color === color && product.name === name;
  });
};
/*=== Analytics ===*/
/**
Event Tracking
------------------
A naive solution would be to add {{action}} handlers on the specific elements within each template then bubble up to ApplicationRoute and track.
 -Litters templates with analytics-specific actions
 -No encapsulation so the code begins to fragment extrememly quickly
 -Doesn't scale well for a larger applications
 
Not good!
*/
App.ApplicationRoute = Ember.Route.extend({
  actions: {
    track: function(data) {
      console.log(data);
      //ga('send', 'event', event.target.nodeName, event.type, event.target.className, data);
    }
  }
});
/*=== App Code ===*/
App.ProductsIndexRoute = Ember.Route.extend({
  redirect: function() {
    var color = Product.COLOR[Math.floor(Math.random()*Product.COLOR.length)],
        name = Product.NAME[Math.floor(Math.random()*Product.NAME.length)];
    this.transitionTo('products.product', color, name);
  }
});
App.ProductsProductRoute = Ember.Route.extend({
  model: function(params) {
    return Product.findByType(params.color, params.name)[0];
  }
});
App.HelpRoute = Ember.Route.extend({
  model: function() {
    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.run.later(function() {
        reject({ msg: "Help Route Doesn't Work!!!" });
      }, 2000);
    });
  }
});
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
alexdilibertopro
0viewers