0

I would like to use Knockout.js with Bootstrap Tour. In particular, I would like to attach some data bound click handler to a button within a tour step.

I create a simple tour like this:

var tour = new new Tour({
    steps: [
        {
            orphan: true,
            title: "Help Title",
            content: "<button data-bind='click: someBoundFunction'>Click me!</button>"
        }
    ]
});
tour.init();
tour.start(true);

Is it possible to use this kind of binding? Or won't this binding work because of the markup code creation mechanism of bootstraptour? I tried it like this but clicking on the button does not execute the function neither is any error message shown.

Michael Hilus
  • 1,647
  • 2
  • 21
  • 42

1 Answers1

3

Knockout doesn't update bindings automatically for newly added elements. There is a workaround though, see this question (and related). Below is a possible solution based on this workaround.

So you have a main view model, which starts your tour:

function ViewModel(){
    var self = this;
    this.tour = new Tour({ 
        steps: [
            {
                orphan: true,
                title: "Help Title",
                content: '<button id="newButton" data-bind="click: showMessage">showMessage</button>',
                onShown: function(tour) {
                    // apply the bindings after content is added
                    ko.applyBindings(self, document.getElementById("newButton"));
                }
            }
        ]
    });
    this.startTour = function() {
        this.tour.init();        
        self.tour.start(true);        
    }
    this.showMessage = function() {
        alert('Hello!');
    }
}

Working demo.

Community
  • 1
  • 1
Ilya Luzyanin
  • 7,910
  • 4
  • 29
  • 49