Frage

I'm bit of confused with Browserify. I'm trying to design application with next structure:

Acces structure of application

config.js

module.export = {loaded: false};

main.js

var $ = require("jquery");

$(document).ready(function(){
   var App = require("app.js");
   App.start();
});

app.js

var TagsManager = require("./mng/tags"); 
var SettingsManager = require("./mng/settings");

function App(){
   var config = require("./config");
   this.tagsManager = new TagsManager();
   this.settingsManager = new SettingsManager();
   this.method = function(){
       // here i'm changing config json
       config.loaded = true;
   }

   this.method(); //writing from this to config
   this.tagsManager.method(); //writing from tags manager 

}

module.exports = new App();

tags.js

In this module I need to get access config.

function TagsManager(){
    var config = require("./config");
    ...
    this.method = function(){
       config.newProp =  config.loaded ? "This is new property" : null; 
    }
    ...
}

module.exports = TagsManager();

If I'm including config as a module into each other module then in every module I can read and write into config, but when I need read from another module supposed changes in config then I don't see them. I mean that when in app.js I write to config property config.loaded = true and after it I call method in tags manager that writes config.newProp = "This is new property" if config.loaded == true, I see that in tags manager config looks like it was determined in config.js.

Solution to put config to window object looks pretty musch weird. Are there any other solutions? Or I just don't understand how to use Browserify?

War es hilfreich?

Lösung

I think you're misunderstanding how the Browserify workflow works. Requiring a module into a variable and then modifying it doesn't save the information in the file where it came from. Browserify basically concatenates all your modules into a single script, with a light wrapper to read require calls. But you're still working in the browser. A global object is the right approach. If you want to save the information permanently, then you'll need a cookie, or localStorage or a database.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top