Ghost.init()

- Modified jsonDataProvider to return promises for findAll and save
- Move the dataProvider initialization into the Ghost.init() function.
- Created basic unit test
This commit is contained in:
Jacob Gable 2013-05-30 09:01:29 -05:00 committed by Gabor Javorszky
parent ce5e757672
commit a8bf3c962f
5 changed files with 44 additions and 13 deletions

8
app.js
View File

@ -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..

View File

@ -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'),

View File

@ -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()]);
};
/**

View File

@ -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) {

View File

@ -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);
});
});
}());