Ghost/core/client/init.js
2013-11-27 09:57:38 +00:00

86 lines
2.3 KiB
JavaScript

/*globals window, $, _, Backbone, Validator */
(function () {
'use strict';
function ghostPaths() {
var path = window.location.pathname,
root = path.substr(0, path.search('/ghost/'));
return {
ghostRoot: root,
apiRoot: root + '/ghost/api/v0.1'
};
}
var Ghost = {
Layout : {},
Views : {},
Collections : {},
Models : {},
Validate : new Validator(),
paths: ghostPaths(),
// This is a helper object to denote legacy things in the
// middle of being transitioned.
temporary: {},
currentView: null,
router: null
};
_.extend(Ghost, Backbone.Events);
Backbone.oldsync = Backbone.sync;
// override original sync method to make header request contain csrf token
Backbone.sync = function (method, model, options, error) {
options.beforeSend = function (xhr) {
xhr.setRequestHeader('X-CSRF-Token', $("meta[name='csrf-param']").attr('content'));
};
/* call the old sync method */
return Backbone.oldsync(method, model, options, error);
};
Ghost.init = function () {
// remove the temporary message which appears
$('.js-msg').remove();
Ghost.router = new Ghost.Router();
// This is needed so Backbone recognizes elements already rendered server side
// as valid views, and events are bound
Ghost.notifications = new Ghost.Views.NotificationCollection({model: []});
Backbone.history.start({
pushState: true,
hashChange: false,
root: Ghost.paths.ghostRoot + '/ghost'
});
};
Ghost.Validate.error = function (object) {
this._errors.push(object);
return this;
};
Ghost.Validate.handleErrors = function () {
Ghost.notifications.clearEverything();
_.each(Ghost.Validate._errors, function (errorObj) {
Ghost.notifications.addItem({
type: 'error',
message: errorObj.message || errorObj,
status: 'passive'
});
if (errorObj.hasOwnProperty('el')) {
errorObj.el.addClass('input-error');
}
});
};
window.Ghost = Ghost;
window.addEventListener("load", Ghost.init, false);
}());