findone.js 2.1 KB
Newer Older
S
Sercan 已提交
1 2 3 4
/**
 * Created by RSercan on 1.1.2016.
 */
Template.findOne.onRendered(function () {
S
Sercan 已提交
5
    Template.initializeAceEditor('preSelector', Template.findOne.executeQuery);
S
Sercan 已提交
6 7 8 9
    Template.findOne.initializeOptions();
});

Template.findOne.initializeOptions = function () {
S
Sercan 已提交
10
    var cmb = $('#cmbFindOneCursorOptions');
S
Sercan 已提交
11 12 13 14 15 16 17 18 19 20
    $.each(CURSOR_OPTIONS, function (key, value) {
        // dont add limit, it will be 1 already
        if (value != CURSOR_OPTIONS.LIMIT) {
            cmb.append($("<option></option>")
                .attr("value", key)
                .text(value));
        }
    });

    cmb.chosen();
S
Sercan 已提交
21
    Template.setOptionsComboboxChangeEvent(cmb);
S
Sercan 已提交
22 23 24
};

Template.findOne.executeQuery = function () {
S
Sercan 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    var laddaButton = Template.browseCollection.initExecuteQuery();
    var connection = Connections.findOne({_id: Session.get(Template.strSessionConnection)});
    var selectedCollection = Session.get(Template.strSessionSelectedCollection);
    var cursorOptions = Template.find.getCursorOptions();
    var selector = ace.edit("preSelector").getSession().getValue();

    if (!selector) {
        selector = {};
    }
    else {
        try {
            selector = JSON.parse(selector);
        }
        catch (err) {
            toastr.error("Syntax error on selector: " + err.message);
            laddaButton.ladda('stop');
            return;
        }
    }

    if (cursorOptions["ERROR"]) {
        toastr.error(cursorOptions["ERROR"]);
        laddaButton.ladda('stop');
        return;
    }

    Meteor.call("findOne", connection, selectedCollection, selector, cursorOptions, function (err, result) {
        if (err || result.error) {
            var errorMessage;
            if (err) {
                errorMessage = err.message;
            } else {
                errorMessage = result.error.message;
            }
            toastr.error("Couldn't execute query: " + errorMessage);
            // stop loading animation
            laddaButton.ladda('stop');
            return;
        }

        Template.browseCollection.setResult(result.result);
        // stop loading animation
        laddaButton.ladda('stop');
    });
69
};