Sunday, June 2, 2024
188
rated 0 times [  189] [ 1]  / answers: 1 / hits: 16000  / 11 Years ago, tue, september 17, 2013, 12:00:00

I have a situation where I build a div container dynamically that has other html elements inside bonded to my knockout view model. It works up to the point where I call a method on my knockout view model that needs to redraw the whole div. After the redraw knockout stops working.



for example:



 calendar += ('<div class=month-nav-container><div class=nav-prev data-bind=click:          $root.showPreviousMonthOnPrevMonthBtnClick ><<<</div><span class=month-name-calendar>' + monthNames[month] + '</span><div class=nav-next data-bind=click: $root.showNextMonthOnNextMonthBtnClick >>>></div></div>');


I build my calendar control like so of course this is just part of it, but I hope you get the general Idea.



my knockout view model method:



self.showPreviousMonthOnPrevMonthBtnClick = function () {
alert(prev);
var $calendar = $(#calendar);
$calendar.empty();

////// previous month
if (self.calendarDisplayDate.month == 0) {
$calendar.calendarWidget({ month: 12, year: self.calendarDisplayDate.year - 1 });
} else {
$calendar.calendarWidget({ month: self.calendarDisplayDate.month - 1, year: self.calendarDisplayDate.year});
}

}


On my page load I build my calendar div, then I call ko.applyBindings() to my view model and it works. But when I click on the btn that calles my previous month method which needs to redraw calendar according to the right month, knockout stops working. I redraw the whole parent div that holds all the knockout bindings. Does anyone know solution to my problem. I need to redraw the div that has KO bindings inside so maybe what i'm looking for is some kind of bindings refresh method of Knockout ?


More From » data-binding

 Answers
14

found solution here:



How to clear/remove observable bindings in Knockout.js?



 var element = $('#elementId')[0]; 
ko.cleanNode(element);


and then



 ko.applyBindings(myVieModel, parentDiv)

[#75633] Monday, September 16, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cristinadomoniquel

Total Points: 320
Total Questions: 94
Total Answers: 94

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
cristinadomoniquel questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Tue, Dec 1, 20, 00:00, 4 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Mon, Aug 17, 20, 00:00, 4 Years ago
;