generators/jelly/templates/javascripts/jelly.js in honkster-jelly-0.7.7 vs generators/jelly/templates/javascripts/jelly.js in honkster-jelly-0.8.1

- old
+ new

@@ -1,10 +1,10 @@ /** * Jelly. a sweet unobtrusive javascript framework * for jQuery and Rails * - * version 0.7.7 + * version 0.8.1 * * Copyright (c) 2009 Pivotal Labs * Licensed under the MIT license. * * * Date: 2009-07-20 9:50:50 (Mon, 20 Jul 2009) @@ -19,79 +19,94 @@ return self.apply(object, arguments); } } } Jelly.init = function() { - this.components = []; this.observers = []; + this.observers.pending = []; + this.attach = this.Observers.attach; this.notifyObservers = this.Observers.notify; this.Components.initCalled = false; this.Pages.init(); var self = this; $(document).ready(function() { self.Components.init(); }); }; -Jelly.attach = function() { - for (var i = 0; i < arguments.length; i++) { - var definition = arguments[i]; - var component = (typeof definition.component == "string") ? - eval(definition.component) : - definition.component; - var evaluatedDefinition = { - component: component, - arguments: definition.arguments - }; - this.components.push(evaluatedDefinition); - if (Jelly.Components.initCalled) { - Jelly.Components.initComponentFromDefinition(evaluatedDefinition); +Jelly.Components = { + init: function() { + for (var i = 0; i < Jelly.observers.pending.length; i++) { + Jelly.Observers.initPending.call(Jelly.observers, Jelly.observers.pending[i]); } + this.initCalled = true; } }; -Jelly.Components = { - init: function() { - for (var i = 0; i < Jelly.components.length; i++) { - this.initComponentFromDefinition(Jelly.components[i]); +Jelly.Observers = { + attach: function() { + if (this == Jelly) { + return Jelly.Observers.attach.apply(this.observers, arguments); } - this.initCalled = true; + this.pending = this.pending || []; + for (var i = 0; i < arguments.length; i++) { + var definition = arguments[i]; + var component = (typeof definition.component == "string") ? + eval(definition.component) : + definition.component; + var evaluatedDefinition = { + component: component, + arguments: definition.arguments + }; + this.pending.push(evaluatedDefinition); + if (Jelly.Components.initCalled) { + Jelly.Observers.initPending.call(this, evaluatedDefinition); + } + } }, - initComponentFromDefinition: function(definition) { + + initPending: function(definition) { var observer; if (definition.component.init) { observer = definition.component.init.apply(definition.component, definition.arguments); } - Jelly.observers.push(observer ? observer : definition.component); - } -}; + this.push(observer ? observer : definition.component); + }, -Jelly.Observers = { - notify: function(params) { - if (this == Jelly) return Jelly.Observers.notify.call(this.observers, params); + notify: function(callbacks) { + if (this == Jelly) { + return Jelly.Observers.notify.apply(this.observers, arguments); + } + if (!$.isArray(callbacks)) { + callbacks = [callbacks]; + } + var observers = this.slice(0); + for (var i = 0; i < callbacks.length; i++) { + var callback = callbacks[i]; - // Deprecate 'on' in favor of making each page action a Component. - if (params.on) { - var additionalObserver = eval(params.on); - if (observers.indexOf(additionalObserver) == -1) { - observers.push(additionalObserver); + // Deprecate 'on' in favor of making each page action a Component. + if (callback.on) { + var additionalObserver = eval(callback.on); + if (observers.indexOf(additionalObserver) == -1) { + observers.push(additionalObserver); + } } - } - for (var i = 0; i < observers.length; i++) { - var observer = observers[i]; - if (observer[params.method]) { - if (observer.detach && observer.detach()) { - Jelly.Observers.garbageCollectObserver.call(this, observer); - } else { - observer[params.method].apply(observer, params.arguments); + for (var j = 0; j < observers.length; j++) { + var observer = observers[j]; + if (observer[callback.method]) { + if (observer.detach && observer.detach()) { + Jelly.Observers.garbageCollectObserver.call(this, observer); + } else { + observer[callback.method].apply(observer, callback.arguments); + } } } - } - if (params.attach) { - Jelly.attach.apply(Jelly, params.attach); + if (callback.attach) { + Jelly.Observers.attach.apply(this, callback.attach); + } } }, garbageCollectObserver: function(observer) { var index = this.indexOf(observer);