knockoutjs数据绑定不与esri脚本一起使用

时间:2012-07-13 07:46:46

标签: knockout.js esri

我是KnockOutJS的新手。我正在尝试使用在线示例,它工作正常并显示预期的结果,如果我不从html调用以下脚本。但我需要使用此脚本稍后在浏览器上显示esri地图。如果我取消注释下面的脚本,它将无法正常工作。 如果有人帮我解决问题,我将不胜感激。

<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?   v=3.0"/> 

这是我的代码

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
<title>Imagery</title>
<link rel="stylesheet" type="text/css"     href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.0/js/dojo/dijit/themes/claro/claro.c     ss">
 <style type="text/css">
  html, body { height: 100%; width: 100%; margin: 0; padding: 0; }
  #map{padding:0;}
</style>
</head>
<body>
<p data-bind="text: firstName"></p>
<p data-bind="text: lastName"></p>

<input type="text" data-bind="value: firstName" />
<input type="text" data-bind="value: lastName" />
<ul data-bind="foreach: items">
    <li><span data-bind="text:firstName"></span></li>
    <li><span data-bind="text:lastName"></span></li>
</ul>
  <ul data-bind="foreach: items">
    <li data-bind="text:name()"></li>

</ul>
</body>
 <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?     v=3.0"></script> <!--if I comment this line, it works-->
<script type="text/javascript" src="Scripts/knockout-2.1.0.js"></script>
<script type="text/javascript">
   var MyViewModel = function () {
       var self = this;

       self.firstName = ko.observable("Sravan");
       self.lastName = ko.observable("Kumar");

       self.items = ko.observableArray([
           { firstName: "A", lastName: "B", name: ko.computed = function () { return "Hello"; }},
           { firstName: "AA", lastName: "BB", name: ko.computed = function () { return "World"; } }
       ])
    };
   var viewModel = new MyViewModel();
   ko.applyBindings(viewModel);
 </script>

</html>

1 个答案:

答案 0 :(得分:1)

如果您恢复包含脚本的顺序,则代码可以正常运行:

<script type="text/javascript" src="Scripts/knockout-2.1.0.js"></script>
<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?     v=3.0"></script> <!--if I comment this line, it works-->

问题是esri脚本抛出XMLHttpRequestException错误,导致页面其余部分出现问题。

现在您恢复订单,ko绑定将起作用,但Chrome将继续抛出XMLHttpRequestException。问题可能是:您正在运行文件系统中的代码而不是本地Web服务器:

http://forums.arcgis.com/threads/61584-Sample-scripts-no-longer-work-since-upgrade-to-v3.0-of-the-API