Skip to content

Instantly share code, notes, and snippets.

var View = Backbone.View.extend({
initialize: function() {
// when model is updated update the DOM
this.model.on('change', this.render.bind(this));
},
render: function() {
// do rendering
}
var Router = Backbone.Router.extend({
routes: {
'foo': 'foo'
},
foo: function() {
console.log('foo() called');
}
});
// prevent double-click
$(‘button.my-button’).on(‘click’, _.debounce(function() {
/* .. code to handle form submition .. */
}, 500, true);
var eventListener = _.extend({}, Backbone.Events);
var viewA = new Backbone.View({ eventlistener: eventListener });
var viewB = new Backbone.View({ eventlistener: eventListener });
var db = {
// will be called only once
connect: _.once(function() {
/* .. do something .. */
}),
find: function(id) {
// lazy connection
this.connect();
Backbone.View.extend({
init: function () {
this.subviews = [
new Backbone.View(),
new Backbone.View(),
new Backbone.View()
];
},
disable: function() {
// — Vanilla Javascript (7 loc)
function getHigherKarma(name) {
var user
for (var i = 0; i < users.length) {
if (users[i].name === name && users[i].karma > higherKarma) {
user = users[i]
}
}
return user
}
var names = _.pluck(users, 'name')
// — Vanilla Javascript (5 loc)
function getFullNames() {
var names = []
for (var i = 0; i < users.length; i++) {
names.push(user.name + ' ' + user.lastName
}
return names
}
// — Underscore version (3 loc)
// — Vanilla Javascript (9 loc)
function findByName(name) {
var found
for (var i = 0; i < users.length; i++) {
var user = users[i]
if (user.name === name) {
found = user
break
}
}