【问题标题】:Refactoring Crossrider extension code重构 Crossrider 扩展代码
【发布时间】:2014-08-17 13:21:55
【问题描述】:

我使用Crossrider 构建了我的扩展,目前我的所有代码都在 extension.js 文件中。但是,在这个单一文件中它变得相当长并且变得越来越难以维护。有没有办法将我的代码拆分成单独的文件并仍然在我的扩展程序中使用它们?

例如,如果我的 extension.js 文件的结构类似于以下示例,我希望函数 f1 和 f2 位于可以加载到扩展程序中的单独文件中:

appAPI.ready(function($) {
  init();
  f1();
  f2();
  ...

  function init() {
    // init code
    ...
  }
  function f1() {
    //hundreds of lines of code
  }
  function f2() {
    //hundreds of lines of code
  }
  ...
});

【问题讨论】:

    标签: javascript cross-browser include browser-extension crossrider


    【解决方案1】:

    您可以在一个或多个资源文件(例如 init.jsfunctions.js)中定义函数,然后将其包含在 appAPI.ready 中。有关资源的更多信息,请参阅appAPI.resources

    因此,使用您的示例,您的代码将类似于:

    extension.js

    appAPI.ready(function($) {
      appAPI.resources.includeJS('init.js');
      appAPI.resources.includeJS('functions.js');
      init();
      f1();
      f2();
      ...
    });
    

    init.js

    function init() {
      // init code
      ...
    }
    

    functions.js

    function f1() {
      //hundreds of lines of code
    }
    function f2() {
      //hundreds of lines of code
    }
    

    [披露:我是 Crossrider 的员工]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-12
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多