diff --git a/app.js b/app.js index 8aa6197989..42e23213d0 100755 --- a/app.js +++ b/app.js @@ -29,7 +29,8 @@ * @type {Ghost} */ ghost = new Ghost(), - ghostGlobals = ghost.globals(); + // This is assigned after the call to ghost.init() below + ghostGlobals; @@ -99,7 +100,10 @@ // Expose the promise we will resolve after our pre-loading ghost.loaded = loading.promise; - when.all([ghost.dataProvider().init(), filters.loadCoreFilters(ghost), helpers.loadCoreHelpers(ghost)]).then(function () { + when.all([ghost.init(), filters.loadCoreFilters(ghost), helpers.loadCoreHelpers(ghost)]).then(function () { + + // Assign the globals we have loaded + ghostGlobals = ghost.globals(); /** * API routes.. diff --git a/core/admin/controllers/index.js b/core/admin/controllers/index.js index 4146be4320..5bd60cc7e5 100755 --- a/core/admin/controllers/index.js +++ b/core/admin/controllers/index.js @@ -116,8 +116,6 @@ if (req.params.id !== undefined) { api.posts.read({id: parseInt(req.params.id, 10)}) .then(function (post) { - // res.flash('success', 'test'); - console.log('this thing runs'); res.render('editor', { bodyClass: 'editor', adminNav: setSelected(adminNavbar, 'content'), diff --git a/core/ghost.js b/core/ghost.js index 0a13951382..3443d18c54 100644 --- a/core/ghost.js +++ b/core/ghost.js @@ -7,6 +7,7 @@ // ## Setup Prerequisites var config = require('./../config'), + when = require('when'), express = require('express'), path = require('path'), hbs = require('express-hbs'), @@ -47,7 +48,6 @@ plugin, polyglot; - if (!instance) { // this.init(); instance = this; @@ -64,7 +64,7 @@ _.extend(instance, { app: function () { return app; }, config: function () { return config; }, - globals: function () { return instance.init(); }, // there's no management here to be sure this has loaded + globals: function () { return instance.globalsData; }, // there's no management here to be sure this has loaded dataProvider: function () { return bookshelfDataProvider; }, statuses: function () { return statuses; }, polyglot: function () { return polyglot; }, @@ -85,12 +85,14 @@ }; Ghost.prototype.init = function() { - var globals; - jsonDataProvider.save(config.blogData); - jsonDataProvider.findAll(function (error, data) { - globals = data; + var initGlobals = jsonDataProvider.save(config.blogData).then(function () { + return jsonDataProvider.findAll().then(function (data) { + // We must have an instance to be able to call ghost.init(), right? + instance.globalsData = data; + }); }); - return globals; + + return when.all([initGlobals, instance.dataProvider().init()]); }; /** diff --git a/core/shared/models/dataProvider.json.js b/core/shared/models/dataProvider.json.js index ef9ba6b704..5b98c8c233 100644 --- a/core/shared/models/dataProvider.json.js +++ b/core/shared/models/dataProvider.json.js @@ -16,8 +16,8 @@ instance = this; _.extend(instance, { data: [], - findAll: function(callback) { - callback(null, instance.data); + findAll: function() { + return when(instance.data); }, save: function (globals) { _.each(globals, function (global, key) { diff --git a/core/test/ghost/ghost_spec.js b/core/test/ghost/ghost_spec.js index e037df61af..11212ac9da 100644 --- a/core/test/ghost/ghost_spec.js +++ b/core/test/ghost/ghost_spec.js @@ -4,6 +4,8 @@ "use strict"; var should = require('should'), + when = require('when'), + sinon = require('sinon'), Ghost = require('../../ghost'); describe("Ghost API", function () { @@ -15,6 +17,31 @@ should.strictEqual(ghost1, ghost2); }); + it("uses init() to initialize", function (done) { + var ghost = new Ghost(), + fakeDataProvider = { + init: function() { + return when.resolve(); + } + }, + dataProviderInitSpy = sinon.spy(fakeDataProvider, "init"); + + // Stub out the dataProvider + sinon.stub(ghost, "dataProvider", function () { + return fakeDataProvider; + }); + + should.not.exist(ghost.globals()); + + ghost.init().then(function () { + should.exist(ghost.globals()); + + dataProviderInitSpy.called.should.equal(true); + + done(); + }, done); + }); + }); }()); \ No newline at end of file