Llu*_*nez 5 javascript memento undo-redo
我正在寻找在CRUD表单中使用的memento模式(GoF)的JavaScript实现.在基本级别上,撤消对输入的更改就足够了,但将它与标准JS框架(如YUI或Ext)一起使用来撤消和重做网格操作(新行,删除行等)会很棒.
谢谢
因为我没有看到任何代码示例,所以这里是一个快速'n脏实现撤消EXT表单:
var FormChangeHistory = function(){
this.commands = [];
this.index=-1;
}
FormChangeHistory.prototype.add = function(field, newValue, oldValue){
//remove after current
if (this.index > -1 ) {
this.commands = this.commands.slice(0,this.index+1)
} else {
this.commands = []
}
//add the new command
this.commands.push({
field:field,
before:oldValue,
after:newValue
})
++this.index
}
FormChangeHistory.prototype.undo = function(){
if (this.index == -1) return;
var c = this.commands[this.index];
c.field.setValue(c.before);
--this.index
}
FormChangeHistory.prototype.redo = function(){
if (this.index +1 == this.commands.length) return;
++this.index
var c = this.commands[this.index];
c.field.setValue(c.after);
}
Ext.onReady(function(){
new Ext.Viewport({
layout:"fit",
items:[{
xtype:"form",
id:"test_form",
frame:true,
changeHistory:new FormChangeHistory("test_form"),
defaults:{
listeners:{
change:function( field, newValue, oldValue){
var form = Ext.getCmp("test_form")
form.changeHistory.add(field, newValue, oldValue)
}
}
},
items:[{
fieldLabel:"type some stuff",
xtype:"textfield"
},{
fieldLabel:"then click in here",
xtype:"textfield"
}],
buttons:[{
text:"Undo",
handler:function(){
var form = Ext.getCmp("test_form")
form.changeHistory.undo();
}
},{
text:"Redo",
handler:function(){
var form = Ext.getCmp("test_form")
form.changeHistory.redo();
}
}]
}]
})
});
Run Code Online (Sandbox Code Playgroud)
为可编辑网格实现这一点有点棘手,但是您应该能够创建一个GridChangeHistory来执行相同的操作,然后从EditorGrid的AfterEdit侦听器调用add()函数.
"之前"和"之后"属性可以是回调函数,允许您撤消/重做任何类型的命令,但在调用add()时需要更多工作
| 归档时间: |
|
| 查看次数: |
3817 次 |
| 最近记录: |