问题标签 [knockout-3.2]

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 投票
1 回答
238 浏览

knockout.js - Structure knockout.js application in different files

I'm staring with Knockout.js and I didn't find much documentation about how to properly structure a knockout application.

It is easy to follow the tutorial from the docs and its multiple examples in them or in other pages, but there's no much about the good practices regarding the file structure.

I've seen some videos of Steve Sanderson talking about how to architect a big project but they seem to be going a bit too far me. He mentions the Yeoman tool to generate a basic KO structure, but I'm using Node.js with Express.js and I already have another structure I'm working with and I'm not quite sure about how to mix both.

What I'm having at the moment is 3 main files:

  • functions.js
  • viewmodels.js (ko viewmodels and domain classes)
  • events.js (for jQuery events)

As you can see, the viewmodels.js file will become bigger and bigger so I was thinking about separating each viewmodel with its associated domain classes in different files.

The problem I found is that some of my viewmodels are related with each other as they have to access to each other data at some point.

I'm making use of a mastermodel at the moment for it:

So I can do things such as mm.user.sayHi() from a departmentViewModel

Any suggestions regarding the structure issue?

0 投票
1 回答
307 浏览

knockout.js - 使用 knockout.js 防止在组件更改时再次加载数据

我正在使用组件在一个或另一个视图之间进行切换。这就是我被建议做的事情

现在,每次我在视图之间进行更改时,都会再次加载数据。 这是该问题的再现。(检查控制台)。

在我提供的示例中,这并不是什么大问题,但是在调用外部 API 时,它是一个大问题。

我怎样才能避免这种情况?

作为一个相关问题,目前它在加载时调用 API(或在我的示例中加载数据)两次。每个注册的组件一次。那也不应该是这样的。

我应该使用 jQuery / Javascript 使用 ajax 调用 API,然后在视图模型中设置数据吗?

0 投票
1 回答
52 浏览

javascript - 为什么在 params 中配置的组件会发生变化?

根据ko关于组件生命周期的组件文档:

如果组件绑定的名称值发生明显变化,或者如果封闭的控制流绑定导致容器元素被移除,则视图模型上的任何 dispose 函数都会在容器元素从 DOM 中移除之前被调用

我不确定为什么我的组件被放置在这个小提琴上。

我在我的小提琴上出现的文档中看不到上述任何一点。如果注入的变化,我当然不希望出现componentdisposedre-instantiatedparams

任何想法为什么会发生这种情况?

0 投票
1 回答
809 浏览

knockout.js - 同步或异步绑定数据

绑定函数在 knockout.js 上是否同步?getDeliveryNote在这个例子中,我会一直得到我函数中的数据吗?

0 投票
1 回答
855 浏览

knockout.js - 在同一个模块中将淘汰代码与 jQuery 混合?

我正在为我的 jQuery / javascript 代码使用模块模式,并且我正在为它使用自执行函数,就像我为 Knockout.js 所做的那样。(与官方的 knockout.js 教程相同)。

这可能是一个非常主观的问题,但我想知道您对此的看法以及淘汰者通常如何在他们的 Web 应用程序中处理这个问题。

找到有关此主题的信息并不容易。

你会在同一个模块中混合 knockout.js 逻辑和 jQuery 事件和函数吗?或者你会把它分成不同的文件和不同的模块吗?

此外,有时有些事件在淘汰赛中并不容易绑定:

  • 滚动/鼠标滚轮事件
  • 引导事件$('#demo').on('shown.bs.modal', whatever);
  • 插件事件:$('#demo').bind('typeahead:select', whatever);
  • 不容易以$(this)正确的方式检索对象

例如,假设我有这个模块来处理客户订单:

然后,我有一个 Orders 模块来处理屏幕中 order 元素上的所有 jQuery 事件和操作,它与 knockout.js 模块分开:

0 投票
2 回答
1060 浏览

knockout.js - KnockoutJS 使用单选按钮检查绑定

我有一个对象列表,我循环并为它们创建单选按钮,然后我想将选择的对象存储在 observable 中,但我不知道该怎么做。这个小提琴是我试图做的事情的例子:jsfiddle.net/whx96806/,但它不起作用。

和js代码:

0 投票
1 回答
164 浏览

knockout.js - 使用“with”绑定的 Knockout js 组件未按预期运行

我有来自我设置的 JSFiddle 的以下代码:https ://jsfiddle.net/ktce56hr/2/

运行此代码时,我收到以下错误:

有人可以解释为什么我不能在组件视图模型上使用“rootViewModel”属性设置绑定上下文吗?

0 投票
5 回答
2064 浏览

javascript - 在 Knockout.js 3 中的 preventDefault() `click`

我在 Knockout.js 中有一个事件,只要元素中存在类data-bind="click: ...",我就会尝试禁用它。disabled

我正在尝试这个没有成功:

在线复制

我更喜欢通过使用类名来实现,因为这样我可以处理元素的样式以及与站点中多个元素的交互。

0 投票
1 回答
179 浏览

jquery - 淘汰赛绑定选择更改与 MVC 5

我在我的 MVC 应用程序模型上使用 Knockout 的“foreach”函数来加载下拉选项表。When the selected dropdown value changes, I need to track that change in the model.

我尝试在模型中的元素上使用 subscribe 选项,但是当它发生变化时,我绑定到它的函数不会触发。我知道有几种方法可以实现这种更改,但我更愿意坚持使用这种定义我的函数的格式,如视图底部所示。

模型:

看法:

我尝试了这个建议,但这里的答案不是从 MVC 模型加载数据,而是从硬编码数组加载

订阅以从 foreach 循环中选择项目

编辑:

我更新了视图以反映将数组添加为可观察的。

这是我调用来映射对象的函数

0 投票
2 回答
1963 浏览

javascript - 将事件对象传递给 knockout.js 函数

从我的角度来看,我正在使用bind参数调用该函数:

根据这个答案,我event通过这样做得到对象:

在 Chrome 中一切正常,但在 Firefox 中出现以下控制台错误:

事件未定义

我怎样才能让它在 Firefox 中也能正常工作?淘汰赛文档没有对此提供太多答案。