Node Webkit / Angular / Change Model

135 views Asked by At

Sorry for a not very specific question by someone new to node webkit, new to Angular, new to about everything in web development:

My app is based on a JSON file that I load at the init of my node webkit app and which is at the center of a bunch of calculations.

In the app, one can open a file dialog to create a new JSON file. Now, of course, I would like the app to recalculate everything based on the new JSON. It works when I press the "refresh" button of node webkit, but I couldn't get it running by using either

require('nw.gui').Window.open('index.html');

nor

require('nw.gui').Window.get().reload(3);.

I am also wondering if handling this on the node level is the good way to do it. Shouldn't it rather be done by Angular? But I couldn't really connect to the content of my controller from an "outside" javascript.

Grateful for any hint...

1

There are 1 answers

3
Thomas Weglinski On BEST ANSWER

Having logic on the page loading is always tricky and as you mentioned - requires page reloading what is not very elegant and modern applications avoid this.

In your case, I suggest that if your JSON file is not very big - store it in variable and modify it as needed. The elegant way will be to create Angular service, which can act as a "model".

angular.service('JsonService', function() {
   var json = {
    // content
   };

   return {
      getJson: function () {
        return json;
      },
      setJson: function (newJson) {
         json = newJson;
      }
   };
 });

Then, whenever you need to update JSON invoke setJson(newJson) method and modify your controllers to use the service getJson() method.

You can also add the loading/saving to file functions to this service. The loading function can be invoked in your main controller connected to your dashboard page. Then before the first page will be visible, the JSON file will be already loaded and you preserve desired behavior.