From c41971c460080d5ba9f9d648bb7b9a7865be2b3d Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 30 Dec 2012 14:09:57 -0800 Subject: [PATCH] Upgrade Backbone.Marionette to 1.0.0-rc2. --- .../1.0.0-rc2-amdjs/backbone.marionette.js | 1787 +++++++++++++++++ .../backbone.marionette.min.js | 6 + .../1.0.0-rc2/backbone.marionette.js | 1764 ++++++++++++++++ .../1.0.0-rc2/backbone.marionette.min.js | 6 + ajax/libs/backbone.marionette/package.json | 6 +- 5 files changed, 3566 insertions(+), 3 deletions(-) create mode 100644 ajax/libs/backbone.marionette/1.0.0-rc2-amdjs/backbone.marionette.js create mode 100644 ajax/libs/backbone.marionette/1.0.0-rc2-amdjs/backbone.marionette.min.js create mode 100644 ajax/libs/backbone.marionette/1.0.0-rc2/backbone.marionette.js create mode 100644 ajax/libs/backbone.marionette/1.0.0-rc2/backbone.marionette.min.js diff --git a/ajax/libs/backbone.marionette/1.0.0-rc2-amdjs/backbone.marionette.js b/ajax/libs/backbone.marionette/1.0.0-rc2-amdjs/backbone.marionette.js new file mode 100644 index 00000000000000..0cb32621693886 --- /dev/null +++ b/ajax/libs/backbone.marionette/1.0.0-rc2-amdjs/backbone.marionette.js @@ -0,0 +1,1787 @@ + // Backbone.Marionette, v1.0.0-rc2 + // Copyright (c)2012 Derick Bailey, Muted Solutions, LLC. + // Distributed under MIT license + // http://github.com/marionettejs/backbone.marionette + +(function (root, factory) { + if (typeof exports === 'object') { + + var jquery = require('jquery'); + var underscore = require('underscore'); + var backbone = require('backbone'); + var eventBinder = require('backbone.eventbinder'); + var wreqr = require('backbone.wreqr'); + var babysitter = require('backbone.babysitter'); + + module.exports = factory(jquery, underscore, backbone, eventBinder, wreqr, babysitter); + + } else if (typeof define === 'function' && define.amd) { + + define(['jquery', 'underscore', 'backbone', 'backbone.wreqr', 'backbone.eventbinder', 'backbone.babysitter'], factory); + + } +}(this, function ($, _, Backbone) { + + Backbone.Marionette = Marionette = (function(Backbone, _, $){ + var Marionette = {}; + + // Helpers + // ------- + + // For slicing `arguments` in functions + var slice = Array.prototype.slice; + + // Marionette.extend + // ----------------- + + // Borrow the Backbone `extend` method so we can use it as needed + Marionette.extend = Backbone.Model.extend; + + // Marionette.getOption + // -------------------- + + // Retrieve an object, function or other value from a target + // object or it's `options`, with `options` taking precedence. + Marionette.getOption = function(target, optionName){ + if (!target || !optionName){ return; } + var value; + + if (target.options && target.options[optionName]){ + value = target.options[optionName]; + } else { + value = target[optionName]; + } + + return value; + }; + + // Mairionette.createObject + // ------------------------ + + // A wrapper / shim for `Object.create`. Uses native `Object.create` + // if available, otherwise shims it in place for Marionette to use. + Marionette.createObject = (function(){ + var createObject; + + // Define this once, and just replace the .prototype on it as needed, + // to improve performance in older / less optimized JS engines + function F() {} + + + // Check for existing native / shimmed Object.create + if (typeof Object.create === "function"){ + + // found native/shim, so use it + createObject = Object.create; + + } else { + + // An implementation of the Boodman/Crockford delegation + // w/ Cornford optimization, as suggested by @unscriptable + // https://gist.github.com/3959151 + + // native/shim not found, so shim it ourself + createObject = function (o) { + + // set the prototype of the function + // so we will get `o` as the prototype + // of the new object instance + F.prototype = o; + + // create a new object that inherits from + // the `o` parameter + var child = new F(); + + // clean up just in case o is really large + F.prototype = null; + + // send it back + return child; + }; + + } + + return createObject; + })(); + + // Trigger an event and a corresponding method name. Examples: + // + // `this.triggerMethod("foo")` will trigger the "foo" event and + // call the "onFoo" method. + // + // `this.triggerMethod("foo:bar") will trigger the "foo:bar" event and + // call the "onFooBar" method. + Marionette.triggerMethod = function(){ + var args = Array.prototype.slice.apply(arguments); + var eventName = args[0]; + var segments = eventName.split(":"); + var segment, capLetter, methodName = "on"; + + for (var i = 0; i < segments.length; i++){ + segment = segments[i]; + capLetter = segment.charAt(0).toUpperCase(); + methodName += capLetter + segment.slice(1); + } + + this.trigger.apply(this, args); + + if (_.isFunction(this[methodName])){ + args.shift(); + return this[methodName].apply(this, args); + } + }; + + // DOMRefresh + // ---------- + // + // Monitor a view's state, and after it has been rendered and shown + // in the DOM, trigger a "dom:refresh" event every time it is + // re-rendered. + + Marionette.MonitorDOMRefresh = (function(){ + // track when the view has been rendered + function handleShow(view){ + view._isShown = true; + triggerDOMRefresh(view); + } + + // track when the view has been shown in the DOM, + // using a Marionette.Region (or by other means of triggering "show") + function handleRender(view){ + view._isRendered = true; + triggerDOMRefresh(view); + } + + // Trigger the "dom:refresh" event and corresponding "onDomRefresh" method + function triggerDOMRefresh(view){ + if (view._isShown && view._isRendered){ + if (_.isFunction(view.triggerMethod)){ + view.triggerMethod("dom:refresh"); + } + } + } + + // Export public API + return function(view){ + view.bindTo(view, "show", function(){ + handleShow(view); + }); + + view.bindTo(view, "render", function(){ + handleRender(view); + }); + }; + })(); + + + // EventBinder + // ----------- + // Import the event binder from it's new home + // https://github.com/marionettejs/backbone.eventbinder + Marionette.EventBinder = Backbone.EventBinder.extend(); + + // Add the EventBinder methods to the view directly, + // but keep them bound to the EventBinder instance so they work properly. + // This allows the event binder's implementation to vary independently + // of it being attached to the view... for example the internal structure + // used to store the events can change without worry about it interfering + // with Marionette's views. + Marionette.addEventBinder = function(target){ + var eventBinder = new Marionette.EventBinder(); + target.eventBinder = eventBinder; + + target.bindTo = function(source, event, callback, context){ + // check the context of the bindTo and set it to the object + // that is having the eventBinder attached to it, if no context + // has been specified in the .bindTo call + context = context || target; + eventBinder.bindTo(source, event, callback, context); + }; + + target.unbindFrom = _.bind(eventBinder.unbindFrom, eventBinder); + target.unbindAll = _.bind(eventBinder.unbindAll, eventBinder); + }; + + // Event Aggregator + // ---------------- + // A pub-sub object that can be used to decouple various parts + // of an application through event-driven architecture. + // + // Extends [Backbone.Wreqr.EventAggregator](https://github.com/marionettejs/backbone.wreqr) + // and mixes in an EventBinder from [Backbone.EventBinder](https://github.com/marionettejs/backbone.eventbinder). + Marionette.EventAggregator = Backbone.Wreqr.EventAggregator.extend({ + + constructor: function(){ + Marionette.addEventBinder(this); + + var args = Array.prototype.slice.apply(arguments); + Backbone.Wreqr.EventAggregator.prototype.constructor.apply(this, args); + } + + }); + + // Marionette.bindEntityEvents + // --------------------------- + // + // This method is used to bind a backbone "entity" (collection/model) + // to methods on a target object. + // + // The first paremter, `target`, must have a `bindTo` method from the + // EventBinder object. + // + // The second parameter is the entity (Backbone.Model or Backbone.Collection) + // to bind the events from. + // + // The third parameter is a hash of { "event:name": "eventHandler" } + // configuration. Multiple handlers can be separated by a space. A + // function can be supplied instead of a string handler name. + Marionette.bindEntityEvents = (function(){ + + // Bind the event to handlers specified as a string of + // handler names on the target object + function bindFromStrings(target, entity, evt, methods){ + var methodNames = methods.split(/\s+/); + + _.each(methodNames,function(methodName) { + + var method = target[methodName]; + if(!method) { + throw new Error("Method '"+ methodName +"' was configured as an event handler, but does not exist."); + } + + target.bindTo(entity, evt, method, target); + }); + } + + // Bind the event to a supplied callback function + function bindToFunction(target, entity, evt, method){ + target.bindTo(entity, evt, method, target); + } + + // Export the bindEntityEvents method + return function(target, entity, bindings){ + if (!entity || !bindings) { return; } + + _.each(bindings, function(methods, evt){ + + // allow for a function as the handler, + // or a list of event names as a string + if (_.isFunction(methods)){ + bindToFunction(target, entity, evt, methods); + } else { + bindFromStrings(target, entity, evt, methods); + } + + }); + }; + })(); + + + // Callbacks + // --------- + + // A simple way of managing a collection of callbacks + // and executing them at a later point in time, using jQuery's + // `Deferred` object. + Marionette.Callbacks = function(){ + this._deferred = $.Deferred(); + this._callbacks = []; + }; + + _.extend(Marionette.Callbacks.prototype, { + + // Add a callback to be executed. Callbacks added here are + // guaranteed to execute, even if they are added after the + // `run` method is called. + add: function(callback, contextOverride){ + this._callbacks.push({cb: callback, ctx: contextOverride}); + + this._deferred.done(function(context, options){ + if (contextOverride){ context = contextOverride; } + callback.call(context, options); + }); + }, + + // Run all registered callbacks with the context specified. + // Additional callbacks can be added after this has been run + // and they will still be executed. + run: function(options, context){ + this._deferred.resolve(context, options); + }, + + // Resets the list of callbacks to be run, allowing the same list + // to be run multiple times - whenever the `run` method is called. + reset: function(){ + var that = this; + var callbacks = this._callbacks; + this._deferred = $.Deferred(); + this._callbacks = []; + _.each(callbacks, function(cb){ + that.add(cb.cb, cb.ctx); + }); + } + }); + + + // Marionette Controller + // --------------------- + // + // A multi-purpose object to use as a controller for + // modules and routers, and as a mediator for workflow + // and coordination of other objects, views, and more. + Marionette.Controller = function(options){ + this.triggerMethod = Marionette.triggerMethod; + this.options = options || {}; + + Marionette.addEventBinder(this); + + if (_.isFunction(this.initialize)){ + this.initialize(this.options); + } + }; + + Marionette.Controller.extend = Marionette.extend; + + // Controller Methods + // -------------- + + // Ensure it can trigger events with Backbone.Events + _.extend(Marionette.Controller.prototype, Backbone.Events, { + close: function(){ + this.unbindAll(); + this.triggerMethod("close"); + this.unbind(); + } + }); + + // Region + // ------ + // + // Manage the visual regions of your composite application. See + // http://lostechies.com/derickbailey/2011/12/12/composite-js-apps-regions-and-region-managers/ + + Marionette.Region = function(options){ + this.options = options || {}; + + Marionette.addEventBinder(this); + + this.el = Marionette.getOption(this, "el"); + + if (!this.el){ + var err = new Error("An 'el' must be specified for a region."); + err.name = "NoElError"; + throw err; + } + + if (this.initialize){ + var args = Array.prototype.slice.apply(arguments); + this.initialize.apply(this, args); + } + }; + + + // Region Type methods + // ------------------- + + _.extend(Marionette.Region, { + + // Build an instance of a region by passing in a configuration object + // and a default region type to use if none is specified in the config. + // + // The config object should either be a string as a jQuery DOM selector, + // a Region type directly, or an object literal that specifies both + // a selector and regionType: + // + // ```js + // { + // selector: "#foo", + // regionType: MyCustomRegion + // } + // ``` + // + buildRegion: function(regionConfig, defaultRegionType){ + var regionIsString = (typeof regionConfig === "string"); + var regionSelectorIsString = (typeof regionConfig.selector === "string"); + var regionTypeIsUndefined = (typeof regionConfig.regionType === "undefined"); + var regionIsType = (typeof regionConfig === "function"); + + if (!regionIsType && !regionIsString && !regionSelectorIsString) { + throw new Error("Region must be specified as a Region type, a selector string or an object with selector property"); + } + + var selector, RegionType; + + // get the selector for the region + + if (regionIsString) { + selector = regionConfig; + } + + if (regionConfig.selector) { + selector = regionConfig.selector; + } + + // get the type for the region + + if (regionIsType){ + RegionType = regionConfig; + } + + if (!regionIsType && regionTypeIsUndefined) { + RegionType = defaultRegionType; + } + + if (regionConfig.regionType) { + RegionType = regionConfig.regionType; + } + + // build the region instance + + var regionManager = new RegionType({ + el: selector + }); + + return regionManager; + } + + }); + + // Region Instance Methods + // ----------------------- + + _.extend(Marionette.Region.prototype, Backbone.Events, { + + // Displays a backbone view instance inside of the region. + // Handles calling the `render` method for you. Reads content + // directly from the `el` attribute. Also calls an optional + // `onShow` and `close` method on your view, just after showing + // or just before closing the view, respectively. + show: function(view){ + + this.ensureEl(); + this.close(); + + view.render(); + this.open(view); + + Marionette.triggerMethod.call(view, "show"); + Marionette.triggerMethod.call(this, "show", view); + + this.currentView = view; + }, + + ensureEl: function(){ + if (!this.$el || this.$el.length === 0){ + this.$el = this.getEl(this.el); + } + }, + + // Override this method to change how the region finds the + // DOM element that it manages. Return a jQuery selector object. + getEl: function(selector){ + return $(selector); + }, + + // Override this method to change how the new view is + // appended to the `$el` that the region is managing + open: function(view){ + this.$el.empty().append(view.el); + }, + + // Close the current view, if there is one. If there is no + // current view, it does nothing and returns immediately. + close: function(){ + var view = this.currentView; + if (!view || view.isClosed){ return; } + + if (view.close) { view.close(); } + Marionette.triggerMethod.call(this, "close"); + + delete this.currentView; + }, + + // Attach an existing view to the region. This + // will not call `render` or `onShow` for the new view, + // and will not replace the current HTML for the `el` + // of the region. + attachView: function(view){ + this.currentView = view; + }, + + // Reset the region by closing any existing view and + // clearing out the cached `$el`. The next time a view + // is shown via this region, the region will re-query the + // DOM for the region's `el`. + reset: function(){ + this.close(); + delete this.$el; + } + }); + + // Copy the `extend` function used by Backbone's classes + Marionette.Region.extend = Marionette.extend; + + + // Template Cache + // -------------- + + // Manage templates stored in `