如何使用 knockout.js 显示警报/弹出窗口/窗口
我认为我们不应该在视图模型中引用 html 元素。所以我认为我不能做 $('#id').dialog() 或不应该做alert('message') 或 window.open()。 那我该怎么办呢?
I think we shouldn't have references to html elements in viewmodel. So I think I can't do $('#id').dialog() or shouldn't do alert('message') or window.open().
Then how should I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看此答案,了解使用自定义绑定从视图模型控制 jQuery UI 对话框的示例。
将jquery ui对话框与knockoutjs集成
将可观察值设置为true打开将对话框设置为 false 将其关闭。
Take a look at this answer for a sample of using a custom binding to control a jQuery UI dialog from your view model.
integrating jquery ui dialog with knockoutjs
Setting an observable to true opens the dialog, setting it to false closes it.
实际上,使用对话框根本不费力。只需让 Knockout 进行绑定,数据就会出现在对话框元素中。在我的应用程序中,对话框是否打开没有任何影响。淘汰赛仍会更新数值。
但考虑到这一点,我在逻辑部分进行绑定。因此,我将在两次调用中将 ko.applybindings 分别应用于页面主体和对话框。
对于警报,您必须传入要显示的文本。因此,要在这里使用淘汰赛,您必须让淘汰赛更新隐藏元素。然后获取结果值以显示在警报中。
对于 window.open,不确定您关心什么。据我了解,这与进入新页面相同。因此该页面的逻辑将完成填充。
Actually, using dialogs are no effort at all. Just let Knockout do it's binding and the data will be in the dialog elements. In my app, if the dialog is open or not has no effect. Knockout will still update the values.
One consideration though, I do my bindings in logical sections. So I would have the ko.applybindings apply to the main body of the page and the dialog separately in two calls.
With regards to alert's, you have to pass in the text to be displayed. So to use knockout here, you would have to let knockout update a hidden element. Then get the resulting value for display in your alert.
For window.open, not sure what your concern is. As I understand it, that is the same as going to a new page. So that page's logic would do the populating.