Commit 2f867851 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Init comments before launching Docs app.

Change Docs.Application to initialize comments before anything else.
This way the rest of the app can rely on comment counts to be
already loaded and avoid setting up listeners all over the place.

Additionally the CommentCounts is now accessed through Docs.Comments
and never directly as it's no more a singleton.
parent bd45dd0b
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@ Ext.Loader.setConfig({
    }
});

Ext.require('Docs.view.Viewport');
Ext.require('Ext.form.field.Trigger');
Ext.require('Ext.tab.Panel');
Ext.require('Ext.grid.column.Action');
+36 −29
Original line number Diff line number Diff line
/**
 * Main application definition for Docs app.
 * Launcher of the Docs app.
 *
 * We define our own Application class because this way we can also
 * easily define the dependencies.
 * To have greater control of all the dependencies and do some
 * additional setup before launching the actual Ext.app.Application
 * instance we're not using the basic Ext.application().
 */
Ext.define('Docs.Application', {
    extend: 'Ext.app.Application',
    name: 'Docs',

    requires: [
        'Ext.app.Application',
        'Docs.History',
        'Docs.Auth',
        'Docs.Settings'
        'Docs.Comments',
        'Docs.Settings',
        'Docs.view.Viewport'
    ],

    uses: [
        'Ext.util.History',
        'Ext.data.JsonP'
    ],
    constructor: function() {
        // Initialize the comments system before anything else.
        //
        // This way all the controllers and views can rely on the
        // basic comments data being already loaded and they don't
        // need to set up additional listeners and callback to wait
        // for it being loaded.
        Docs.Comments.init(this.createApp, this);
    },

    createApp: function() {
        new Ext.app.Application({
            name: "Docs",
            controllers: [
                'Auth',
                'Welcome',
@@ -32,6 +40,9 @@ Ext.define('Docs.Application', {
                'Tabs',
                'Tests'
            ],
            launch: this.launch
        });
    },

    launch: function() {
        Docs.App = this;
@@ -41,10 +52,6 @@ Ext.define('Docs.Application', {

        Docs.History.init();

        if (Docs.enableComments) {
            Docs.Auth.init();
        }

        // When google analytics event tracking script present on page
        if (Docs.initEventTracking) {
            Docs.initEventTracking();
+7 −19
Original line number Diff line number Diff line
@@ -3,30 +3,21 @@
 */
Ext.define('Docs.Auth', {
    singleton: true,
    mixins: {
        observable: "Ext.util.Observable"
    },
    requires: [
        'Ext.Ajax',
        'Ext.JSON',
        'Ext.util.Cookies'
    ],

    /**
     * @event initialized
     * Fired after the connection to comments server is successfully
     * established.
     */

    constructor: function(config) {
        this.mixins.observable.constructor.call(this, config);
    },

    /**
     * Checks if a user is logged in server side and sets up a local
     * session if they are.
     *
     * @param {Function} callback Fired after init attempt finished.
     * @param {Boolean} callback.success True when session initialized.
     * @param {Object} scope
     */
    init: function() {
    init: function(callback, scope) {
        Ext.Ajax.request({
            url: Docs.baseUrl + '/session_new',
            params: { sid: this.getSid() },
@@ -43,13 +34,10 @@ Ext.define('Docs.Auth', {
                    if (data && data.userName) {
                        this.currentUser = data;
                    }

                    this.fireEvent("initialized");
                    callback.call(scope, true);
                }
                else {
                    // when comments server is down or something else
                    // is seriously wrong, don't fire the event.
                    // By this we stop the whole comments system.
                    callback.call(scope, false);
                }
            },
            scope: this
+14 −28
Original line number Diff line number Diff line
/**
 *
 * Manages comment counts.
 */
Ext.define('Docs.CommentCounts', {
    singleton: true,
    requires: ["Docs.Comments"],

    constructor: function() {
    /**
     * Initialized CommentCounts with the main Comments class instance
     * which is used to perform queries.
     * @param {Docs.Comments} comments
     */
    constructor: function(comments) {
        this.comments = comments;
        this.loadCallbacks = [];
    },

    /**
     * Fetches all comment counts.
     *
     * @param {Function} callback Called after done.
     * @param {Object} scope
     */
    fetch: function() {
        Docs.Comments.request("jsonp", {
    fetch: function(callback, scope) {
        this.comments.request("jsonp", {
            url: '/comments_meta',
            method: 'GET',
            success: function(response) {
                this.load(response.comments);
                this.fireLoadCallbacks();
                callback.call(scope);
            },
            scope: this
        });
@@ -60,27 +67,6 @@ Ext.define('Docs.CommentCounts', {
            }, this);
        }
        return this.totals[className];
    },

    /**
     * Calls the given function after comment counts have been loaded.
     * @param {Function} callback
     * @param {Object} scope
     */
    afterLoaded: function(callback, scope) {
        if (this.counts) {
            callback.call(scope);
        }
        else {
            this.loadCallbacks.push(Ext.Function.bind(callback, scope));
        }
    },

    fireLoadCallbacks: function() {
        Ext.Array.forEach(this.loadCallbacks, function(cb) {
            cb();
        });
        this.loadCallbacks = [];
    }

});
+50 −0
Original line number Diff line number Diff line
@@ -5,10 +5,60 @@ Ext.define('Docs.Comments', {
    singleton: true,
    requires: [
        "Docs.Auth",
        "Docs.CommentCounts",
        "Ext.data.JsonP",
        "Ext.Ajax"
    ],

    /**
     * Initializes the whole comments system.
     *
     * - Establishes session with comments server.
     * - Loads comments counts data.
     *
     * @param {Function} callback Called after done.
     * @param {Object} scope
     */
    init: function(callback, scope) {
        if (!Docs.enableComments) {
            callback.call(scope);
            return;
        }

        Docs.Auth.init(function(success) {
            if (success) {
                this.enabled = true;
                this.counts = new Docs.CommentCounts(this);
                this.counts.fetch(callback, scope);
            }
            else {
                callback.call(scope);
            }
        }, this);
    },

    /**
     * True when comments system is enabled.
     * @return {Boolean}
     */
    isEnabled: function() {
        return this.enabled;
    },

    /**
     * @inheritdoc Docs.CommentCounts#get
     */
    getCount: function(type, cls, member) {
        return this.counts.get(type, cls, member);
    },

    /**
     * @inheritdoc Docs.CommentCounts#getClassTotal
     */
    getClassTotalCount: function(className) {
        return this.counts.getClassTotal(className);
    },

    /**
     * Performs request to the comments server.
     *
Loading