简体   繁体   English

谷歌浏览器和互联网浏览器不同

[英]Different in google chrome and internet explorer

My application uses knockout.js custom elements. 我的应用程序使用knockout.js自定义元素。 The load sequence of the viewmodel and template are different in google chrome and internet explorer. 视图模型和模板的加载顺序在Google Chrome和Internet Explorer中是不同的。

In my web application I have defined a number of custom elements like: 在我的Web应用程序中,我定义了许多自定义元素,如:

ko.components.register('account-management-me', {
        viewModel: {  require: 'Scripts/app/components/accountManagementMe/ViewModel' },
        template: { require: 'text!Scripts/app/components/accountManagementMe/' + language + 'View.html' }
    });

With Internet Explorer I observed the following load sequence: 使用Internet Explorer,我观察到以下加载顺序:

  1. ViewModel.js ViewModel.js
  2. enView.html enView.html

With Chrome I observed the following load sequence: 使用Chrome,我观察到以下加载顺序:

  1. enView.html enView.html
  2. ViewModel.js ViewModel.js

Hence fields bound in the view from the ViewModel.js are undefined and an exception is thrown by knockout. 因此, ViewModel.js在视图中绑定的字段是未定义的,并且由knockout抛出异常。

Is there a way to influence the load order? 有没有办法影响负载顺序?

Is there a way to influence the load order? 有没有办法影响负载顺序?

Yes, if you register your component as a single module, as shown in the documentation , then you can make it so that your view model is dependent on your template and so your template will always be loaded before your view model. 是的,如果您将组件注册为单个模块( 如文档中所示) ,则可以使视图模型依赖于模板,因此您的模板将始终在视图模型之前加载。

Knockout is using RequireJS to load the view model and the template. Knockout正在使用RequireJS加载视图模型和模板。 As explained in the documentation, it issues a plain require call with the module names you to ko.components.register . 如文档中所述,它会发出一个普通的require调用,并将模块命名为ko.components.register For your code, the equivalent require calls would be: 对于您的代码,等效的require调用将是:

require(['Scripts/app/components/accountManagementMe/ViewModel'], callback);
require(['text!Scripts/app/components/accountManagementMe/enView.html', callback);

These two calls cannot constrain the load order. 这两个调用不能约束加载顺序。 It does not matter that .../ViewModel is first. 无关紧要.../ViewModel是第一个。 (Actually, it could be second.) Both calls are asynchronous and can resolve in any order unless the modules use define to establish a dependency of one on the other. (实际上,它可能是第二个。)两个调用都是异步的,并且可以按任何顺序解析, 除非模块使用define来建立一个的依赖关系。 This is just how RequireJS works. 这就是RequireJS的工作原理。 Without an explicit dependency, the order is indeterminate and can vary due to browser internals, network conditions, etc. 没有明确的依赖关系,订单是不确定的,并且可能因浏览器内部,网络条件等而有所不同。

To register your component as a single module, you'd do: 要将组件注册为单个模块,您需要执行以下操作:

ko.components.register('account-management-me', 
  { require: 'Scripts/app/components/accountManagementMe/ViewModel' });

And the module referred above would contain something like: 上面提到的模块将包含以下内容:

define(['knockout', 'text!Scripts/app/components/accountManagementMe/enView.html'], function(ko, template) {
    function ViewModel() {
    }

    ViewModel.prototype.foo = function() { ... };

    return { viewModel: ViewModel, template: template };
});

In this example, I've made the template module name static string. 在这个例子中,我已经将模板模块名称设为静态字符串。 You want to load different templates depending on the configured language but putting strings computed at run time in the dependencies of a define is not supported. 您希望根据配置的语言加载不同的模板,但不支持将在运行时计算的字符串放在define的依赖项中。 You can make it work for some trivial cases but if you want to optimize your modules with r.js , it will choke on computed strings. 你可以使它适用于一些简单的情况,但如果你想用r.js优化你的模块,它会阻塞计算字符串。 To get support for languages, you should use the i18n plugin . 要获得语言支持,您应该使用i18n插件 Or something that provides similar functionality. 或者提供类似功能的东西。 There are a lot of questions on SO on how to use it which you can look at if you run into trouble setting up i18n. 关于如何使用它有很多问题,如果你在设置i18n时遇到麻烦,你可以看一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM