如何在browserify项目中加载jQuery mobile?

时间:2015-03-11 01:27:39

标签: jquery jquery-mobile gulp browserify browserify-shim

我试图在我的browserify项目中加载jquery-mobile并收到错误,因为this引用了一个空对象而不是window对象。 Browserify将jquery-mobile包装在其常用的包装器中(function(require, module, exports){ ... }),但this不在window的范围内。

的package.json

{
  "dependencies": {
    "animate.css": "^3.1.1",
    "backbone": "^1.1.2",
    "backbone-query-parameters": "git://github.com/jhudson8/backbone-query-parameters",
    "backbone.marionette": "^2.3.1",
    "backbone.radio": "^0.6.0",
    "backbone.storage": "^0.1.0",
    "backbone.syphon": "^0.5.0",
    "bootstrap": "^3.3.1",
    "browserify-swap": "^0.2.1",
    "handlebars": "^1.3.0",
    "jquery": "^2.1.3",
    "jquery-mobile": "^1.4.1",
    "lodash": "^2.4.1",
    "nprogress": "^0.1.6"
  },
  "browser": {
    "bootstrap": "./node_modules/bootstrap/dist/js/bootstrap.js",
    "jquery-mobile": "./node_modules/jquery-mobile/dist/jquery.mobile.js"
  },
  "browserify-shim": {
    "bootstrap": {
      "depends": [
        "jquery:jQuery"
      ]
    },
    "jquery": "$",
    "jquery-mobile" : { "exports": "$.mobile", "depends": [ "jquery:$" ] }
  },
  "browserify-swap": {
    "@packages": [
      "underscore"
    ],
    "dist": {
      "underscore.js$": "lodash"
    }
  },
  "browserify": {
    "transform": [
      "babelify",
      [
        "hbsfy",
        {
          "extensions": [
            "hbs"
          ]
        }
      ],
      "browserify-swap",
      "browserify-shim"
    ]
  }
}

错误消息

  

jquery.mobile.js:14931 未捕获的TypeError:无法读取属性' jQuery'未定义的

然后我找到了moduleify

我在谷歌搜索后找到了moduleify并试图在我的 package.json 中使用它来获取jquery-mobile,但是我收到了一个错误。这是我使用moduleify更新的转换配置:

  "browserify": {
    "transform": [
      "babelify",
      [
        "hbsfy",
        {
          "extensions": [
            "hbs"
          ]
        }
      ],
      [
        "moduleify",
        [
          "jquery.mobile"
        ]
      ],
      "browserify-swap",
      "browserify-shim"
    ]
  }

这样做会给我带来moduleify的错误:

  

moduleify / index.js:9   var rules = Array.isArray(别名)?别名:Object.keys(别名).map(fu

任何人都有关于如何this参考window的任何建议?我觉得我在正确的轨道上,但不知道下一步该怎么做。谢谢!

1 个答案:

答案 0 :(得分:0)

在第一种方法中:

"jquery-mobile" : { "exports": "$.mobile", "depends": [ "jquery:$" ] }

我认为你需要这样做:

"jquery-mobile" : { "exports": "$.mobile", "depends": [ "jquery:jQuery" ] }