generators/jelly/templates/javascripts/jelly.js in honkster-jelly-0.6.7 vs generators/jelly/templates/javascripts/jelly.js in honkster-jelly-0.7.0

- old
+ new

@@ -12,94 +12,114 @@ */ if (!window.Jelly) Jelly = new Object(); Jelly.init = function() { this.components = []; - Jelly.Pages.init(); + this.observers = []; + this.Components.initCalled = false; + this.Pages.init(); + var self = this; $(document).ready(function() { - Jelly.Components.init(); + self.Components.init(); }); }; Jelly.attach = function() { - for(var i = 0; i < arguments.length; i++) { + for (var i = 0; i < arguments.length; i++) { var definition = arguments[i]; var component = (typeof definition.component == "string") ? eval(definition.component) : definition.component; - this.components.push({ + var evaluatedDefinition = { component: component, arguments: definition.arguments - }); + }; + this.components.push(evaluatedDefinition); + if (Jelly.Components.initCalled) { + Jelly.Components.initComponentFromDefinition(evaluatedDefinition); + } } }; Jelly.notifyObservers = function(params, observers) { - var context; if (!observers) { - // Deprecate 'on' in favor of making each page action a Component. - context = params.on ? eval(params.on) : page; - observers = [context]; + observers = []; + for (var i = 0; i < Jelly.observers.length; i++) { + observers.push(Jelly.observers[i]); + } - for(var i=0; i < Jelly.components.length; i++) { - var component = Jelly.components[i].component; - if (component != context) { - observers.push(component); + // Deprecate 'on' in favor of making each page action a Component. + if (params.on) { + var additionalObserver = eval(params.on); + var additionalObserverAlreadyAnObserver = false; + for (var i = 0; i < observers.length; i++) { + if (observers[i] == additionalObserver) { + additionalObserverAlreadyAnObserver = true; + break; + } } + if (!additionalObserverAlreadyAnObserver) { + observers.push(additionalObserver); + } } } - for(var i = 0; i < observers.length; i++) { + for (var i = 0; i < observers.length; i++) { var observer = observers[i]; if (observer[params.method]) { observer[params.method].apply(observer, params.arguments); } } }; Jelly.Components = { init: function() { - for(var i = 0; i < Jelly.components.length; i++) { - var definition = Jelly.components[i]; - if (definition.component.init) { - definition.component.init.apply(definition.component, definition.arguments); - } + for (var i = 0; i < Jelly.components.length; i++) { + this.initComponentFromDefinition(Jelly.components[i]); } + this.initCalled = true; + }, + initComponentFromDefinition: function(definition) { + var observer; + if (definition.component.init) { + observer = definition.component.init.apply(definition.component, definition.arguments); + } + Jelly.observers.push(observer ? observer : definition.component); } }; Jelly.Pages = { init: function() { this.all = {}; Jelly.all = this.all; // Deprecated }, add: function(name) { - var page = new Jelly.Page(name); + var page = new Jelly.Page.Constructor(name); for (var i = 1; i < arguments.length; i++) { $.extend(page, arguments[i]); } return page; } }; Jelly.add = Jelly.Pages.add; // Deprecated -Jelly.Page = function(name) { - this.documentHref = Jelly.Location.documentHref; +Jelly.Page = { + init: function(controllerName, actionName) { + var page = Jelly.Pages.all[controllerName] || new Jelly.Page.Constructor(controllerName); + window.page = page; + if (page.all) page.all(); + if (page[actionName]) page[actionName].call(page); + page.loaded = true; + return page; + }, + Constructor: function(name) { + this.loaded = false; + this.documentHref = Jelly.Location.documentHref; - this.name = name; - Jelly.Pages.all[name] = this; -}; -Jelly.Page.prototype.loaded = false; -Jelly.Page.prototype.all = function() { -}; - -Jelly.Page.init = function(controllerName, actionName) { - var page = Jelly.Pages.all[controllerName] || new Jelly.Page(controllerName); - window.page = page; - if (page.all) page.all(); - if (page[actionName]) page[actionName].call(page); - page.loaded = true; + this.name = name; + Jelly.Pages.all[name] = this; + } }; Jelly.Location = { init: function() { },