Skip to content

Commit

Permalink
backbone-postmaster rough prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasvo-taulia committed Jan 16, 2015
0 parents commit c60ebff
Show file tree
Hide file tree
Showing 11 changed files with 13,709 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2015, Lucas Vogelsang
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
105 changes: 105 additions & 0 deletions backbone-postmaster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (c) 2015, Lucas Vogelsang
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// * Neither the name of the <organization> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// # Backbone Postmaster
//
// Backbone Postmater is an extension to the built in backbone router. Postmaster let's
// you split up your routes into smaller modules and register these. Take a look at `examples/todo.html`
// for a usage example.

(function(root, factory) {
// Set up Postmaster appropriately for the environment. Start with AMD.
if (typeof define === 'function' && define.amd) {
define(['underscore', 'backbone', 'exports'], function(_, Backbone, exports) {
return factory(root, _, Backbone);
});

// Next for Node.js or CommonJS.
} else if (typeof exports !== 'undefined') {
var _ = require('underscore');
var Backbone = require('backbone');
exports = factory(root, _, Backbone);

// Finally, as a browser global.
} else {
root.BackbonePostmaster = factory(root, root._, root.Backbone );
}

}(this, function(root, _, Backbone) {

Postmaster = function (options) {
options || (options = {});
this.initialize.apply(this, arguments);
};

_.extend(Postmaster.prototype, {
initialize: function () {},

// Backbone Router Object used to interface with Backbone.js
_backboneRouter: new Backbone.Router(),

// ## register method
// The register function accepts a combination of different arguments. It can register
// single routes or objects that contain multiple routes.
//
// The method takes the following arguments:
// * `path`: this is the entry point for routes to be registered. If
// * `name` (*optional*): Backbone.Router let's you name routes. If this argument is omitted, the `path` will This parameter is only required
// if route is "".
register: function (path, name, dest, noAppendSlash) {

if (arguments.length == 2) {
if (path == "") throw "Name argument is required if route is \"\"";
dest = name;
name = path;
}


if (_.isFunction(dest)) {
this._route(path, name, dest);
return;
}

if (_.isObject(dest) && !_.isFunction(dest) && _.isObject(dest.routes)) {
this._registerObject(path, name, dest.routes, dest, noAppendSlash);
return;
}

throw "Calling ModularRouter with invalid arguments. Make sure the third argument is a function or an object.";
},

_registerObject: function (route, name, object, context, noAppendSlash) {
_.each(object, function (method, subroute) {
subroute = this._joinPath(route, subroute, noAppendSlash);
var routeName = name+"_"+method;
this._route(subroute, routeName, function() {
method = context[method];
method.apply(context, arguments);
});
}, this);
},

_joinPath: function (a, b, noAppendSlash) {
if (a == "" || noAppendSlash === true) return a+b;
if (a.slice(-1) == "/") return a+b;
return a+"/"+b;
},

_route: function (route, name, dest) {
this._backboneRouter.route(route, name, dest);
}
});

return Postmaster;

}));
69 changes: 69 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Karma configuration
// Generated on Mon Dec 15 2014 09:38:31 GMT-0700 (MST)

module.exports = function(config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',

plugins: ['karma-jasmine', 'karma-requirejs', 'karma-phantomjs-launcher', 'karma-chrome-launcher'],

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine', 'requirejs'],


// list of files / patterns to load in the browser
files: [
'test/test-main.js',
{pattern: 'backbone-postmaster.js', included: false},
{pattern: 'test/**/*Spec.js', included: false},
{pattern: 'test/vendor/**/*.js', included: false}
],


// list of files to exclude
exclude: [
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
Loading

0 comments on commit c60ebff

Please sign in to comment.