Skip to main content

Clear cache

$(document).ready(function () {

    // Clear cache
    // location.reload(true);

    // Create the namespace instance
    let ns = {};

    // Create the model instance
    ns.model = (function () {
        'use strict';

        let $event_pump = $('body');

        // Return the API
        return {
            'post_file': function (file) {
                let ajax_options = {
                    type: 'POST',
                    url: '/file',
                    async: true,
                    contentType: "application/json; charset=utf-8",
                    accepts: 'application/json',
                    dataType: 'json',
                    data: JSON.stringify({ 'paths': file }),
                };
                $.ajax(ajax_options)
                    .done(function (data) {
                        $event_pump.trigger('model_post_file_success', [data]);
                    })
                    .fail(function (xhr, textStatus, errorThrown) {
                        $event_pump.trigger('model_error', [xhr, textStatus, errorThrown]);
                    })
            },
            'get_file': function (which) {
                let ajax_options = {
                    type: 'GET',
                    url: '/file/' + which,
                    accepts: 'application/json',
                    dataType: 'json'
                };
                $.ajax(ajax_options)
                    .done(function (data) {
                        $event_pump.trigger('model_get_file_success', [data]);
                    })
                    .fail(function (xhr, textStatus, errorThrown) {
                        $event_pump.trigger('model_error', [xhr, textStatus, errorThrown]);
                    })
            },

        };
    }());

    // Create the view instance
    ns.view = (function () {
        'use strict';

        return {
            'get_file': function (data) {
                console.log(data)
            },
            'post_file': function (data) {
                console.log(data)
            },
            'success': function (data) {
                console.log(data)
            },
            'error': function (error_msg) {
                $('.error')
                    .text(error_msg)
                    .css('visibility', 'visible');
                setTimeout(function () {
                    $('.error').css('visibility', 'hidden');
                }, 3000)
            }
        };
    }());

    // Create the controller
    ns.controller = (function (m, v) {
        'use strict';

        // Define local variables
        let model = m,
            view = v,
            $event_pump = $('body');


        // Have local functions here
        function startup_function() {

        }

        // Add event handles here

        document.getElementById('id2').addEventListener('change', function () {
            // Do something
            model.post_file()
        });

        // Jquery
        $('#id2').click(function () {
            // Do something
            model.get_file()
        });

        $event_pump.on('model_post_file_success', function (e, data) {
            console.log(data);
            view.post_file(data);
        });

        $event_pump.on('model_get_file_success', function (e, data) {
            console.log(data);
            view.get_file(data);
        });

        $event_pump.on('model_error', function (e, xhr, textStatus, errorThrown) {
            let error_msg = textStatus + ': ' + errorThrown + ' - ' + JSON.parse(xhr.responseText).message;
            console.log(error_msg);
            view.error(error_msg);
        })

        // Put here what you want to do on startup
        startup_function()

    }(ns.model, ns.view));

});