Extjs将xmlfile属性加载到store中

时间:2013-12-13 12:55:48

标签: javascript xml extjs extjs4

我正在尝试使用以下代码将属性值加载到extjs存储中:

       Ext.define('assert', {
        extend: 'Ext.data.Model',
        fields: [
            { name: 'id', mapping: '@id' },
            { name: 'test', mapping: '@test' },
            { name: 'name', mapping: '@name' }
        ]
    });

    var store = new Ext.data.Store({
        model: 'assert',
        proxy: {
            type: 'ajax',
            url : 'App/data/rules.xml',
            reader: {
                type: 'xml',
                model:'assert',
                record: 'assert'
            }
        }
    });

但商店总是空着。 下面是xml文件的摘录:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="****" xmlns:axsl="****" xmlns:sch="*****" xmlns:iso="******"     xmlns:xs="*****" xmlns:rule="******">
   <title>test Schematron</title>
   <pattern name="test Schematron">
       <rule context="*****" name="*****">
           <assert test="*****" diagnostics="*****" name="*****" id="****" comment=""     uval="" source="****"/>
           <assert test="******" diagnostics="****" name="****" id="***" comment="" uval="" source="*****"/>
      </rule>
   </pattern>
</schema>

我非常擅长将XML文件读取到ExtJS,所以如果有人能做到这一点会很棒 告诉我映射这些属性的正确方法。

1 个答案:

答案 0 :(得分:1)

您的XML文件没有任何问题。您的问题是ExtJS存储默认情况下不会在创建时自动加载数据。您必须启用autoLoad或使用加载功能手动加载存储:

Ext.define('assert', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'id', mapping: '@id' },
        { name: 'test', mapping: '@test' },
        { name: 'name', mapping: '@name' }
    ]
});

var store = new Ext.data.Store({
    model: 'assert',        
    autoLoad: true,         // The store will start loading as soon as the framework is ready.
    listeners: {
        load: function(){   // This gets executed when your store is done loading.
            console.log('Loaded!!');
        }  
    },
    proxy: {
        type: 'ajax',
        url : 'data/rules.xml',
        reader: {
            type: 'xml',
            model:'assert',
            record: 'assert'
        }
    }
});

Ext.onReady(function(){

    console.log('Ready!!');

    store.load({   // This works too!
        scope: this,
        callback: function(records, operation, success) {
            console.log('loaded records!!');
        }
    });

});

请记住,加载是异步的,您必须等到加载完成才能使用数据!

我建议您查看API文档,了解使用load函数可以做的所有事情。