views:

446

answers:

2

I am trying to get working a simple (noob) examle of Combo loaded with data from Xml file. Here is my xml:

<?xml version="1.0" encoding="UTF-8"?>
  <accounts>
 <account> 
  <name>Savings Account</name>
  <id>1</id>
 </account>
<account> 
  <name>Current Account</name>
  <id>2</id>
 </account>
</accounts>

When I configure and add XmlStore, it reports 2 records found.

Here is the code for the XmlStore:

cteo = Ext.extend(Ext.data.XmlStore, {
constructor: function(cfg) {
    cfg = cfg || {};
    cteo.superclass.constructor.call(this, Ext.apply({
        storeId: 'cteo',
        url: 'cteo.xml',
        record: 'account',
        data: '',
        fields: [
            {
                name: 'name',
                mapping: 'name'
            },
            {
                name: 'id',
                mapping: 'name'
            }
        ]
    }, cfg));
    }
 });
new cteo();

finally, this is the code for the combo:

MyPanelUi = Ext.extend(Ext.Panel, {
title: 'My Panel',
width: 400,
height: 250,
initComponent: function() {
    this.items = [
        {
            xtype: 'label',
            text: 'Cuenta Origen'
        },
        {
            xtype: 'combo',
            store: 'cteo',
            displayField: 'name',
            valueField: 'id'
        }
    ];
    MyPanelUi.superclass.initComponent.call(this);
    }
});

It must be something simple, but I am stuck...

A: 

This will not do anything:

store: 'cteo',

You need to pass in the object reference that you assigned earlier, not a string:

store: cteo,

Alternately, you could call Ext.StoreMgr.lookup('cteo'), but judging by your code I assume that the variable reference was your intention.

One comment on your code. Doing this:

cteo = Ext.extend(Ext.data.XmlStore, {
...
cteo();

...is a bit strange, and is most likely creating a global variable in the window scope (assuming that cteo is not defined as a var somewhere earlier). Think of it as defining a custom class, then creating a new instance of the class you defined. Also, think about your naming -- a store subclass should be a specific type of store, which should be evident in the name. Typically, your code should look more like this:

Ext.ns('MyNamespace');

MyNamespace.CteoStore = Ext.extend(Ext.data.XmlStore, {
...
});
var cteoStore = new CteoStore();

Oh yeah, one other thing. You do not need to override the constructor for the sole purpose of providing default configs. Just do this:

MyNamespace.CteoStore = Ext.extend(Ext.data.XmlStore, {
    storeId: 'cteo',
    url: 'cteo.xml',
    record: 'account',
    data: '',
    fields: [
        {
            name: 'name',
            mapping: 'name'
        },
        {
            name: 'id',
            mapping: 'name'
        }
    ]
});

This is also more useful since these configs are overrideable, unlike in your example. This makes it more reusable (like if you ever wanted to assign a different id to another instance).

bmoeskau
A: 

Check out this thread at sencha site:

http://www.sencha.com/forum/showthread.php?105818-(noob)-Populating-combo-from-XmlStore-with-Ext-js-designer

Dan
Fine, but my comments still stand if you care.
bmoeskau