如何使用拉力赛sdk 2.1在自定义拉力赛应用程序中维护对话框组件的状态?

时间:2016-09-30 18:45:50

标签: rally

所以我正在玩拉力赛sdk来为拉力赛建立自定义应用程序,而我正试图做一些待办事项列表。我正在使用集会文本字段和集会对话框来实现它。它还没有完美的功能,但我想要做的是保持我添加的对话框的状态,这样当我刷新应用程序时它们仍然存在。我还没想到它,而且我似乎无法通过阅读关于维持状态的拉力指南来获得它。

这是我到目前为止的代码:

Ext.define('CustomApp', {
    extend: 'Rally.app.App',
    stateful: true,
    componentCls: 'app',

    getState: function() {
        return {
            dialogs: this.dialogs
        };
    },

    applyState: function(state) {
        this.dialogs = state.dialogs;
    },

    doLayout: function() {
        var me = this;
        var textField = Ext.create('Rally.ui.TextField', {
            fieldLabel: 'Add task:',
            listeners: {
                scope: me, 
                specialkey: me._checkEnter
            }
        });

        me.add(textField);
    },

    launch: function() {
        this.doLayout();
        this.taskCounter = 0;
        this.yPosition = 50;
        this.dialogs = [];
    },

    _checkEnter: function(field, event) {
        if (event.getKey() == event.ENTER) {
            this._onEnter(field);
        }
    },

    _onEnter: function(field) {
        this.taskCounter = this.taskCounter + 1;
        var dialog = Ext.create(Rally.ui.dialog.Dialog, {
            context: this.getContext(),
            id: (99990 + this.taskCounter).toString(),
            autoShow: true,
            autoCenter: false,
            draggable: false,
            closable: true,
            modal: false,
            width: 300,
            x: 100,
            y: this.yPosition,
            title: 'Task ' + this.taskCounter,
            items: {
                xtype: 'component',
                html: field.getRawValue(),
                padding: 10
            }
        })

        this.yPosition = this.yPosition + 100;
    }
});

**** **** EDIT

所以我一直在尝试做这项工作。我一直试图在页面刷新和导航之间保持状态,并开始通过使用数组存储不同的字符串来测试它,并看看它们在返回应用程序时是否仍然存在。每次我在字段框中输入内容,然后按回车键,就会创建一个对话框,我调用saveState(),并尝试保存带有创建对话框ID的字符串。但是,似乎getState函数仅在第一次调用时保存状态(在第一次输入时),因为当我刷新或返回应用程序时,唯一保留的字符串是创建的第一个对话框的id ,但不是以下的。这是来自SDK的saveState函数的错误,还是我使用它错了?谢谢。这是代码:

Ext.define('CustomApp', {
extend: 'Rally.app.App',
stateful: true,
componentCls: 'app',

getState: function() {
    console.log('getting state');

    if(this.dialogs.length > 0) {
        console.log('this.dialogs from getState:', this.dialogs);
        var newState = {
            currentDialogs: this.dialogs
        };
        console.log(newState);
        return newState;
    }
    return null;
},

applyState: function(state) {
    console.log('applying state');
    console.log('current state:', state);

    if(state.currentDialogs) {
        this.dialogs = state.currentDialogs;    
        console.log('this.dialogs after applying state:', this.dialogs);
    }
    else {
        this.dialogs = [];
    }
},

doLayout: function() {
    var me = this;
    var textField = Ext.create('Rally.ui.TextField', {
        fieldLabel: 'Add task:',
        listeners: {
            scope: me, 
            specialkey: me._checkEnter
        }
    });

    me.add(textField);
},

launch: function() {
    this.dialogs = this.dialogs;
    this.doLayout();
    this.taskCounter = 0;
    this.yPosition = 50;
},

_checkEnter: function(field, event) {
    if (event.getKey() === event.ENTER) {
        this._onEnter(field);
    }
},

_onEnter: function(field) {
    this.taskCounter = this.taskCounter + 1;
    var dialog = Ext.create(Rally.ui.dialog.Dialog, {
        context: this.getContext(),
        autoShow: true,
        autoCenter: false,
        draggable: false,
        closable: true,
        modal: false,
        width: 300,
        x: 100,
        y: this.yPosition,
        title: 'Task ' + this.taskCounter,
        items: {
            xtype: 'component',
            html: field.getRawValue(),
            padding: 10
        }
    });

    this.yPosition = this.yPosition + 100;
    this.dialogs.push((99990 + this.taskCounter).toString());
    this.saveState();
}
});

1 个答案:

答案 0 :(得分:1)

这是一个令人头疼的问题。事实证明,如果状态层认为值没有改变,那么在状态层中存在一些优化。因为在你的getState方法中,你返回的是由app使用和修改的同一个对话框数组实例,它总是认为值是最新的,所以不会重新保存它。

因此,如果你只是调整你的getState以在每次你应该好的时候返回一个新的数组:

 var newState = {
     currentDialogs: [].concat(this.dialogs)
 };

每次添加任务后,您都会看到一个更新首选项的呼叫。

然后,您只需在启动方法中添加代码即可查看当前状态并重新添加已添加的任务。

_.each(this.dialogs, function(dialog) {
    this._addDialog(dialog.title, dialog.html); //todo: implement
}, this);