Skip to content

Instantly share code, notes, and snippets.

// Adds an `append` option to the Backbone Router
Backbone.Router.prototype.navigate = (function(origNavigate) {
return function(fragment, options) {
if (options && options.append) {
fragment = Backbone.history.fragment + fragment;
delete options.append;
}
return origNavigate.call(this, fragment, options);
};
})(Backbone.Router.prototype.navigate);
@sarink
sarink / backbone.subiews.js
Last active August 29, 2015 14:01
backbone.subviews
// I wrote this when Backbone was like v0.2 - now there are many many tools like Marionette,
// and even things native to Backbone (like `stopListening`) that invalidate a lot of this
var SubView = Backbone.View.extend({
// tagName: must be implemented
// className: must be implemented
// template: must be implemented
initialize: function() {
this.model.on("change", this.render, this);
this.model.on("close", this.close, this);
(function(root, factory) {
if (typeof define === "function" && define.amd) {
define(["underscore", "backbone", "backboneBabySitter"], function(_, Backbone, ChildViewContainer) {
return factory(_, Backbone, ChildViewContainer);
});
}
else if (typeof exports !== "undefined") {
var _ = require("underscore");
var Backbone = require("backbone");
var ChildViewContainer = require("backboneBabySitter");
@sarink
sarink / private.js
Last active December 25, 2022 21:56
// Sample demo of how to gain access to "private" variables in JavaScript,
// even if they'd normally be inaccessible due to the closure.
var Table = function () {
var _array = ["super", "secret", "message"];
return {
get: function (i) { return _array[i]; },
store: function (i,v) { _array[i] = v; },
append: function (v) { _array.push(v); }
};
};