页面组成后是否可以注入Durandal小部件? [英] Is it possible to inject a Durandal widget after the page has been composed?

查看:81
本文介绍了页面组成后是否可以注入Durandal小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理durandal应用程序,并且根据某些操作,我需要使用< ;!-ko小部件:{kind:'widget / cards'}->注入小部件;<!-/ ko->

I am working on a durandal application and depending on certain action, I would need to inject a widget using <!-- ko widget: {kind:'widget/cards'} --><!-- /ko -->

目前未显示小部件,但只能在其中显示注释HTML内容。
有什么帮助吗?
谢谢

At the moment the widget is not shown but only the comment can be seen in the HTML content. Any help? Thanks

推荐答案

是。在窗口小部件绑定的直接父节点上使用 if visible 绑定。如果没有直接的父节点,则仅出于持有 if visible 绑定的目的而创建一个。

Yes. Use the if or visible binding on the immediate parent node of your widget binding. If there is no immediate parent node, create one solely for the purpose of holding the if or visible binding.

从我为 Durandal with Durandal编写的DatePicker小部件中获取以下代码(您可以观看视频此处):

Take at this code from the DatePicker Widget I wrote for Durandal with Durandal (you can see the video here):

<div>
    <div data-bind="visible: viewMode() === 'month'">
        <div data-bind="compose: {model: 'widgets/_common/datetime/datepickerMonth', view: 'widgets/_common/datetime/datepickerMonth', activate: true, activationData: messageChannel }"></div>
    </div>
    <div data-bind="visible: viewMode() === 'months'">
        <div data-bind="compose: {model: 'widgets/_common/datetime/datepickerMonths', view: 'widgets/_common/datetime/datepickerMonths', activate: true, activationData: messageChannel }"></div>
    </div>
    <div data-bind="visible: viewMode() === 'years'">
        <div data-bind="compose: {model: 'widgets/_common/datetime/datepickerYears', view: 'widgets/_common/datetime/datepickerYears', activate: true, activationData: messageChannel }"></div>
    </div>
</div>

请注意,我正在根据情况动态注入要显示的日历视图。顺便说一下,这是创建多视图窗口小部件的一个技巧(而不是必须编写自定义视图定位器,这会变得很复杂)。

Notice that I'm dynamically injecting which calendar view to show based on a condition. By the way, this is a bit of a trick for creating multi-view widgets (rather than having to write a custom view locator, which can get complicated).

当您动态注入窗口小部件时,原理是相同的(与合成相反,就像我在这里所做的那样)。

The principle is the same when you're dynamically injecting widgets (as opposed to compositions, as I'm doing here).

我使用在这种情况下,可见绑定是为了提高速度。我不想每次用户在视图之间切换时都重新创建构图。用户期望日期选择器的性能更高。

I use the visible binding in this case for speed. I don't want to recreate the compositions every time the user switches between views. Users expect a datepicker to be more performant than that.

这篇关于页面组成后是否可以注入Durandal小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆