Skip to content

Instantly share code, notes, and snippets.

function countCSSRules() {
var results = '',
log = '';
if (!document.styleSheets) {
return;
}
for (var i = 0; i < document.styleSheets.length; i++) {
countSheet(document.styleSheets[i]);
}
function countSheet(sheet) {
@thomsbg
thomsbg / compile_templates.js
Last active December 22, 2015 20:29
Loop through all the view prototype objects in a namespace, transforming their templateId into a template function.
var compileTemplates = function() {
_.each(App.Views, function(View) {
if (View.prototype.templateId) {
// The templateId references the id of a DOM element containing
// the content of the template
var html = jQuery('#' + View.prototype.templateId).html() || '';
View.prototype.template = _.template(html);
}
});
}
@thomsbg
thomsbg / app.js
Last active December 22, 2015 20:29
App initialization with namespaces
// main.js
App = {
// Namespaces
Collections: {},
Models: {},
Views: {},
Mixins: {},
initialize: function() {
this.comments = new App.Collections.Comments({ model: App.Models.Comment });
@thomsbg
thomsbg / main.js
Created September 11, 2013 17:18
App object extends Backbone.Events
// main.js
App = _.extend({}, Backbone.Events, {
...
});
@thomsbg
thomsbg / main.js
Created September 11, 2013 17:39
Global settings object
defaults: {
ajaxTimeout: 15000,
autoUpdateAlertTimeShown: 6500,
editTimeWindow: 90,
maxIndentationDepth: 30,
showAutoUpdateAlert: true,
showAvatars: true,
showSignatures: true,
showImages: true,
sortMethod: 'ancestry'
@thomsbg
thomsbg / main.js
Created September 11, 2013 17:48
Mixing shared ajax functionality into a model
App.Mixins.Ajax = {
...
};
App.Models.Comment = Backbone.extend(_.extend({},
// Mixins
App.Mixins.Ajax, {
// Methods
initialize: function() {
@thomsbg
thomsbg / ajax.js
Last active December 22, 2015 20:29
Backbone ajax mixin
App.Mixins.Ajax = {
// Override urls in the target object to contain a map of
// actionName => /url/path/with/:bound/:segments
urls: {},
// Any options specified here are merged with those you pass to
// this.ajax({...}) before being passed to jQuery.ajax
ajaxOptions: {},
// Return the matching url from the 'urls' object, with /:path/:segments
@thomsbg
thomsbg / delegate_events.js
Created September 11, 2013 18:38
Redefining delegateEvents() on a collection view to handle children's events too
App.Views.CommentList = Backbone.View.extend({
childrenById: {},
childSelector: 'div',
childView: function() {
return App.Views.Comment;
},
// Bind child event handlers once, instead of once per child view
delegateEvents: function() {
// Call super
@thomsbg
thomsbg / comment.js
Last active December 22, 2015 22:39
Using this.ajax inside a model
App.Models.Comment = Backbone.Model.extend(_.extend({}, App.Mixins.Ajax, {
urls: {
flag: '/comments/flag_comment/:id'
},
flag: function(formData) {
this.set('is_flagged', true);
return this.ajax('flag', {
data: formData,
error: function() {
@thomsbg
thomsbg / tentpole.js
Created September 27, 2013 18:50
goTo function
function goTo(page) {
var callback = function(e, adMarkup) {
if (page < bb.current) {
// Insert AFTER the requested page, if moving backwards
page += 1;
}
insertAdBeforePage(adMarkup, page);
};
$(document).on('renderTentpoleFeatureAd', callback);
$(document).trigger('tentpoleFeaturePageFlip', pagesSeen, bb.current, page);