1

如何在 Dojo 1.8 API 中定义 XML 存储

在旧 API 中,我们定义它是这样的

var store = new dojox.data.XmlStore({url: "books.xml", rootItem: "book"});
var gotBooks = function(items, request){
  for(var i = 0; i < items.length; i++){
    var item = items[i];
    console.log("Located book: " + store.getValue(item, "title");
  }
}
var request = store.fetch({query: {isbn:"A9B57*"}, onComplete: gotBooks});
4

1 回答 1

2

这是books.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<books>
  <book>
      <isbn>1</isbn>
      <title>Title of 1</title>
      <author>Author of 1</author>
  </book>
  <book>
      <isbn>2</isbn>
      <title>Title of 2</title>
      <author>Author of 2</author>
  </book>
  <book>
      <isbn>3</isbn>
      <title>Title of 3</title>
      <author>Author of 3</author>
  </book>
  <book>
      <isbn>4</isbn>
      <title>Title of 4</title>
      <author>Author of 4</author>
  </book>
  <book>
      <isbn>5</isbn>
      <title>Title of 5</title>
      <author>Author of 5</author>
  </book>
  <book>
      <isbn>6</isbn>
      <title>Title of 6</title>
      <author>Author of 6</author>
  </book>
</books>

您可以使用以下代码查看这个新 API:

<script type="text/javascript" src="web/dojo/dojo.js" data-dojo-config="async: true"></script>

<script>
 require(["dojox/data/XmlStore"],
          function(XmlStore){
      var store = new XmlStore({url: "http://localhost:8080/DojoProject/book.xml", rootItem: "book"});
      var gotBooks = function(items, request){
        for(var i = 0; i < items.length; i++){
          var item = items[i];
          console.log("Located book: " + store.getValue(item, "title"));
        }
      }
      var request = store.fetch({query: {isbn:"1"}, onComplete: gotBooks});

          });
</script>
于 2013-01-26T10:24:02.030 回答