javascript - Calling ko.applyBindings on dynamic elements not working -
i have chat room style application , each entry processed on client once received , html generated display it. messages have tooltips , other bits of data embedded within them use knockout bindings display.
now after looking on posts around answers problem seem be:
- make sure elements exist front empty
- call ko.applybindings on new elements when added
the first 1 not feasible create elements chat comes in server, second option seems way it, require calling each chat message comes in. lot of people ko.applybindings can incur significant overheads, think mean if call upon elements rather targeting specific ones.
just on same page, here basic snippet of view around area matters:
<!-- ko foreach: {data: chatroom.entries, afterrender: chatentryrendered } --> <div class="entry-content" data-bind="html: processedcontent"></div> <!-- /ko -->
ignoring bits around that, loop around each entry, add div contain html contain bindings need knockout goodness applied. example processedcontent
like:
<span>some content <span data-bind="tooltip: 'some content here'">dynamic</span> bit of <span class="special-text">content</span></span>
so above html not processed foreach dynamically applied page, , before starts freaking out injecting html views, server not send down markup, sends down string load of tokens client converts html. without muddying waters much, in scenario chatroom.entries
object culled every , again fluctuate 0-200 (usually 100-200) entries @ given time , rest of chat entries cached in local storage, there never more 150 bound entries view @ given time.
here jsfiddle showing issue in practical example.
the main reason html
binding doesn't bind inserted html because can unsafe. if know it's safe, can use custom binding bind contents. here's example question: https://stackoverflow.com/a/17756777/1287183
fiddle: http://jsfiddle.net/mps4q/9/
Comments
Post a Comment