0

我创建了一个简单的应用程序,它从 json 商店加载数据并将其绘制在屏幕上。我需要做的是添加到客户端的商店(我不控制源数据)。因此,无论是在 store load:funciton() 还是在 MainController 中,如果还不算太晚,我想在开头插入几行,这样这些记录就会出现John、Paul、George 和 Ringo 之前。我已经包含了我所有的 Javascript,所以代码是完整的。

在此处输入图像描述

员工模型:

Ext.define('Sencha.model.Employee', {
    extend: 'Ext.data.Model',

    config: {
        fields: [
            {name: 'firstName', type: 'string'},
            {name: 'lastName', type: 'string'},
            {
                name: 'fullName',
                type: 'string',
                convert: function (value, record) {

                    firstName = record.data.firstName;
                    lastName = record.data.lastName;
                    fullName = firstName + " " + lastName;
                    return fullName;
                }
            }
        ]
    },
    load: function () {
        console.log("Employee model");
        this.callParent(arguments);
    }
});

员工商店:

Ext.define('Sencha.store.EmployeeStore', {
    extend: 'Ext.data.Store',

    requires: [],

    config: {
        model: 'Sencha.model.Employee',
        autoLoad: true,
        defaultRootProperty: 'items',

        proxy: {
            type: 'ajax',
            url: 'employees.json',
            reader: {
                type: 'json',
                rootProperty: 'items'
            }
        }
    },

    load: function () {
        console.log("EmployeeStore");

        this.callParent(arguments);
    }
});

控制器:(尝试获取存储并打印值)

Ext.define('Sencha.controller.MainController', {
    extend: 'Ext.app.Controller',

    requires: [],

    config: {

    },
    launch: function () {
        console.log("Main Controller...");
        var empStore = Ext.getStore('EmployeeStore');
        console.log(empStore.length);
        empStore.each(function (val) {  // Not working
            var firstName = val.get('firstName');
            console.log(firstName);
        });


    }
});

应用程序.js:

Ext.Loader.setConfig({enabled: true});

Ext.application({
    name: "Sencha",

    models: ['Employee'],
    stores: ['EmployeeStore'],
    views: [],
    controllers: ['MainController'],

    launch: function () {
    console.log("Launching");

    var aList = Ext.create("Ext.List", {
        fullscreen: true,
        store: 'EmployeeStore',
        itemTpl: "{lastName}, {firstName} - {fullName}"
    });
        Ext.Viewport.add(aList);
    }
});

员工.json:

{"items": [
    { "firstName": "John", "lastName": "Lennon" },
    { "firstName": "Paul", "lastName": "McCartney" },
    { "firstName": "George", "lastName": "Harrison" },
    { "firstName": "Ringo", "lastName": "Starr" }
]}
4

1 回答 1

3

离开autoload您的商店,在您的启动函数中加载商店,并使用回调添加您的记录:

launch: function () {
    var empStore = Ext.getStore('EmployeeStore');
    empStore.load(function() {
        empStore.insert(0, [ /*records to insert*/ ]);
    });
}
于 2013-08-12T18:14:47.040 回答