无法从Rally wsapidatastore访问数据

时间:2013-01-31 02:06:00

标签: javascript rally

当我创建WsapiDataStore时,store.data.items和store.data.keys返回空数组,虽然我在执行console.log(store.data)时能够看到键和项目

store = Ext.create('Rally.data.WsapiDataStore', {
    model: 'Defect',
    context: {
        project: '/project/xxxxxx'
    },
    autoLoad: true,
    fetch: ['Rank', 'FormattedID', 'Name']
});

console.log(store.data)的输出:

constructor {items: Array[0], map: Object, keys: Array[0], length: 0, allowFunctions:   false…}
    allowFunctions: false
    events: Object
    generation: 8
    getKey: function (record) {
    hasListeners: HasListeners
    items: Array[7]
    keys: Array[7]
    length: 7
    map: Object
    sorters: constructor
    __proto__: TemplateClass

注意第一行如何表示“items:Array [0]”和“keys:Array [0]”,但在展开时它会显示“items:Array [7]”和“keys:Array [7]”。当我进一步扩展时,我也能看到7条记录。

当我添加加载侦听器并从侦听器函数访问数据时,所有内容都按预期工作(但我不想这样做)

1 个答案:

答案 0 :(得分:0)

我认为最好的方法是通过两个Rally.data.WsapiDataStore来处理数据。您需要将两个存储的侦听器链接在一起才能正确处理异步负载。以下是一个说明该过程的示例:

<!DOCTYPE html>
<html>
<head>
    <title>MultipleModelExample</title>

    <script type="text/javascript" src="https://rally1.rallydev.com/apps/2.0p5/sdk.js"></script>

    <script type="text/javascript">
        Rally.onReady(function() {
            Ext.define('CustomApp', {
                extend: 'Rally.app.App',
                componentCls: 'app',

                // Combined Story/Defect Records
                dataRecords: null,

                launch: function() {
                    //Write app code here

                    this.dataRecords = [];

                    Rally.data.ModelFactory.getModels({
                        types: ['HierarchicalRequirement','Defect'],
                        scope: this,
                        success: function(models) {
                            this.myModels = models;

                            this.storyStore = Ext.create('Rally.data.WsapiDataStore', {
                                model: models.HierarchicalRequirement,
                                fetch: true,
                                autoLoad: true,
                                remoteSort: true,
                                sorters: [
                                    { property: 'FormattedID', direction: 'Asc' }
                                ],
                                listeners: {
                                    load: this._processStories,
                                    scope: this
                                }
                            });
                        }
                    });
                },

                _processStories: function(store, records, successful, opts) {

                    var storyRecords = [];

                    Ext.Array.each(records, function(record) {
                        //Perform custom actions with the data here
                        //Calculations, etc.
                        storyRecords.push({
                            FormattedID: record.get('FormattedID'),
                            Name: record.get('Name'),
                            Description: record.get('Description')
                        });
                    });

                    this.dataRecords = storyRecords;

                    this.defectStore = Ext.create('Rally.data.WsapiDataStore', {
                        model: this.myModels.Defect,
                        fetch: true,
                        autoLoad: true,
                        remoteSort: true,
                        sorters: [
                            { property: 'FormattedID', direction: 'Asc' }
                        ],
                        listeners: {
                            load: this._processDefects,
                            scope: this
                        }
                    });
                },

                _processDefects: function(store, records, successful, opts) {

                    var defectRecords = [];

                    Ext.Array.each(records, function(record) {
                        //Perform custom actions with the data here
                        //Calculations, etc.
                        defectRecords.push({
                            FormattedID: record.get('FormattedID'),
                            Name: record.get('Name'),
                            Description: record.get('Description')
                        });
                    });

                    var combinedRecords = defectRecords.concat(this.dataRecords);

                    this.add({
                        xtype: 'rallygrid',
                        store: Ext.create('Rally.data.custom.Store', {
                            data: combinedRecords,
                            pageSize: 25
                        }),
                        columnCfgs: [
                            {
                                text: 'FormattedID', dataIndex: 'FormattedID'
                            },
                            {
                                text: 'Name', dataIndex: 'Name', flex: 1
                            },
                            {
                                text: 'Description', dataIndex: 'Description', flex: 1
                            }
                        ]
                    });
                }

            });

            Rally.launchApp('CustomApp', {
                name: 'MultipleModelExample'
            });
        });
    </script>

    <style type="text/css">
        .app {
             /* Add app styles here */
        }
    </style>
</head>
<body></body>
</html>