简体   繁体   English

Extjs将xmlfile属性加载到存储中

[英]Extjs load xmlfile attributes into store

i am trying to load attribute values into an extjs store with the following code: 我正在尝试使用以下代码将属性值加载到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'
            }
        }
    });

but the store always shows up empty. 但商店始终显示为空。 Heres a blanked out excerpt from the xml file: 这是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>

Im very new to reading XML files to ExtJS, so it would be great if one of you could show me the proper way to map those attributes. 对于将XML文件读取到ExtJS来说是非常新的,所以如果你们中的一个能向我展示映射这些属性的正确方法,那将是很棒的。

There's nothing wrong with your XML file. 您的XML文件没有任何问题。 Your problem is that ExtJS stores don't Load data on automatically on creation by default. 您的问题是,默认情况下,ExtJS存储不会在创建时自动加载数据。 You have to either enable the autoLoad or load the Store manually with the load function: 您必须启用自动加载或使用加载功能手动加载商店:

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!!');
        }
    });

});

Remember that the load is asynchronous and you have to wait until the load is done to be able to use the data! 请记住,加载是异步的,您必须等到加载完成后才能使用数据!

I recommend that you check the API doc too see all that you can do with the load function. 我建议您也检查API文档,以查看可以使用load函数执行的所有操作。

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

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