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 expensiveFn = _.profile(function(profileId) { | |
var i = 0; | |
// expensive loop | |
while (i < 100) { | |
arr = _.range(0, 50 * 1000); | |
i++; | |
} | |
// expensive loop |
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 loadConversation = _.profile(function(conversationId) { | |
/* .. code profiled .. */ | |
}); |
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
_.mixin({ | |
profile: function(fn) { | |
return function(profileId) { | |
// start profiler | |
console.profile(profileId); | |
// call our real function | |
fn.apply(this, arguments); | |
// stop profiler |
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
/** | |
* Load according conversation in the window | |
*/ | |
var loadConversation = function(conversationId) { | |
// (the code we want to profile) | |
// — send an ajax call to fetch email | |
// — render a templates with email’s informations | |
// — etc .. | |
} |
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({ | |
tagName: 'div', | |
initialize: function() { | |
this.model.on('change:name', function(model, name) { | |
console.log('name is now: ' + 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
/* .. code for router **/ | |
loadView: function(index) { | |
var view = this.instanciateView(index); | |
this.views.push(view); | |
// unbind all events of the previous view to avoid 'ghost' views | |
if (this.currentView) this.previousView.remove(); | |
$('#app').html(view.render().$el); |
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 = new Router(); | |
Backbone.history.start(); | |
// Load first view | |
window.location.hash = '#load/0'; | |
// update model of the first view | |
views[0].model.set('name', 'John'); // => name is now: John | |
setTimeout(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
var Router = Backbone.Router.extend({ | |
init: function() { | |
this.views = []; | |
}, | |
routes: { | |
'load/:index': 'loadView' | |
}, |
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({ | |
tagName: 'div', | |
initialize: function() { | |
this.model.on('change:name', function(model, name) { | |
console.log('name is now: ' + 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
var View = Backbone.View.extend({ | |
initialize: function() { | |
// when model is updated update the DOM | |
this.model.on('change', function(model, params) { | |
// remove ‘createdDate’, ‘id’, ‘updatedDate’ columns from changeset | |
var changeset = _.omit(params.changes, ['createdDate', 'id', 'updatedDate']); | |
if (changeset.status) { | |
this.updateStatus(); |