<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
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |