淘汰赛:通过 iframe 访问视图模型?

发布于 2024-12-02 10:46:29 字数 2021 浏览 1 评论 0原文

我正在构建的应用程序需要使用 iframe 进行所见即所得文本编辑,并且这些 iframe 需要连接到 viewModel,但我发现这与淘汰赛冲突......或者至少淘汰赛似乎不应用绑定当我尝试通过父对象访问它们时。

这是一些代码...

    <script type="text/javascript">
        $(function(){
            ko.applyBindings(parent.model.project, $('#root')[0]);
        });
    </script>

    <ul id="root" data-bind="template: {name: function(){return type()},
                                                                            foreach: populate() }"></ul>

    <script id="document" type="text/html">

        <li class="draft" draft="${draft()}" data-bind="css: {expanded: $data.expanded}">
            <span data-bind="click: function(){parent.model.project.expand($data, 'draft')}">
                ${ordinal(draft())} Draft
                <img src="icons/close-black.png"
                    data-bind="click: function(){parent.model.project.deleteDraft($data)},
                                         css:{ only: function() {parent.model.project.drafts > 1} }"/>
            </span>
            <div>
                <ul data-bind="css: {expanded: $data.expanded},
                        template: {
                            name: 'draft',
                            foreach: $data.draftItems,
                        }"
                >
                </ul>
            </div>
        </li>

    </script>

    <script id="draft" type="text/html">
        {{if $data.name}}
        <li class="${name}">${name}</li>
        {{/if}}
    </script>

好吧,这不是一个所见即所得的文本编辑器,但它仍然说明了我的观点。

现在的问题是,当我写这篇文章时,它运行得很好。我有 viewModel 的一部分,所有绑定都在仅由该 html 访问的 js 文件中定义...但我需要父窗口访问相同的 ViewModel,就像我使用工具栏按钮的所见即所得编辑器一样其他外部控件,所以我将 viewModel 的那部分移动到定义其余部分的文件中......但现在它不起作用!

在我之前的外部文件中,我仍在使用parent.model访问父视图模型,但现在无法直接独占访问该模型,它似乎不起作用。问题是,虽然我可以使用 console.log 访问视图模型,但我也可以从中进行 document.write,它会将事件触发回 viewModel,并且我的视图最初会更新,但在最初的视图之后,它不再更新。

有办法解决这个问题吗?

The app I'm building requires the use of iframes for wysiwyg text editing, and these iframes need to be wired up to the viewModel, but I'm finding that this clashes with knockout... or at least knockout seems not to apply bindings when I try to access them through the parent object.

Here's some code...

    <script type="text/javascript">
        $(function(){
            ko.applyBindings(parent.model.project, $('#root')[0]);
        });
    </script>

    <ul id="root" data-bind="template: {name: function(){return type()},
                                                                            foreach: populate() }"></ul>

    <script id="document" type="text/html">

        <li class="draft" draft="${draft()}" data-bind="css: {expanded: $data.expanded}">
            <span data-bind="click: function(){parent.model.project.expand($data, 'draft')}">
                ${ordinal(draft())} Draft
                <img src="icons/close-black.png"
                    data-bind="click: function(){parent.model.project.deleteDraft($data)},
                                         css:{ only: function() {parent.model.project.drafts > 1} }"/>
            </span>
            <div>
                <ul data-bind="css: {expanded: $data.expanded},
                        template: {
                            name: 'draft',
                            foreach: $data.draftItems,
                        }"
                >
                </ul>
            </div>
        </li>

    </script>

    <script id="draft" type="text/html">
        {{if $data.name}}
        <li class="${name}">${name}</li>
        {{/if}}
    </script>

OK, this isn't a wysiwyg text editor, but it still illustrates my point.

Now the thing is, when I wrote this it worked perfectly. I had the part of viewModel that all the bindings refer to defined in a js file accessed only by this html... but I need the same ViewModel to be accessed by the parent window, as I would with a wysiwyg editor for toolbar buttons and other external controls, so I moved that part of the viewModel to the file where the rest of it was defined... and now it doesn't work!

In the external file that I had previously I was still accessing the parent view model using parent.model, but now not having direct exclusive access to that model it doesn't seem to work. The thing is though that I can access the view model with console.log, I can document.write from it too, it fires off events back to the viewModel, and my view updates initially, but after that initial one it no longer updates.

Is there a way to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀手六號 2024-12-09 10:46:29

iframe 不会继承父元素的绑定。

你不能让它以这种方式工作,因为 iframe 实际上是另一个页面中的单独页面。

每个 iframe 都需要有自己的视图模型。如果该视图模型需要来自另一个视图模型,则需要通过全局 JS 对象或消息传递或其他机制来共享该数据。

iframes won't inherit bindings from parent elements.

You can't make it work that way, as iframes really are separate pages within another page.

Each iframe will need to have its own view model. If that viewmodel needs to come from another view model, you'll need to share that data via global JS objects or message passing or some other mechanism.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文