Params not available in ViewModel

33 views
Skip to first unread message

drawalegna

unread,
May 18, 2015, 4:57:43 PM5/18/15
to sam...@googlegroups.com
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
 

Aaron Quint

unread,
May 19, 2015, 10:37:52 AM5/19/15
to sam...@googlegroups.com
The template needs to be an actual template that Sammy can render through some template engine (ie a .tmpl, .haml, .mustache) and then interpolate the variables through the relevant templating syntax. Pure html files are not interpolated.

--AQ

--
You received this message because you are subscribed to the Google Groups "Sammy.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sammyjs+u...@googlegroups.com.
To post to this group, send email to sam...@googlegroups.com.
Visit this group at http://groups.google.com/group/sammyjs.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages