This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Router = Backbone.Router.extend({ | |
routes: { | |
'foo': 'foo' | |
}, | |
foo: function() { | |
console.log('foo() called'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// prevent double-click | |
$(‘button.my-button’).on(‘click’, _.debounce(function() { | |
/* .. code to handle form submition .. */ | |
}, 500, true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var eventListener = _.extend({}, Backbone.Events); | |
var viewA = new Backbone.View({ eventlistener: eventListener }); | |
var viewB = new Backbone.View({ eventlistener: eventListener }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var db = { | |
// will be called only once | |
connect: _.once(function() { | |
/* .. do something .. */ | |
}), | |
find: function(id) { | |
// lazy connection | |
this.connect(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backbone.View.extend({ | |
init: function () { | |
this.subviews = [ | |
new Backbone.View(), | |
new Backbone.View(), | |
new Backbone.View() | |
]; | |
}, | |
disable: function() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// — 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var names = _.pluck(users, 'name') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// — 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// — 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 | |
} | |
} |