mootools
We all know that do/undo functionality is a God send for word processing apps. I've used those terms so often that I think of JavaScript actions in terms of "do" an "undo." I've put together a proof of concept Do/Undo class with MooTools.
我们都知道,做/撤消功能是文字处理应用程序的上帝之选。 我经常使用这些术语,以至于我将JavaScript动作理解为“执行”和“撤消”。 我已经用MooTools组合了概念验证Do / Undo类。
MooTools类 (The MooTools Class)
(function($){ this.DoUndo = new Class({ //initialization initialize: function() { this.registry = {}; }, //register something you'd do register: function(key,doo,undo,arguments) { this.registry[key] = { 'doo': doo || $empty, 'undo': undo || $empty, 'args': arguments }; }, //do it! doo: function(key) { return this.registry[key]['doo'](this.registry[key]['args']); }, undo: function(key) { return this.registry[key]['undo'](this.registry[key]['args']); } }); })(document.id);
Once you've created your class instance you need to register your do's/undo's by providing a key, do function, undo function, and your arguments. From then on out you just call doo and undo methods, providing the key of course, to make it happen.
创建类实例后,您需要通过提供键,do函数,undo函数和参数来注册do / undo。 从那时起,您只需调用doo和undo方法即可,当然要提供实现它的关键。
样本用法 (The Sample Usage)
//usagewindow.addEvent('domready',function(){ //instance var dooer = new DoUndo(); //set up an do/undo command dooer.register('toggle-submit',function(submit) { submit.fade('out'); },function(submit) { submit.fade('in'); },$('submit')); //use $('hider').addEvent('click',function() { dooer.doo('toggle-submit'); }); $('shower').addEvent('click',function() { dooer.undo('toggle-submit'); });});
This do/undo does simple show/hide of a button.
执行/撤消操作简单地显示/隐藏按钮。
货物 (The Goods)
- I like the terminology: do and undo, just like every word processing application you use. 我喜欢这样的术语:执行和撤消操作,就像您使用的每个文字处理应用程序一样。
- I like the syntax: short and sweet. 我喜欢这样的语法:简短而甜美。
坏处 (The Bads)
- This was a total "proof of concept" class and I'll admit my execution isn't very refined. 这是一个完全的“概念验证”类,我承认我的执行不是很完善。
- The method names may need to be changed on account of "do" being a reserved word. 由于“ do”是保留字,可能需要更改方法名称。
- This structure only allows for one "undo", so to speak. I wonder if it would enhance this solution to have an internal variable which would keep track of undos. 可以这么说,这种结构只允许一个“撤消”。 我想知道它是否可以增强此解决方案的内部变量来跟踪撤消。
What are your thoughts? Dead end? Good start? I'm a ball of emotions with this. One second I think it's great, the next second I don't know how useful it would be.
你觉得呢?你有没有什么想法? 死路? 好的开始? 我对此很感动。 我认为一秒钟很棒,下一秒钟我不知道它会有多大用处。
翻译自:
mootools