jquery - Removing bindings in Knockout js -
i using kogrid in jquery mobile. when user click on row, go detail page. in page user can update data , save it.blow implementation
var progressgrid = function(){ var self = this; self.ticketlist = ko.observablearray(servicet.filtertsfromstatus('progress')); columndefs: [ { field: 'ticketnumber', displayname: 'ticket', celltemplate: "<button data-bind=\"click: function() {getrowdetails($parent.entity)}\"><span data-bind=\"text:$parent.entity['ticketnumber']\"></span></button>", width: '*', minwidth: "200px" } self.getrowdetails = function(row){ var rtjs = servicet.getticketbyid(row.id); servicet.gototicketdetails(rtjs,false); }, }
ticket object
function runticket(jsrunticket) { ko.validatedobservable(ko.mapping.fromjs(jsrunticket, validationmapping, self)); this.save = function (action) { } }
service method
gotorunticketdetails: function(ticketjs,readonly){ ko.cleannode(document.getelementbyid('runticketdetailspage')); var rt = new ticket(ticketjs); ko.applybindings(rt, document.getelementbyid("ticketdetailspage")); $.mobile.changepage($('#ticketdetailspage')) ; }
when click row button in grid, go "ticketdetailspage" , update values , save server well.to first saving part functionality working fine, if go again grid load ticket , save "save message appear twice"
if save 5 tickets "save" message appear 5 times. think there old tickets event not updating dom, me solve issue,i have been trying solve 2 days, still couldn't find proper solution,
thank in advance
ko.cleannode
meant make sure memory freed when bindings no longer needed. bindings, though, don't remove event handlers because it's not necessary free memory. should not use ko.cleannode
try re-apply bindings.
knockout supports 2 methods of changing view model that's bound dom section. first through templates , second through observable view models.
template method
this method binds new set of dom elements (copied template). there various methods of defining , rendering template; 1 use knockout's ko.rendertemplate
method. first, make sure you've defined template in html using <script type="text/html" id="ticketdetailspagetemplate">
. you'll use template this:
gotorunticketdetails: function (ticketjs, readonly) { var rt = new ticket(ticketjs); ko.rendertemplate("ticketdetailspagetemplate", rt, {}, document.getelementbyid("ticketdetailspage")); $.mobile.changepage($('#ticketdetailspage')) ; }
observable view model method
this method works existing code in dom elements retained , re-bound new view model. instead creating new event handlers, method updates event handlers new view model. here's 1 way it:
gotorunticketdetails: function (ticketjs, readonly) { var elementtobind = document.getelementbyid('runticketdetailspage'); var existingcontext = ko.contextfor(elementtobind); var rt = new ticket(ticketjs); if (existingcontext && ko.isobservable(existingcontext.$rawdata)) { existingcontext.$rawdata(rt); // update observable new view model } else { ko.applybindings(ko.observable(rt), elementtobind); // initialize observable view model } $.mobile.changepage($('#ticketdetailspage')) ; }
Comments
Post a Comment