Note that there are some explanatory texts on larger screens.

plurals
  1. POUnique element ids in knockout.js: Chrome OK, FF and Opera fail
    primarykey
    data
    text
    <p>I am working on a library of SVG UI elements and planning to use knockout.js. Here is a very basic page that does what I need without knockout.js, it works fine in Chrome, FF, Opera. It draws a piece of pipe:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;div width="100px" height="100px"&gt; &lt;svg width='100px' height='100px'&gt; &lt;linearGradient id="gradId" y1='-20%' x1='0%' y2='100%' x2='0%' gradientUnits='objectBoundingBox'&gt; &lt;stop offset='-20%' style="stop-color: #303030"/&gt; &lt;stop offset='40%' style="stop-color: #D0D0D0"/&gt; &lt;stop offset='100%' style="stop-color: #303030"/&gt; &lt;/linearGradient&gt; &lt;rect id="myRect" x='0%' y='30%' width='100%' height='40%' /&gt; &lt;/svg&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; document.getElementById("myRect").style.fill = "url(#gradId)"; console.log(document.getElementById("myRect").style.fill); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Now to the knockout.js templates. For every instance of the templated control, I will need unique id, so correspondent gradients are referenced properly and different pipe elements do not share gradients. With some help from <a href="https://stackoverflow.com/questions/9233176/unique-ids-in-knockout-js-templates">this post</a>, I crafted the following html:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="js/knockout-2.1.0.debug.js "&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; // knockout code inspired by https://stackoverflow.com/questions/9233176/unique-ids-in-knockout-js-templates ko.bindingHandlers.uniqueId = { init: function (element, valueAccessor) { // Generate new element id element.id = ko.bindingHandlers.uniqueId.prefix + valueAccessor() + ko.bindingHandlers.uniqueId.prefix + (++ko.bindingHandlers.uniqueId.counter); console.log("Element id:" + element.id); }, prefix: "autoId_", counter: 0 }; ko.bindingHandlers.uniqueStyleFill = { init: function (element, valueAccessor) { // Use recently generated element id in the style.fill value var value = ko.bindingHandlers.uniqueId.prefix + valueAccessor() + ko.bindingHandlers.uniqueId.prefix + ko.bindingHandlers.uniqueId.counter.toString(); element.style.fill = "url(#" + value + ")"; console.log("Binding:" + element.style.fill); } }; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;!-- Define ko template plumbing-horizontal-pipe that uses bodyColor and bodyColorLight attributes of the bound object --&gt; &lt;script type="text/html" id="plumbing-horizontal-pipe"&gt; &lt;svg width='100px' height='100px'&gt; &lt;linearGradient data-bind="uniqueId: 'horPipeGrad_'" y1='-20%' x1='0%' y2='100%' x2='0%' gradientUnits='objectBoundingBox'&gt; &lt;stop offset='-20%' data-bind="style: {'stop-color': bodyColor()}"/&gt; &lt;stop offset='40%' data-bind="style: {'stop-color': bodyColorLight()}"/&gt; &lt;stop offset='100%' data-bind="style: {'stop-color': bodyColor()}"/&gt; &lt;/linearGradient&gt; &lt;rect x='0%' y='30%' width='100%' height='40%' data-bind="uniqueStyleFill: 'horPipeGrad_'"/&gt; &lt;/svg&gt; &lt;/script&gt; &lt;div width="100px" height="100px"&gt; &lt;!-- Load ko template defined above, bind 'pipe' --&gt; &lt;svg width="100" height="110" data-bind="template: { name: 'plumbing-horizontal-pipe', data: pipe }" /&gt; &lt;/div&gt; &lt;!-- ko view model with one item 'pipe' --&gt; &lt;script type="text/javascript"&gt; function MyViewModel() { this.pipe = { bodyColor: ko.observable("#FF0000"), bodyColorLight: ko.observable("#FFA0A0") }; } var g_vm = new MyViewModel(); // This should make the browser display a red pipe. Works in Chrome, doesn't work in FF and Opera. ko.applyBindings(g_vm); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>It works fine in Chrome - the red pipe is displayed. FF and Opera keep displaying black rectangle, which means the SVG object doesn't pick up the gradient. I checked FF log output, it looks totally fine:</p> <pre><code>[21:30:08.606] Element id:autoId_horPipeGrad_autoId_1 [21:30:08.610] Binding:url("#autoId_horPipeGrad_autoId_1") none </code></pre> <p>As one can see, the id for the gradient is generated and used in the fill attribute for the rect style. But the browser doesn't use this gradient when displaying the rectangle.</p> <p>What's wrong here? Any workarounds? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload