common.js 1.8 KB
Newer Older
S
Sercan 已提交
1 2 3
/**
 * Created by RSercan on 26.12.2015.
 */
S
Sercan 已提交
4 5 6 7 8
Template.strSessionConnection = 'connection';
Template.strSessionCollectionNames = 'collectionNames';
Template.strSessionSelectedCollection = 'selectedCollection';
Template.strSessionSelectedQuery = 'selectedQuery';
Template.strSessionSelectedOptions = "selectedCursorOptions";
S
Sercan 已提交
9

S
Sercan 已提交
10 11 12 13
Template.clearSessions = function () {
    Session.set(Template.strSessionCollectionNames, undefined);
    Session.set(Template.strSessionConnection, undefined);
    Session.set(Template.strSessionSelectedCollection, undefined);
S
Sercan 已提交
14 15
};

S
Sercan 已提交
16
Template.registerHelper('getConnection', function () {
S
Sercan 已提交
17 18
    if (Session.get(Template.strSessionConnection)) {
        return Connections.findOne({_id: Session.get(Template.strSessionConnection)});
S
Sercan 已提交
19 20 21
    }
});

S
Sercan 已提交
22
Template.registerHelper('getCollectionNames', function () {
S
Sercan 已提交
23
    return Session.get(Template.strSessionCollectionNames);
S
Sercan 已提交
24 25
});

S
Sercan 已提交
26
Template.registerHelper('getSelectedCollection', function () {
S
Sercan 已提交
27
    return Session.get(Template.strSessionSelectedCollection);
S
Sercan 已提交
28
});
S
Sercan 已提交
29 30


S
Sercan 已提交
31 32 33
/**
 * Adds remove by value functionality to arrays. e.x. myArray.remove('myValue');
 * */
S
Sercan 已提交
34 35 36 37 38 39 40 41 42
Array.prototype.remove = function () {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];
        while ((ax = this.indexOf(what)) !== -1) {
            this.splice(ax, 1);
        }
    }
    return this;
S
Sercan 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
};

/**
 * Get the parent template instance
 * @param {Number} [levels] How many levels to go up. Default is 1
 * @returns {Blaze.TemplateInstance}
 */

Blaze.TemplateInstance.prototype.parentTemplate = function (levels) {
    var view = Blaze.currentView;
    if (typeof levels === "undefined") {
        levels = 1;
    }
    while (view) {
        if (view.name.indexOf("Template.") != -1 && !(levels--)) {
            return view.templateInstance();
        }
        view = view.parentView;
    }
S
Sercan 已提交
62
};