0

I have a jquery javascript to update to page every x minutes/seconds. I want to define this update interval in a separate json-config file so that the project can be easily configurated. I have a javascript named ConfigReader which has a method to read this config-file. I want know to load this ConfigReader to my jQuery javascript with 'require' but because the jQuery is on Clientside it does not know require.

How can I import my ConfigReader to my jQuery javascript file?

Here is the jQuery file:

var ConfigReader = require('../libs/ConfigReader');

var update = function() {
    var divs = $('div[id^="data-"]')
    for (var i = 0; i < divs.length; i++) {
        var id = divs[i].id;

        var dataType = id.split('-')[1];
        var cacheId = id.split('-')[2];
        $.ajax({
            type: "GET",
            url: '/' + dataType + 'View?cacheId=' + cacheId,
            context: id,
            success: function(data) {
                console.log("id=" + id + " this=" + this);
                $("#" + this.replace(/\./g, '\\\.')).html(data);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(this + "View", jqXHR, textStatus, errorThrown);
            }
        });
    }
}

var configReader = new ConfigReader();
var configUpdate;

var configuration = configReader.getConfigutation();
for(var i = 0; i < configuration.length; i++){
    var current = configuration[i];
    switch(current["name"]){
        case "update":
            configUpdate = parseInt(current["interval"]);
        break;
    }
}

$(document).ready(function() {
    update();
    window.setInterval(update, configUpdate);
});
sanyooh
  • 1,260
  • 2
  • 18
  • 31
  • Wait, are you on Node? or on the browser? Why do you have a `document` object when you're on Node? – Joseph Oct 29 '14 at 14:52
  • Yes I am using Node. This script is integrated in my ejs-file to update the view frequently so it is on the browser side. – sanyooh Oct 29 '14 at 14:54
  • This might help: http://stackoverflow.com/questions/7576001/how-to-require-commonjs-modules-in-the-browser Or: http://blog.brianbeck.com/post/10667967423/node-js-require-in-the-browser – bluelDe Oct 29 '14 at 14:56

0 Answers0