From 2a3aead51456fdc6462a032e387dad1b23b783ec Mon Sep 17 00:00:00 2001 From: Stephane Bellity Date: Wed, 23 Jan 2013 22:37:05 +0100 Subject: [PATCH] aura_spec & aura.extensions_spec ok --- lib/aura.app.js | 13 +++++--- lib/aura.extensions.js | 23 +++++++++----- lib/ext/widgets.js | 9 +++--- notes/base.md | 1 - notes/widgets.md | 1 + spec/lib/aura.extensions_spec.js | 9 +++--- spec/lib/aura_spec.js | 52 +++++++++++++++----------------- 7 files changed, 58 insertions(+), 50 deletions(-) create mode 100644 notes/widgets.md diff --git a/lib/aura.app.js b/lib/aura.app.js index 94b6958..db4cd98 100644 --- a/lib/aura.app.js +++ b/lib/aura.app.js @@ -87,12 +87,15 @@ define([ // Then we call all the `afterAppStart` provided by the extensions base.util.each(exts, function(i, ext) { if (ext && typeof(ext.afterAppStart) === 'function') { - ext.afterAppStart(app); + try { + ext.afterAppStart(app); + } catch(e) { + console.error("Error on ", (ext.name || ext) , ".afterAppStart callback: (", e.message, ")", e); + } } - }); - - freeze(app.sandbox); - freeze(app.core); + }); + // freeze(app.sandbox); + // freeze(app.core); }); // If there was an error in the boot sequence we diff --git a/lib/aura.extensions.js b/lib/aura.extensions.js index a5b945e..e065cdc 100644 --- a/lib/aura.extensions.js +++ b/lib/aura.extensions.js @@ -48,7 +48,7 @@ define(['./base'], function(base) { this.initStarted = true; - var extensions = this._extensions.slice(0), + var extensions = _.compact(this._extensions.slice(0)), initialized = [], initStatus = this.initStatus; @@ -58,14 +58,19 @@ define(['./base'], function(base) { (function _init(extDef) { if (extDef) { var ext = initExtension(extDef); - ext.done(function() { _init(extensions.shift()); }); - ext.fail(initStatus.reject); initialized.push(ext); + ext.done(function() { _init(extensions.shift()); }); + ext.fail(function(err) { + console.error("Failed loading extension ", ext, (err && err.message), err); + initStatus.reject(); + }); + } else if (extensions.length === 0) { + when.apply(undefined, initialized).done(function() { + initStatus.resolve(Array.prototype.slice.call(arguments)); + }); } })(extensions.shift()); - when.apply(undefined, initialized).done(initStatus.resolve); - return initStatus.promise(); }; @@ -170,17 +175,19 @@ define(['./base'], function(base) { if (ext && ext.require && ext.require.paths) { var deps = Object.keys(ext.require.paths) || []; require.config(ext.require); - require(deps, function() { dfd.resolve(ext); }, reject); + require(deps, function() { + dfd.resolve(ext); + }, reject); } else { dfd.resolve(ext); - } + } } catch(err) { - console.error("Error resolving ext: ", err); reject(err); } }; var reject = function(err) { + console.error("Error loading ext:", ext, err); dfd.reject(err); }; diff --git a/lib/ext/widgets.js b/lib/ext/widgets.js index 4f77e2a..f9d15c5 100644 --- a/lib/ext/widgets.js +++ b/lib/ext/widgets.js @@ -245,9 +245,8 @@ define('aura/ext/widgets', function() { name: 'widgets', - config: { - require: { paths: { text: 'requirejs-text/text' } } - }, + require: { paths: { text: 'components/requirejs-text/text' } }, + init: function(app) { @@ -275,8 +274,8 @@ define('aura/ext/widgets', function() { afterAppStart: function(app) { // Auto start widgets when the app is loaded. - if (app.options.widgets) { - app.sandbox.start(app.options.widgets); + if (app.startOptions.widgets) { + app.sandbox.start(app.startOptions.widgets); } } diff --git a/notes/base.md b/notes/base.md index 521f5fc..d448f41 100644 --- a/notes/base.md +++ b/notes/base.md @@ -18,7 +18,6 @@ Current status: - eventemitter2, for the moment. we could also have a look at postal.js which is extensible and may suit more our needs. - the selector engine is required by the widgets extension, which is always included for the moment. but we don't really have to. - ### since we're here, is their a minimum browser requirement for aura? a recommended browser/cpu requirement? it should work everywhere. aura itself is really tiny ! diff --git a/notes/widgets.md b/notes/widgets.md new file mode 100644 index 0000000..13568de --- /dev/null +++ b/notes/widgets.md @@ -0,0 +1 @@ +# Widgets extension \ No newline at end of file diff --git a/spec/lib/aura.extensions_spec.js b/spec/lib/aura.extensions_spec.js index 8d38ee5..52af5a7 100644 --- a/spec/lib/aura.extensions_spec.js +++ b/spec/lib/aura.extensions_spec.js @@ -79,13 +79,14 @@ define(['aura/aura.extensions'], function(ExtManager) { it("Should be possible to add an extension via its module ref name", function(done) { var mgr = new ExtManager(), - ext = { init: sinon.spy(), foo: "bar" }; + ext = { init: sinon.spy(), foo: "bar" }, + ctx = { foo: "bar" }; define("myExt", ext); - mgr.add({ ref: "myExt", context: "yep" }); + mgr.add({ ref: "myExt", context: ctx }); mgr.init().done(function(extResolved) { - ext.init.should.have.been.calledWith("yep"); - extResolved.foo.should.equal("bar"); + extResolved[0].foo.should.equal("bar"); + ext.init.should.have.been.calledWith(ctx); done(); }); }); diff --git a/spec/lib/aura_spec.js b/spec/lib/aura_spec.js index a608ff5..6f5cbbe 100644 --- a/spec/lib/aura_spec.js +++ b/spec/lib/aura_spec.js @@ -6,52 +6,52 @@ define(['aura/aura'], function(aura) { describe("Aura Apps", function() { describe("App Public API", function() { - var env; var ext = { - init: sinon.spy(function(appEnv) { - env = appEnv; - env.sandbox.foo = "bar"; + init: sinon.spy(function(app) { + app.sandbox.foo = "bar"; }), afterAppStart: sinon.spy() }; - var app = aura(); + var App = aura(); - app.use(ext); + App.use(ext); - var startOptions = { foo: "bar" }; - var initStatus = app.start(startOptions); + var startOptions = { foo: "bar" }; + var initStatus = App.start(startOptions); // Make sure the app is started before... before(function(done) { - initStatus.done(function() { done(); }); + initStatus.done(function() { + done(); + }); initStatus.fail(done); }); it("Should have loaded its core dependencies", function() { - env.core.data.deferred.should.be.a('function'); + App.core.data.deferred.should.be.a('function'); }); it("Should have a public API", function() { - app.use.should.be.a('function'); - app.start.should.be.a('function'); - app.stop.should.be.a('function'); + App.use.should.be.a('function'); + App.start.should.be.a('function'); + App.stop.should.be.a('function'); }); it("Should call init method on extension", function() { - ext.init.should.have.been.calledWith(env); + ext.init.should.have.been.calledWith(App); }); it("Should call afterAppStart method on extension", function() { - ext.afterAppStart.should.have.been.calledWith(env); + ext.afterAppStart.should.have.been.called; //With(App); }); it("Should have extended the sandbox", function() { - env.sandbox.foo.should.equal('bar'); + App.sandbox.foo.should.equal('bar'); }); it("Should complain if I try to use a new extension and the app is already started", function() { - app.use.should.Throw(Error); + App.use.should.Throw(Error); }); }); @@ -66,29 +66,27 @@ define(['aura/aura'], function(aura) { }); it("Should be able to use extensions defined as functions", function(done) { - var env; var insideExt = sinon.spy(); var ext = sinon.spy(function(appEnv) { - env = appEnv; insideExt('foo'); }); - aura().use(ext).start().done(function() { - ext.should.have.been.calledWith(env); + var App = aura().use(ext); + App.start().done(function() { + ext.should.have.been.calledWith(App); insideExt.should.have.been.calledWith('foo'); done(); }); }); it("Should pass the start options to the extensions...", function(done) { - var env; var startOptions = { foo: 'bar' }; var insideExt = sinon.spy(); - var ext = sinon.spy(function(appEnv) { - env = appEnv; - insideExt(env.options); + var ext = sinon.spy(function(app) { + insideExt(app.startOptions); }); - aura().use(ext).start(startOptions).done(function() { - ext.should.have.been.calledWith(env); + var App = aura().use(ext); + App.start(startOptions).done(function() { + ext.should.have.been.calledWith(App); insideExt.should.have.been.calledWith(startOptions); done(); });