I am currently working on an application which requires a new window to be open in the browser on the click of button and then as a user types into a textbox on the main window the new window will update accordingly. I have used knockout before, but for some reason I am having problems getting the second windows view to update. Here is my current code.
//main.js
$(function () {
var viewModel = new ViewModel();
ko.applyBindings(viewModel);
var newwindow;
$("#new-window-btn").click(function () {
newwindow = window.open("a/path/to/newwindow.html", "New Window","status=1,width=350,height=350");
newwindow._ViewModel = viewModel;
});
});
function ViewModel() {
var self = this;
self.textbox = ko.observable("");
};
This is the index.html and contains a very basic button which will open the new window, a textbox which the user will type into and a span tag to show I am not crazy.
//index.html
<div id="new-window-btn" class="btn">new window</div>
<textarea cols="3" rows ="3" data-bind="value:textbox,valueUpdate:'afterkeydown'"></textarea>
<span data-bind="text: textbox"></span>
This is the code for the second window which pops up when the user clicks on the new window button in the index.html
//newwindow.html
<script type="text/javascript">
$(function () {
var viewModel = window._ViewModel;
ko.applyBindings(viewModel);
$("#alert-viewModel").click(function () {
alert(viewModel.textbox());
});
});
</script>
<span data-bind="text:textbox()"></span>
<div id="alert-viewModel" class="btn">show texbox value</div>
When the user types into the textbox on the main page, the span tag on that page updates automagically. When the user clicks on the new window button the new window pops up with the text the user just entered, when the user continues to type in the main windows textbox the span tag in the secondary window does NOT update. However when a user presses the "show texbox value" button the text is displayed in an alert box, and HAS BEEN UPDATED! So my question is why is my span tag in the second window not updating when the ViewModel clearly has been(because of the value displayed from the "show texbox value" button).
Quick Comment: For some reason accessing a file through window.open("somepath"); does not actually work properly in the context of this question. I found that I need to load the new window file in a small HTTP server and make "somepath" an actual url. (Which is why there is not some example code attached to this question).