RequireJS + Waypoints : Object [object Object] has no method 'waypoint'

Deadly 提交于 2019-12-12 12:01:12

问题


I can't use waypoints with RequireJS although everything looks good. Here is my code: http://jsfiddle.net/7nGzj/

main.js

requirejs.config({
    "baseUrl": "theme/PereOlive/js/lib",
    "paths": {
      "app":"../app",
      "jquery": "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min"
    },
    "shim": {
        "waypoints.min": ["jquery"]
    }
});
requirejs(["app/common"]);

common.js

define([
    'jquery',
    "waypoints.min",
], function () {
    $(function () {
        console.log($.waypoints);
        $('#loading').waypoint(function (direction) {
            // do stuff
        });
    });
});

I even shimed it to be sure jQuery is properly loaded but it doesn't work. My other libraries works like they should (responsiveslides, flexslider, hoverintent, smoothscroll, ..).

  • jQuery V1.10.2
  • Waypoints V2.0.3
  • RequireJS V2.1.8

回答1:


Dependencies which are AMD-compliant (and Waypoints is) should be required via their registered module name. The easiest (only?) way to find out what that name is is to look into the lib's source code:

// (...)
if (typeof define === 'function' && define.amd) {
  return define('waypoints', ['jquery'], function($) {
    return factory($, root);
  });
} // (...)

It's "waypoints"!

Since the library is AMD-compatibile, you don't need a shim but you'll need to define the path to it (since the file name can be different than the AMD module name):

requirejs.config({
    "baseUrl": "theme/PereOlive/js/lib",
    "paths": {
      "app": "../app",
      "waypoints": "path/to/waypoints.min"
    }
});

After doing that, you'll need to change you require call:

define([
    "jquery",
    "waypoints" // this is the AMD module name
]

Fixed your jsfiddle: http://jsfiddle.net/jVdSk/2/



来源:https://stackoverflow.com/questions/19321127/requirejs-waypoints-object-object-object-has-no-method-waypoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!