在IE8中,KnockoutJS 3.2显示实际的可观察函数而不是可观察值 [英] In IE8, KnockoutJS 3.2 displaying actual observable function rather than the observable's value

查看:144
本文介绍了在IE8中,KnockoutJS 3.2显示实际的可观察函数而不是可观察值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IE8中遇到了Knockout(v3.2)的问题。绑定表达式,例如

I am having issues with Knockout (v3.2) in IE8. Binding expressions such as

<div data-bind="text: $data.Property"></div>

-where Property 是一个可观察的原因要显示的敲除的可观察函数的实际文本而不是值属性应该代表。

-where Property is an observable- causes the actual text of knockout's observable function to display instead of the value Property is supposed to represent.

查看specs(针对淘汰赛的jasmine测试)我发现他们明确地不支持IE的文本节点绑定< 9(第442行) https://github.com/knockout /knockout/blob/master/spec/bindingAttributeBehaviors.js#L442

Looking at the specs (jasmine tests for knockout) I've found that they explicitly don't support text node bindings for IE < 9 (line 442) https://github.com/knockout/knockout/blob/master/spec/bindingAttributeBehaviors.js#L442

添加括号似乎解决了IE8的问题 text:$ data.Property()但是,关注的是:这会对其他浏览器产生负面影响吗?我最初的假设是出于一些常识的原因,但是我想知道是否有人知道这方面的解决方法,或者是否添加括号?

Adding parentheses seems to fix this for IE8 text: $data.Property() but, the concern is: will that have an adverse effect in other browsers? My initial assumption is no for a number of common sense reasons but I'm wondering if anyone knows of workarounds for this or if adding parentheses is it?

UPDATE 1:谢谢大家的评论。经过进一步调查后,似乎问题是正在加载重复的javascript文件。

UPDATE 1: Thank you all for the comments at this point. After further investigation it seems the issue is that there are duplicate javascript files being loaded.

更具体地说,虽然其他人也是重复的,但重复的淘汰库导致ViewModel的observables是一个不同的实例,意味着它们没有正确展开显示。

More specifically, while others are duplicated too the duplicate knockout library is causing the ViewModel's observables to be a different instance meaning they are not properly unwrapped for display.

这可以通过在淘汰库周围添加条件来证明,就像这里建议的那样:停止加载动态包含的脚本两次。添加条件使得首选绑定方法(取决于敲除展开可观察量)在IE8中起作用。

This was proven by adding a condition around the knockout library like what was suggested here: Stop IE from loading dynamically included script twice. Adding the condition made the preferred binding approach (dependent on knockout unwrapping observables) work in IE8.

到目前为止,我找到的所有问题/答案都不足以满足我的需要我将问一个关于如何使多个淘汰库加载两次的新问题。

None of the questions/answers I found so far will suffice in my circumstance so I'll be asking a new question about how to keep multiple knockout libraries from loading twice.

更新2:新问题已发布那些想要跟进的人:如何停止Knockout 3.2库加载两次

UPDATE 2: The new question is posted for those that would like to follow along: How to stop Knockout 3.2 library loading twice

推荐答案

我更新了问题但是,从技术上来说这个问题已经回答我在这里添加了它可以在SO上继续它的生命周期。

I updated the question but, since technically this question is answered I'm adding it here so it can continue it's lifecycle on SO.

经过进一步调查后,似乎问题是正在加载重复的javascript文件。

After further investigation it seems the issue is that there are duplicate javascript files being loaded.

我开始运行实例。这些使用了首选的非括号语法。显然淘汰赛正在那里正确处理事情,所以我深入潜入图书馆,从我有问题的页面,逐步走过每一行。我确认viewmodel的observable没有被解包。

I began by running the Live Examples from knockout in IE8. These worked with the preferred non-parentheses syntax. Obviously knockout is handling things properly there so I deep-dived into the library, from my problematic page, stepping through each line. I confirmed that the observables of the viewmodel were not being unwrapped.

解包observables的逻辑包括检查要解包的实例是否实际上是一个可观察的。该方法是 ko.isObservable 。这个检查说,viewmodel的可观察量实际上并不是可观察的。 ko.isObservable 方法检查每个实例表示的函数,如果引用匹配,则该方法返回true。这告诉我两个功能的实例是不同的。这也表明可能存在重复的 ko 实例。

The logic for unwrapping observables includes a check to see whether the instance to unwrap is actually an observable. The method is ko.isObservable. This check was saying that the viewmodel's observables were not actually observables. The ko.isObservable method checks the functions that each instance represent and if the references match, then the method returns true. This told me that the instances of the two functions were different. This also indicated that duplicate ko instances were likely to exist.

查看IE的网络选项卡,淘汰赛库被加载两次。这可以解释为什么viewmodel的observable和 ko.observable 实例不同。视图模型使用 ko.observable 的第一个实例。然后整个 ko 实例被淘汰库的第二次加载覆盖(当库加载时,它被执行)。

Looking at the Network tab of IE, the knockout library is being loaded twice. This would explain why the viewmodel's observables and the ko.observable instances are different. The viewmodels use the first instance of ko.observable. Then the entire ko instance is overwritten by the second load of the knockout library (when the library loads, it is executed).

通过在淘汰库周围添加条件证明了这一点,如下所示:阻止IE加载动态包含的脚本两次。添加条件使得首选的绑定语法(取决于敲除展开observable)在IE8中工作。

This was proven by adding a condition around the knockout library like what was suggested here: Stop IE from loading dynamically included script twice. Adding the condition made the preferred binding syntax (dependent on knockout unwrapping observables) work in IE8.

这就是说,简单地添加括号是不是在这种情况下有足够的解决方法,并且存在实际的解决方案或重复文件加载的解决方法 - 这是一个单独的问题。

That said, the answer is that simply adding parentheses is not a sufficient workaround in this case and that there is either an actual solution or a workaround for the duplicate file load -which is a separate issue.

这篇关于在IE8中,KnockoutJS 3.2显示实际的可观察函数而不是可观察值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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