Created
August 26, 2015 18:59
-
-
Save quadrophobiac/f5f6c0407dba2a83b646 to your computer and use it in GitHub Desktop.
Electron.js main | renderer concern separation: Menu & Dialog as Edge Case - badly separated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<body> | |
<div id="container" class="container"> | |
walla walla glub glub | |
</div> | |
</body> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ipc = require('ipc'); | |
ipc.on('loadData', function(data) { | |
var fileName = data[0]; | |
fs.readFile(fileName, 'utf-8', function (err, data) { | |
// load data | |
document.getElementById('container').innerHTML = data; | |
}); | |
}); | |
ipc.on('saveData', function(fileName) { | |
var output = document.getElementById('container'); | |
//output.innerText(); | |
fs.writeFile(fileName, output.innerHTML, function (err) {}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this is the file which is called in the apps package.json {main: ""} attribute | |
var app = require('app'); | |
var BrowserWindow = require('browser-window'); | |
var Menu = require('menu'); | |
var Dialog = require('dialog'); | |
var ipc = require('ipc'); | |
app.on('ready', function () { | |
mainWindow = new BrowserWindow({width: 800, height: 600}); | |
mainWindow.loadUrl('file://' + __dirname + '/app.html'); | |
mainWindow.on('close', function () { | |
mainWindow = null; | |
}); | |
var template = [ | |
{ | |
label: 'File', | |
submenu: [ | |
{ | |
label: 'Open..', | |
accelerator: 'CmdOrCtrl+O', | |
click: function() { openFile(); } | |
} | |
] | |
}, | |
]; | |
var menu = Menu.buildFromTemplate(template); | |
Menu.setApplicationMenu(menu); | |
}); | |
function openFile() { | |
var window = BrowserWindow.getFocusedWindow(); | |
Dialog.showOpenDialog( | |
{ filters: [ | |
{ name: 'text', extensions: ['json'] } | |
]}, | |
// callback | |
function (fileNames) { | |
if (fileNames === undefined) { | |
return; | |
} else{ | |
window.webContents.send('loadData', fileNames); | |
} | |
}); | |
} // end open file | |
function saveFile() { | |
var window = BrowserWindow.getFocusedWindow(); | |
Dialog.showSaveDialog({ filters: [ | |
{ name: 'text', extensions: ['json'] } | |
]}, function (fileName) { | |
if (fileName === undefined) return; | |
window.webContents.send('saveData', fileName); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment