2013-09-13 00:03:18 +04:00
|
|
|
/*globals window, $, _, Backbone, Validator */
|
2013-07-11 23:02:18 +04:00
|
|
|
(function () {
|
2013-09-24 14:46:30 +04:00
|
|
|
'use strict';
|
2013-07-11 23:02:18 +04:00
|
|
|
|
2013-11-27 06:00:55 +04:00
|
|
|
function ghostPaths() {
|
|
|
|
var path = window.location.pathname,
|
2013-12-28 20:01:08 +04:00
|
|
|
subdir = path.substr(0, path.search('/ghost/'));
|
2013-11-27 06:00:55 +04:00
|
|
|
|
|
|
|
return {
|
2013-12-28 20:01:08 +04:00
|
|
|
subdir: subdir,
|
|
|
|
apiRoot: subdir + '/ghost/api/v0.1'
|
2013-11-27 06:00:55 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-07-11 23:02:18 +04:00
|
|
|
var Ghost = {
|
|
|
|
Layout : {},
|
|
|
|
Views : {},
|
|
|
|
Collections : {},
|
|
|
|
Models : {},
|
2013-09-13 00:03:18 +04:00
|
|
|
Validate : new Validator(),
|
2013-07-11 23:02:18 +04:00
|
|
|
|
2013-11-27 06:00:55 +04:00
|
|
|
paths: ghostPaths(),
|
2013-07-11 23:02:18 +04:00
|
|
|
|
|
|
|
// This is a helper object to denote legacy things in the
|
|
|
|
// middle of being transitioned.
|
|
|
|
temporary: {},
|
|
|
|
|
|
|
|
currentView: null,
|
|
|
|
router: null
|
|
|
|
};
|
|
|
|
|
2013-08-21 14:50:49 +04:00
|
|
|
_.extend(Ghost, Backbone.Events);
|
|
|
|
|
2013-10-17 17:28:28 +04:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2013-12-10 05:57:23 +04:00
|
|
|
Backbone.oldModelProtoUrl = Backbone.Model.prototype.url;
|
|
|
|
//overwrite original url method to add slash to end of the url if needed.
|
|
|
|
Backbone.Model.prototype.url = function () {
|
|
|
|
var url = Backbone.oldModelProtoUrl.apply(this, arguments);
|
|
|
|
return url + (url.charAt(url.length - 1) === '/' ? '' : '/');
|
|
|
|
};
|
|
|
|
|
2013-07-11 23:02:18 +04:00
|
|
|
Ghost.init = function () {
|
2013-10-27 19:16:34 +04:00
|
|
|
// remove the temporary message which appears
|
|
|
|
$('.js-msg').remove();
|
|
|
|
|
2013-07-11 23:02:18 +04:00
|
|
|
Ghost.router = new Ghost.Router();
|
2013-08-06 11:55:47 +04:00
|
|
|
|
|
|
|
// 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: []});
|
|
|
|
|
2013-07-11 23:02:18 +04:00
|
|
|
Backbone.history.start({
|
|
|
|
pushState: true,
|
|
|
|
hashChange: false,
|
2013-12-28 20:01:08 +04:00
|
|
|
root: Ghost.paths.subdir + '/ghost'
|
2013-07-11 23:02:18 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-09-13 00:03:18 +04:00
|
|
|
Ghost.Validate.error = function (object) {
|
|
|
|
this._errors.push(object);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
Ghost.Validate.handleErrors = function () {
|
2013-09-18 05:45:36 +04:00
|
|
|
Ghost.notifications.clearEverything();
|
2013-09-13 00:03:18 +04:00
|
|
|
_.each(Ghost.Validate._errors, function (errorObj) {
|
2013-09-15 21:19:11 +04:00
|
|
|
|
2013-09-13 00:03:18 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
2013-09-15 21:19:11 +04:00
|
|
|
message: errorObj.message || errorObj,
|
2013-09-13 00:03:18 +04:00
|
|
|
status: 'passive'
|
|
|
|
});
|
|
|
|
if (errorObj.hasOwnProperty('el')) {
|
|
|
|
errorObj.el.addClass('input-error');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-07-11 23:02:18 +04:00
|
|
|
window.Ghost = Ghost;
|
|
|
|
|
2013-11-16 22:47:55 +04:00
|
|
|
window.addEventListener("load", Ghost.init, false);
|
2013-09-13 00:03:18 +04:00
|
|
|
}());
|