Knockout 3.2 and Routing.. Is this a optimal routing solution for a SPA....Code Included

323 views
Skip to first unread message

Ihear Yah

unread,
Jul 26, 2015, 12:37:28 PM7/26/15
to KnockoutJS
Found this routing solution on JSFiddle. Is this optimal for a SPA project????
I've looked @ Sammy,Director, but I like this code. So is it the best. I am always open to new solutions? Thx

Code

<!DOCTYPE html>
<html>
  <head>
    <title>Menu </title>
<script type="text/javascript" src="knockout-3.2.0.js"></script>

<body>
<input type="radio" name="choices" value="summary" data-bind="checked: selectedView" />Summary
<input type="radio" name="choices" value="details" data-bind="checked: selectedView" />Details
<input type="button" name="choices" value="summary" data-bind="checked: selectedView" />Summary
<hr/>

<div data-bind="template: { name: templateToUse, foreach: articles }"></div>

<script id="summary" type="text/html">
    <div data-bind="text: title"></div>    
</script>

<script id="details" type="text/html">
    <div>
        <h2 data-bind="text: title"></h2>
        <p data-bind="text: content"></p>
        <button data-bind="click: $parent.selectedArticle">Edit</button>
    </div>
</script>

<script id="edit" type="text/html">
    <div>
        <input data-bind="value: title" /><br/>
        <input data-bind="value: content" />
    </div>
</script>






</body>

<script>
var viewModel = {
    articles: [{
        id: 1,
        title: "Article One",
        content: "Content for article one."},
    {
        id: 2,
        title: "Article Two",
        content: "Content for article two."},
    {
        id: 3,
        title: "Article Three",
        content: "Content for article three."}
                                               ],
    selectedView: ko.observable("summary"),
    selectedArticle: ko.observable()
};

viewModel.templateToUse = function(item) {
    return item === this.selectedArticle() ? 'edit' : this.selectedView();
}.bind(viewModel);

ko.applyBindings(viewModel);


</script>
</html>



Tony B

unread,
Jul 26, 2015, 10:17:13 PM7/26/15
to KnockoutJS, anib...@gmail.com
Hello!

Could you please link add a link to a particular jsbin (or any equivalent) where this is being used? I believe that would help me better answer your question.

From what I can see right now, I believe the only issue you would run into is if you want the current URL to reflect the state of the viewModel. That is, if the user refreshes the page, will they still be seeing the same thing? And in your case, is this important to you. If so, I recommend using a "router" of sorts (https://github.com/ampersandjs/ampersand-router). If not, then this works fine. I've used this convention before when having viewModel state bound to the URL was not important to me.

-Tony

Daniele Dellafiore

unread,
Aug 4, 2015, 8:23:44 AM8/4/15
to KnockoutJS
For my project I end up using http://pagerjs.com

It took some time to really understand how to make it work properly, especially if (like me) you're not really good with KO or JS in general
But with some advices I ended up making a whole SPA with that, KO using KO components extensively.
The only issue could be that the project is built with KO 2.x, so without components. Is working ok but for example is not easy to integrate transictions when changing page without some workaround.

It the author reads, it would be great to get a revamp for KO 3.x :)

Ihear Yah

unread,
Aug 5, 2015, 2:14:21 PM8/5/15
to knockoutjs

thxs!

--
You received this message because you are subscribed to a topic in the Google Groups "KnockoutJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/knockoutjs/V4k9rObKKbc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cheng Fan

unread,
Mar 23, 2016, 9:07:47 PM3/23/16
to KnockoutJS

Carlos Mendes

unread,
Apr 1, 2016, 5:07:35 AM4/1/16
to KnockoutJS
Another suggestion for Routing:

Best Tennis

unread,
Apr 1, 2016, 10:54:55 AM4/1/16
to knockoutjs
Yo Carlos I've dropped Javascript for JAVA..a real programming language.  

Reply all
Reply to author
Forward
0 new messages