JSON{
"name": "Narciso Jaramillo",
"birthday": "1932-10-05T03:52:12Z",
"address": {
"street": "123 Anywhere Place",
"city": "Somewhere",
zip: 92153
}
}
HTML<script id=“blogPostTemplate” type=“text/x-handlebars-template”>
<div class=“entry”>
<h1>{{title}}</h1>
<div class=“body”>
{{body}}
</div>
</div>
</script>
var source = $(‘#blogPostTemplate’).html();
var postTemplate = Handlebars.compile(source);
$.ajax(postURL, {
success: function(data) {
// data = { title: "My Post", body: "..." }
$(‘#postContent’).html(postTemplate(data));
})
});JavaScript
![]() |
Module loading |
![]() |
DOM manipulation, AJAX |
![]() |
CSS grid/lightweight components |
![]() |
Mobile UI components |
![]() |
Client-side templating |
![]() |
Lightweight MV(C) |
localStorage.setItem("name", "Narciso Jaramillo");
// later...
var name = localStorage.getItem("name");
localStorage.removeItem("name");
HTML<html manifest="example.appcache">
...
</html>
ManifestCACHE MANIFEST
# 2013-01-14:v1
index.html
stylesheet.css
images/logo.png
scripts/main.js
main.jsvar worker = new Worker('doWork.js');
worker.addEventListener('message', function(e) {
console.log('Worker said: ', e.data);
}, false);
worker.postMessage('Hello World'); // Send data to our worker.
doWork.jsself.addEventListener('message', function(e) {
self.postMessage('I heard: ' + e.data);
}, false);
var connection = new WebSocket("ws://echo.websocket.org/");
connection.onopen = function () {
connection.send("Hello there");
};
connection.onmessage = function (e) {
alert("Got message: " + e.data);
};
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
"Everything runs in parallel, except your code"
qunitjs.com | pivotal.github.com/jasmine | visionmedia.github.com/mocha
CoffeeScriptsquare = (x) -> x * x
cube = (x) -> square(x) * x
JavaScriptvar cube, square;
square = function(x) {
return x * x;
};
cube = function(x) {
return square(x) * x;
};
CoffeeScriptcubes = (math.cube num
for num in list)
JavaScriptcubes = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = list.length; _i < _len; _i++) {
num = list[_i];
_results.push(math.cube(num));
}
return results;
})();
is.gd/webdesignhipster
is.gd/webdevhipster
github.com/njx/presos
Built with reveal.js
JavaScriptvar myObject = {
name: "Narciso Jaramillo",
birthday: new Date(1932, 10, 5),
address: {
street: "123 Anywhere Place",
city: "Somewhere",
zip: 92153
}
};
myObject.newProp = "some value";
var fetchNewContent = function(url, container) {
$.ajax(url, {
success: function(data) {
$(container).html(data);
}
});
}
Counter.countUp();
Counter.countUp();
Counter.getCount(); // 2
var Counter = (function() {
var count = 1; // private
return {
countUp: function() {
count++;
},
getCount: function() {
return count;
}
};
})();
var obj1 = { val: 100 },
obj2 = { val: 200 };
function log() {
console.log(this.val);
}
log(); // error: "val" is undefined
obj1.log = obj2.log = log;
obj1.log(); // 100
obj2.log(); // 200
HTML<input id="myButton" value="Click Me">
JavaScriptvar myButton = {
elt: document.getElementById("myButton"),
value: "myValue",
handleClick: function() {
alert(this.value);
},
init: function() {
this.elt.onclick = this.handleClick;
}
};
myButton.init();
myButton.handleClick(); // works, this === myButton
// but clicking on the button shows "Click Me"
// because this === elt
var myButton = {
element: document.getElementById(‘myButton’),
value: ‘myValue’,
handleClick: function() {
alert(this.value);
}
init: function() {
this.element.onclick = this.handleClick.bind(this);
}
};
var myButton = {
element: document.getElementById(‘myButton’),
value: ‘myValue’,
init: function() {
var self = this;
this.element.onclick = function() {
alert(self.value);
};
}
};
var Counter = function() {
this.counter = 1;
};
Counter.prototype.countUp = function() {
this.counter++;
};
Counter.prototype.getCount = function() {
return this.counter;
};
var myCounter = new Counter();
myCounter.countUp();
var debugMixin = {
log: function () {
console.log(this.toString());
},
alert: function () {
alert(this.toString());
}
};
var myObj = {
// ...
};
$.extend(myObj, debugMixin);
myObj.log();
Prototypes: j.mp/jsproto
Mixins: is.gd/jsmixins
QUnittest("countUp adds 1",
function() {
Counter.reset();
Counter.countUp();
equal(Counter.getCount(), 1);
}
);
Jasmineit("should add 1",
function() {
Counter.reset();
Counter.countUp();
expect(Counter.getCount())
.toBe(1);
}
);
CoffeeScriptsquare = (x) -> x * x
cube = (x) -> square(x) * x
JavaScriptvar cube, square;
square = function(x) {
return x * x;
};
cube = function(x) {
return square(x) * x;
};
CoffeeScriptcubes = (math.cube num
for num in list)
JavaScriptcubes = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = list.length; _i < _len; _i++) {
num = list[_i];
_results.push(math.cube(num));
}
return results;
})();
var Todo = Backbone.Model.extend({
defaults: {
title: "empty todo..."
}
});
var TodoView = Backbone.View.extend({
initialize: function() {
this.listenTo(this.model, "change", this.render);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
var myTodo = new Todo({ title: "Buy milk" });
var myTodoView = new TodoView({ model: myTodo });
$("#todolist").append(myTodoView.render().$el);
myTodo.set("title", "Buy lowfat milk"); // view will update