I'm new to JavaScript and Sammy development.
I am creating a SPA so we are using templates. We are also using knockout.js for data binding. I am having trouble accessing the route parameters from the viewModel. Below I have 3 snippets of code, the route, the view, and the viewModel. The email parameter is available in the route function and appears in the URL after the route is processed. However, it is not in the data variable that is expected in the view model. Data is undefined in the viewModel.
I have a javascript file (route.js) that contains the following route:
Sammy.get('#/unconfirmeduser/:emailAddress', function (context) { context.render('/views/header.html').replace("#headerPlaceHolder");
context.render('/sandbox/aw/unconfirmeduser.html', { emailAddress: context.params.emailAddress }).replace("#contentPlaceHolder");
context.render('/views/footer.html').replace("#footerPlaceHolder"); });
The view html file contains:
<h3>Unconfirmed</h3>
<p>Email not confirmed</p>
<div id="unconfirmedAccount"> <p>Email Address: <input data-bind="value: emailAddress" /></p>
<button data-bind="click: resend">Resend</button> <button data-bind="click: resend">Settings</button>
</div>
<script src="sandbox/aw/unconfirmeduser.js"></script>
<script type="text/javascript"> ko.applyBindings(new MyCompany.viewModels.unconfirmedAccount(), document.getElementById("unconfirmedAccount"));
</script>
The viewModel script file contains:
MyCompany.viewModels.unconfirmedAccount = function (data) { var self = this;
self.clientId = ko.observable(); self.emailAddress = ko.observable(data.emailAddress);
MyCompany.viewModels.unconfirmedAccount.prototype.resend = function () { var url = MyCompany.connection.getUrl("/v1/accounts/ConfirmEmailAddress");
$.get(url, { clientId: self.clientId() }, null, "json") .done(function (e) {
alert("Success Confirming email: " + e.statusText);
})
.fail(function (e) {
alert("Fail: " + e.statusText);
});
};
};
If I apply the knockout binding for the view model in the route script instead of in the view, I can pass the data into the viewModel but I am told this is not best practice.
ko.applyBindings(new MyCompany.viewModels.unconfirmedAccount(), document.getElementById("unconfirmedAccount"));
The documentation has the following line of code but I cannot find any examples of where {some: data} is being accessed in the template defined in the '/location' parameter.
this.render('/location', {some: data});
Please advise where I should be able to retrieve the data from.
Thanks