Skip to content

Instantly share code, notes, and snippets.

var expensiveFn = _.profile(function(profileId) {
var i = 0;
// expensive loop
while (i < 100) {
arr = _.range(0, 50 * 1000);
i++;
}
// expensive loop
var loadConversation = _.profile(function(conversationId) {
/* .. code profiled .. */
});
_.mixin({
profile: function(fn) {
return function(profileId) {
// start profiler
console.profile(profileId);
// call our real function
fn.apply(this, arguments);
// stop profiler
/**
* 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 ..
}
var View = Backbone.View.extend({
tagName: 'div',
initialize: function() {
this.model.on('change:name', function(model, name) {
console.log('name is now: ' + name);
});
},
/* .. 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);
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() {
var Router = Backbone.Router.extend({
init: function() {
this.views = [];
},
routes: {
'load/:index': 'loadView'
},
var View = Backbone.View.extend({
tagName: 'div',
initialize: function() {
this.model.on('change:name', function(model, name) {
console.log('name is now: ' + name);
});
},
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();