// ========================================================================== // Project: SproutCore - JavaScript Application Framework // Copyright: ©2006-2010 Sprout Systems, Inc. and contributors. // Portions ©2008-2010 Apple Inc. All rights reserved. // License: Licensed under MIT license (see license.js) // ========================================================================== /** The global bundle methods. See also: lib/boostrap.rhtml */ SC.mixin(/** @scope SC */ { /** @property @default NO @type {Boolean} If YES, log bundle loading. */ logBundleLoading: NO, /** Returns YES is bundleName is loaded; NO if bundleName is not loaded or no information is available. @param bundleName {String} @returns {Boolean} */ bundleIsLoaded: function(bundleName) { var bundleInfo = SC.BUNDLE_INFO[bundleName] ; return bundleInfo ? !!bundleInfo.loaded : NO ; }, /** @private Execute callback function. */ _scb_bundleDidLoad: function(bundleName, target, method, args) { var m = method, t = target ; if(SC.typeOf(target) === SC.T_STRING) { t = SC.objectForPropertyPath(target); } if(SC.typeOf(method) === SC.T_STRING) { m = SC.objectForPropertyPath(method, t); } if(!m) { if(SC.LAZY_INSTANTIATION[bundleName]) { var lazyInfo = SC.LAZY_INSTANTIATION[bundleName]; if (SC.logBundleLoading) console.log("SC.loadBundle(): Bundle '%@' is marked for lazy instantiation, instantiating it now…".fmt(bundleName)); for(var i=0, iLen = lazyInfo.length; i and tags to DOM for bundle's resources var styles, scripts, url, el, head, body; head = document.getElementsByTagName('head')[0] ; if (!head) head = document.documentElement ; // fix for Opera styles = bundleInfo.styles || [] ; for (idx=0, len=styles.length; idx 0) { el = document.createElement('link') ; el.setAttribute('href', url) ; el.setAttribute('rel', "stylesheet") ; el.setAttribute('type', "text/css") ; head.appendChild(el) ; } } // Push the URLs on the the queue and then start the loading. var jsBundleLoadQueue = this._jsBundleLoadQueue; if(!jsBundleLoadQueue) this._jsBundleLoadQueue = jsBundleLoadQueue = {}; jsBundleLoadQueue[bundleName] = []; var q = jsBundleLoadQueue[bundleName] ; scripts = bundleInfo.scripts || [] ; for (idx=0, len=scripts.length; idx 0) { q.push(url); } } // and remember that we're loading bundleInfo.loading = YES ; // Start the load process. this.scriptDidLoad(bundleName); } } } }, /** Load the next script in the queue now that the caller of this function is complete. @param {String} bundleName The name of the bundle. */ scriptDidLoad: function(bundleName) { var jsBundleLoadQueue = this._jsBundleLoadQueue; if(jsBundleLoadQueue) { var q = jsBundleLoadQueue[bundleName]; if(q) { var url = q.shift(); if (SC.logBundleLoading) console.log("SC.scriptDidLoad(): Loading next file in '%@' -> '%@'".fmt(bundleName, url)); var el = document.createElement('script') ; el.setAttribute('type', "text/javascript") ; el.setAttribute('src', url) ; document.body.appendChild(el) ; } } }, /** @private Called by bundle_loaded.js immediately after a framework/bundle is loaded. Any pending callbacks are called (if SC.isReady), and any dependent bundles which were waiting for this bundle to load are notified so they can continue loading. @param bundleName {String} the name of the bundle that just loaded */ bundleDidLoad: function(bundleName) { var bundleInfo = SC.BUNDLE_INFO[bundleName], log = SC.logBundleLoading, callbacks, targets ; if (!bundleInfo) { bundleInfo = SC.BUNDLE_INFO[bundleName] = { loaded: YES} ; return; } if (bundleInfo.loaded && log) { console.log("SC.bundleDidLoad() called more than once for bundle '%@'. Skipping.".fmt(bundleName)); return ; } // remember that we're loaded delete bundleInfo.loading ; bundleInfo.loaded = YES ; // call our callbacks (if SC.isReady), otherwise queue them for later if (SC.isReady) { SC._invokeCallbacksForBundle(bundleName) ; } else { SC.ready(SC, function() { SC._invokeCallbacksForBundle(bundleName) ; }); } // for each dependent bundle, try and load them again... var dependents = bundleInfo.dependents || [] ; for (var idx=0, len=dependents.length; idx