Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to data-bind content for an iframe using KnockoutJS?

Can anyone please tell me how to bind data to an iframe using Knockout? I have tried to do this as below, but it is not working as expected:

<iframe data-bind ="attr: { src: testcontent}"></iframe>

And Javascript:

var ViewModel = function (content) {
     this.testcontent = ko.observable(content);
};

ko.applyBindings(new ViewModel("Hello World!!"));

I want to add the text "Hello Content" into the iframe. Can anyone please help me on this?

like image 610
Meena A Avatar asked Mar 20 '13 12:03

Meena A


People also ask

What is KnockoutJS used for?

KnockoutJS is basically a library written in JavaScript, based on MVVM pattern that helps developers build rich and responsive websites.

What is data bind knockout?

Knockout's declarative binding system provides a concise and powerful way to link data to the UI. It's generally easy and obvious to bind to simple data properties or to use a single binding.


2 Answers

Warning: this obviously has security implications! Only do this with code from sources you absolutely trust.

Here's a basic, straightforward solution to build on. It allows you to have an observable with an entire html structure, and fill the iFrame with that data. If you update the html, the iframe is updated with the new version:

ko.bindingHandlers.iframeContent = {
    update: function(element, valueAccessor) {
        var value = ko.unwrap(valueAccessor());
        element.contentWindow.document.close(); // Clear the content
        element.contentWindow.document.write(value);
    }
};

ko.applyBindings({
    myHtml: ko.observable("<html>\n<head><title>Test</title></head>\n<body>\n\nMy <em>fine</em> text.\n\n</body>\n</html>")
});

You can use this like this in your view:

<p>Edit your html:</p>
<textarea data-bind="value: myHtml, valueUpdate: 'afterkeydown'"></textarea>

<p>And see it appear in an iframe:</p>
<iframe data-bind="iframeContent: myHtml"></iframe>

See this jsfiddle for a demo. The valueUpdate is merely there so the demo is clearer, it's debatable if that's a good idea in bigger scenarios.

like image 160
Jeroen Avatar answered Oct 13 '22 01:10

Jeroen


EDIT: Fiddle Updated.

http://jsfiddle.net/sujesharukil/NnT78/10/

You need to create a custom binding handler for this. I have used one such by http://jsfiddle.net/mbest/GYRUX/

and changed it to suit your needs. Take a look at both and see what works for your needs.

ko.bindingHandlers.bindIframe = {
    init: function(element, valueAccessor) {
        function bindIframe() {
            try {
                var iframeInit = element.contentWindow.initChildFrame,
                    iframedoc = element.contentDocument.body;
            } catch(e) {
                // ignored
            }
            if (iframeInit)
                iframeInit(ko, valueAccessor());
            else if (iframedoc){
                var span = document.createElement('span');
                span.setAttribute('data-bind', 'text: someProperty');
                iframedoc.appendChild(span);
                ko.applyBindings(valueAccessor(), iframedoc);
            }
        };
        bindIframe();
        ko.utils.registerEventHandler(element, 'load', bindIframe);
    }
};
like image 45
Sujesh Arukil Avatar answered Oct 13 '22 01:10

Sujesh Arukil