Skip to content Skip to sidebar Skip to footer

Setting A Knockout Observable Using Sammy For Routing

I have a SPA using knockout JS for data binding and sammy for routing. I have a deck of cards that I am trying to have a dynamic routing to. My problem is that it doesn't work when

Solution 1:

In your code the value assignment is not correct unless you are using Knockout-es5 plugin. here is the correct code

var app = $.sammy('#content', function () {

    this.get('#deck/:id', function (context) {
        showPage("createDeck", ": Create Deck");
        console.log(this.params.id);
        deckView.deck(newDeck(1, "test", null));
        console.log(deckView.deck());            
    });
}); 

Solution 2:

The way I've done this before is to define my Sammy() routes within the ViewModel. Shorter example for brevity:

(function($) {
    functionViewModel() {
        var self = this;

        self.deckId = ko.observable(null);

        Sammy(function() {
            this.get('#/deck/:deckId', function(context) {
                self.deckId(this.params.deckId);
            });
        });
    }

    $(function() {
        ko.applyBindings(newViewModel());
    });
})(jQuery);

That way you can access your observables, etc, via self.

Post a Comment for "Setting A Knockout Observable Using Sammy For Routing"