*MooTools代码中的jQuery*有多邪恶?

nee*_*zer 5 jquery mootools

我生活和呼吸jQuery,但我也喜欢找到一个出色的插件来完成我想要的工作.

我的Rails应用程序进行编码jQuery的,但对于文件上传,我还没有找到任何优于FancyUpload(优于恕我直言UploadifySWFUpload的).尝试了所有三个库后,FancyUpload是最好的集成到我的应用程序中.

但是,FancyUpload基于MooTools,加载这两个库(更不用说使用它们)开始有点令人头疼.首先,我只在使用上传功能的页面上加载MooTools; 所有其他页面都使用jQuery.其次,我不得不手动命名我的许多jQuery函数,这有点烦人.

但也许这种设置最麻烦的特点是我不知道MooTools.因为我已经能够使用jQuery完成其他所有工作,所以我从不费心去学习.既然我强迫自己使用这个FancyUpload库(我喜欢并希望继续使用),我就面临着MooTools的无能.

具体来说,这是我onFileSuccess的FancyUpload功能:

onFileSuccess: function(file, response) {
    var json = new Hash(JSON.decode(response, true) || {});

    if (json.get('status') == '1') {

      // success
        file.element.addClass('file-success');
        (function ($, elem, queue_item) {
          $('#images').append($(elem).hide().fadeIn('slow'));
          $(queue_item).fadeOut('slow', function () { $(this).remove(); });
        })(jQuery, json.get('data'), file.element);

    } else {

      // failure
        file.element.addClass('file-failed');

    }
}
Run Code Online (Sandbox Code Playgroud)

正如您将注意到的,我在MooTools函数中间有一个jQuery函数.

我的问题是:这是一件非常糟糕的事吗?我的功能正如我所希望的那样工作,但我不知道我是否会通过这样做来为未来的灾难而滚雪球.

如果这真的是一个坏主意,有人可以给我一个关于MooTools等效代码的指针吗?

我很感激任何见解或帮助.

gon*_*uki 0

这通常不是问题,但我建议不要在任何功能中混合特定于框架的代码,除非您使用的框架不提供您需要的功能。这样您就可以保持可移植性,并且只需要一个需求,而不需要 2 个不同库的 2 个特定版本。

你的代码应该是这样的:

onFileSuccess: function(file, response) {
    var json = new Hash(JSON.decode(response, true) || {});

    if (json.get('status') == '1') {
      // success
        file.element.addClass('file-success');
        document.id('images').adopt(document.id(json.get('data')).fade('hide').fade('in'));
        document.id(file.element).set('tween', {
          onComplete: function() { this.element.dispose();}
        }).fade('out');
    } else {
      // failure
        file.element.addClass('file-failed');
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:刚刚注意到迪米塔尔做了一些类似的答复。我的解决方案的工作原理完全相同,但正确地避免使用混合框架时特别需要的 $ 函数。