Skip to content

Instantly share code, notes, and snippets.

View sitetechie's full-sized avatar

Peter de Vos sitetechie

View GitHub Profile
@iros
iros / backbonepaginatedcollection.js
Created January 20, 2012 16:08
Backbone Paginated Collection
/**
* A useful little pagination collection extension. Handles two
* types of collections:
* 1. Those where the number of pages is discoered on first page
* 2. Those whose number of pages is known in advance.
* Used by startup Collection and Trends collection.
*/
ST.Collections.PaginatedCollection = Backbone.Collection.extend({
initialize : function(attributes, options) {
@plagi
plagi / Backbone.Subset.js
Created January 10, 2012 11:51
Implements an imaginary subset of a Backbone Collection (as superset)
/*
@date 05/31/2011
@class Backbone.Subset
@name Backbone Subset
@desc
Implements an imaginary subset of a Backbone Collection (as superset)
*/
// Extend the default Backbone.Collection
@tbranyen
tbranyen / get-or-create.js
Created December 28, 2011 05:34
Backbone.Collection.prototype.getOrCreate
Backbone.Collection.prototype.getOrCreate = function(model, options) {
var _model;
// Ensure model has an id property and attempt to get out of the collection
if (model.id && _model = this.get(model.id)) {
options && options.success(_model);
return _model;
}
@addyosmani
addyosmani / pubsub.md
Created October 28, 2011 06:49
Four ways to do Pub/Sub with jQuery 1.7 and jQuery UI (in the future)

#Four Ways To Do Pub/Sub With jQuery and jQuery UI (in the future)

Between jQuery 1.7 and some of work going into future versions of jQuery UI, there are a ton of hot new ways for you to get your publish/subscribe on. Here are just four of them, three of which are new.

(PS: If you're unfamiliar with pub/sub, read the guide to it that Julian Aubourg and I wrote here http://msdn.microsoft.com/en-us/scriptjunkie/hh201955.aspx)

##Option 1: Using jQuery 1.7's $.Callbacks() feature:

$.Callbacks are a multi-purpose callbacks list object which can be used as a base layer to build new functionality including simple publish/subscribe systems. We haven't yet released the API documentation for this feature just yet, but for more information on it (including lots of examples), see my post on $.Callbacks() here:

@tbranyen
tbranyen / fetch-template.js
Created September 26, 2011 04:33
fetching a template asynchronously
var fetchTemplate = function() {
var template = {};
return function(path, callback) {
template.callback = callback;
// Exit early if template is already fetched
if (template.contents && template.callback) {
template.callback.call(this);
delete template.callback;
@tbranyen
tbranyen / app.js
Created September 22, 2011 16:51
backbone.js sub routing
/* Pretend app setup stuff is here */
/* Kick off app */
jQuery(function($) {
var Gallery = app.module("gallery");
app.Router = Backbone.Router.extend({
initialize: function() {
this.gallery = new Gallery.Router("gallery/");
@tbranyen
tbranyen / async-module.js
Created August 29, 2011 14:05
async module loading in backbone
your_namespace = {
// Create a new module reference scaffold or load an
// existing module.
module: function(name) {
var that = this;
var path_to_modules = 'app/modules/';
return $.Deferred(function(def) {
if (that._modules[name]) {
@wesen
wesen / backbone-mixin.js
Created August 21, 2011 10:38
Merge backbone views (mixin pattern)
/**
* ## Merging mixin views in backbone.js ##
*
* really just more a test for tumblr gistr
*/
/**
* Merge the mixin (a Backbone.View) into another Backbone.View. Automatically merge events, defaults, and call the parent initializer.
**/
function mergeMixin(view, mixin) {
@tbranyen
tbranyen / proto.js
Created August 12, 2011 05:45
portable __proto__ setter emulator
// Mutable __proto__ polyfill
Object.setPrototypeOf = function(obj, original) {
var f, i;
function F() {}
F.prototype = original;
f = new F();
for (i in obj) {
@szimek
szimek / backbone_extensions.js
Created June 15, 2011 10:20
Extensions for Backbone.
// TODO:
// - TemplateView
// - change the way templates are compiled (store compiled version inside view "class"?)
//
// - CollectionView
// - cleanup layout rendering if it depends on collection (remove collection container from DOM, rerender the layout and attach it again?)
// - store model views together with models (?)
//
// - ModelView
// - extend Model#toJSON to convert nested objects as well (http://rcos.rpi.edu/projects/concert/commit/got-requests-displaying-properly-again/)