问题标签 [durandal-2.0]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
2520 浏览

javascript - 使用 Durandal 的单个网页应用程序的全局变量

我的应用程序具有三个“全局”的东西:具有 lat 和 lng 值的用户位置、用户是否登录以及他们选择的活动(将其视为一个类别)。

我希望在整个应用程序中使用这些值。每个屏幕都会使用这个日期的一些集合,所以每页加载它不是正确的答案。除非用户创建事件:不同的位置、活动或登录/注销,否则信息也不会改变。

我应该如何设置?

我的想法是在启动时应该有一个初始加载,在我的 main.js 文件中我加载每个然后 app.start 像这样:

站点服务.js

main.js

定义([..., 'modules/siteService'], function (..., site) { ...

});

这确实确保了数据已加载。从那里在我的每个服务中,我将信息存储到本地存储中,因此我不必继续触发 AJAX 调用。如果没有本地存储,它会。

当我在其他页面中需要这些数据时,问题就会出现。我最终在每个页面的激活方法中调用我的 site.init().then() 。这很草率。

外壳.js

然后再次进入:

欢迎.js

我想最初设置这些值,每个页面最初都从这些值加载,然后使用 Durandal 的 pub/sub 对事后的任何更改做出反应。

我正在考虑仅使用 pub/sub,但后来觉得在加载与页面加载相关的数据时,我会遇到先有鸡还是先有蛋的问题(IE 如果我的 AJAX 先加载,而我的页面没有加载,事件触发,但页面永远不会得到更新)。

是否可以在 javascript 中设置类似静态类的东西来最初提取数据,然后在整个视图模型中共享它?或者,有没有办法保证在我最初加载页面时该值存在,例如 app.on('value', { initialize: true })?

0 投票
1 回答
934 浏览

knockout.js - durandal viewmodel 的新实例仍然反映以前的值

在尝试使用 durandal 的稍微复杂的构图场景时,我遇到了一个奇怪的问题。

我想动态组合表格每个单元格的内容。例如,我有以下文件:

Welcome.html => 包含动态呈现 html 表格的代码

Welcome.js => 对应的视图模型

cell-1.html => 显示第一列的视图

cell-1.js => cell-1 对应的视图模型

同样是 cell-2 的视图和视图模型。

我面临的问题是,即使组合工作正常并且为 cell-1.js 调用了两次激活函数(每行一次),可观察的值name将包含前一个值。(即值将是“约翰”,当它试图将其设置为“摩根”时)。这很奇怪,因为 cell-1.js 返回了一个构造函数,因此 durandal 应该创建了视图模型的另一个实例。总而言之,这导致两行都包含相同的数据。

我确信我必须调整一些小问题才能让它工作,但我所有的努力都是徒劳的。

任何帮助将不胜感激。

0 投票
3 回答
678 浏览

durandal - durandal 本地化(服务器视图)

我正在用 durandal 开发一个 SPA。我依赖于 ASP.NET Web API 和 ASP.NET MVC。对于本地化需求,我已经按照 durandal v2 文档中的说明设置了 i18n 模块,即使我一直想在服务器端管理大部分全球化,这也能正常工作:我的意思是在 ASP.Net mvc (*.cshtml) 视图。

但是,由于视图是按需加载的,我怎样才能以某种方式自定义视图(html/cshtml)异步加载调用以请求视图的特定语言(例如提供 url 参数以指定视图语言) .

我已经开始寻找加载视图(html/cshtml)的位置,但到目前为止还没有找到位置)。你能给我任何解释/技巧吗?(是由 durandal、ko、require..?)

谢谢!

编辑:我开始探索 durandal 代码。我刚刚明白它正在使用 require.js,即使是通过 text.js 插件加载的视图/html文件。所以现在我的目标是挂钩该过程,以便将请求的页面文化添加到调用中(url 请求参数或 http 标头)。关于如何以最简单的方式实现这一目标的任何想法?(通过编写等效的 text.js 或者只是通过在 viewEngine.js 中破解 convertViewIdToRequirePath ?)

0 投票
1 回答
883 浏览

javascript - How to load custom client controls/widgets dynamically in the view based on json data?

Requirement: I will be getting json data using REST service from a server. That json data will tell me the what type of controls (ControlID) to present to the user and what is that data that needs to be inserted in those controls. So, the basic need is to have multiple client-side controls and when a view will load, it should make a layout depending on the controls and the data that needs to be presented. It is sort of dashboard concept where all the tiles are kind of custom controls eg. iGoogle.

The json data that I will get from server will include collection of controlId property and other property with values to customise it and the data that needs to go into the control. eg:

So, as such I have to work with multiple control IDs, I think I will have to manage a configuration file which can have id and respective module/widget/control's name or path in a folder so as to load.

What I have researched and implemented: I have started researching for Single Page App and for now I’m using Durandaljs with asp.net mvc5. Below is the code using which I have created two widgets - a “label” and a “textbox”. There is not any fancy customisation in these two but I just want to see how can I achieve this. I’ve started looking into widgets as I believe its a good option to go - a separate view and processing logic - view and viewmodel per widget. I may be wrong if there is any another option available for my requirement.

For now, I am just using those two widgets from my sample view and passing on some properties from its view model.

My questions - Please answer with samples/gist/jsfiddle if possible.

  • However, I do want to load a widget say “A” and this widget should also then use widgets label and textbox. Logically, its going to be a custom component made up of other components. The idea is to divide a big widget into different small widgets to that they can work as standalone with its unique customisation and a view and they can be grouped together to be presented to the user.

  • Is there any other alternative to durandaljs widgets that I should research ? please give examples if possible.

  • As I have mentioned above I need to read the json data from server to make each view and identify which controls to load. Until now, I have only worked with static html but this is kind of different and new to me. Could anyone please help me to find a way of loading these custom widgets on demand as per the json data from server.

Code for Widgets and main view in the app:

Widget - label

html:

javascript:

Widget - textbox

html:

javascript:

Main html page where I'm using the above widgets:

html:

0 投票
0 回答
306 浏览

javascript - jquery可拖动以编程方式恢复

我目前正在开发一个日历,可以将活动拖放到其他日期。当一个活动被放到不同的日子时,我使用 durandal 的对话框插件显示一个自定义模式。问题是当用户关闭模式时,活动必须恢复到其原始位置。删除活动时,将调用以下代码:

在我的 calendarView 中,我实现了 revertActivity 事件以将 revert 设置为 true,但该函数永远不会重新评估自身,但我能够接收到新的 revert 值(true)。

自定义事件代码:

0 投票
1 回答
633 浏览

javascript - DurandalJS 和使用参数组合子视图

我在从 Knockout 的模板绑定过渡到 Durandal 组合绑定时遇到了一些困难。

在我的旧项目中,我有一个选项卡列表,可以通过将它们放入可观察的“selectedTabSection”中来交换到中心舞台。templateId 是子视图的一个属性。所以在我的父视图中,我创建了我的子模型的实例,如下所示:

然后,当我想展示一个时,我会将其放入活动的 observable 中:

当我更改为 compose 绑定时,似乎 Durandal 找不到我的 ViewModel 的关联视图,因为我正在绑定模型的实例而不是 ViewModel 本身的构造函数。换句话说,

找到合适的视图,而

才不是。

如何让 viewLocator 了解我正在传递一个实例而不是 ViewModel 构造函数本身?如果我不能,我如何将参数传递给我的子视图,因为它们在组合之前还没有被初始化?

编辑/更新:

看起来这与我如何组成我的孩子 ViewModels 有关。当您从 ViewModel 的构造函数返回对象时,Durandal 似乎有问题。

这似乎按预期工作:

而这:

才不是。关于从构造函数返回对象的一些事情会使 DurandalJS 在尝试定位 View 时迷失方向,并且还会弄乱各种范围。我正在考虑重新编写我的脚本以适应,但是这种从构造函数返回对象的模式对我来说很好(在 Durandal 之前)很好奇......

0 投票
1 回答
75 浏览

durandal - 如果用户快速单击菜单,则不再显示 Durandal 视图

我在我的 SPA 中使用 Durandal 2.0 & Breeze。

我的司机(司机)有一个侧边栏菜单,用户可以在其中单击子菜单(Récents、Disponibles、Indisponibles)以使用不同的参数调用我的视图。这将用数据填充 koGrid。数据在激活调用中获取,koGrid 的绑定在 compositionComplete 中完成。

在此处输入图像描述

大多数时候一切都很顺利。当我快速单击子菜单(调用相同的视图)时,事情就出错了。示例:我单击“最近”并立即(不等待视图显示)单击“Disponibles”。

我有以下激活:

我有以下compositionComplete代码:

当我跟踪活动时,我注意到如果用户快速单击子菜单,则激活没有时间完成并再次调用(对于用户的第二次单击)并且 compositionComplete 不会执行。在那之后,视觉上什么都没有发生。好像被屏蔽了

知道如何防止这个问题吗?

谢谢。

0 投票
1 回答
641 浏览

javascript - Durandal 2.0 小部件处理

应该如何在 v2.0 中正确处理小部件?从我读过的内容来看,这应该是在从 DOM 中删除时自动完成的,但是使用说 jQuery.remove() 从 DOM 中删除我的小部件不会导致调用 canDeactivate() 或 deactivate() 。是否有一种特定的方式必须从 DOM 中删除才能调用停用回调?

0 投票
1 回答
148 浏览

css - DurandalJS app.showMessage 模态导致 Y 溢出伪影

Durandal 具有对模态对话框的组合支持。我已经能够成功地使用它们,尽管有一个奇怪的图形工件我似乎无法根除。我很确定它是具有类“durandal-wrapper”的 div 的一部分,尽管它直到运行时才存在。如何在不求助于猴子修补一些“溢出-y:blah;”的情况下防止这种行为 在 Durandal 创建并附加到 DOM 之后,将其添加到元素上?

示例 1 示例 2

0 投票
1 回答
395 浏览

durandal - Durandal - Pub/sub - 何时清理订阅

我们的 durandal 应用程序有许多模块化 UI 组件(全部使用基于 AMD 构造函数的视图模型创建),我想开始添加 pub/sub 通信。我需要确定确保正确“清理”订阅的最佳方法。

何时/如何清理这些订阅?

对于少数几个视图模型,该deactivate事件似乎已经足够了,但其余的呢?我没有看到允许视图模型在其自身之后可靠地“清理”的特定机制。

在我实施某种一次性模式之前,是否有一些我在这里遗漏的明显方法?

后续问题:除了调用 subscription.off() 来正确清理之外,还有什么可以做的吗?