2

我一直在为 QuickBase 编写一个自定义脚本,它需要一些日期操作。我决定使用moments.js,因为我在quickbase 中使用它,所以我使用$.getscript 动态加载moments.js。我之前用其他插件(jquery.md5.min.js)完成了这个过程,这工作正常。

问题是,即使正确读取了 moment.js,当我尝试使用它时(例如,请参阅我为调试添加的 console.log(),我收到一条错误消息,告诉我“时刻未定义”。

I know moment.js is being read because I already had it dumped into the console after loading, and I'm trying to use its functions only after it is loaded via the asynchronous methods. I have also played with a few simple jsfiddles just to make sure I was calling it correctly. I also checked several times the url and it is correct. The rest of my code is also correct (if I comment out the lines with moment(), it works as expected).

$.getScript(domain + dbid_app + urlMoments)
  .done(function(script, textStatus) {
    rightNow = moment();
    dfd.resolve();  // Resolve when the script is finished loading.
    console.log("moments.js has finished loading.\n\n" + textStatus);
    console.log(rightNow);
  })
  .fail(function( jqxhr, settings, exception ) {
    console.log( "Triggered ajaxError handler." );
});

How do I call the moment() function? When running the fiddle, and after looking at many online examples, it is as simple as: var myTime = moment();, but this is not working inside of my script. Is there a chance that, even after loading moments.js with $.getscript(), it was not executed? From what I can see on the source code of moments.js, the factory function should be automatically called, shouldn't it?

(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.moment = factory()

}

最后一件事:url 指向我用moments.js 创建的QuickBase 代码页。这也是我毫无问题地使用 jquery.md5.js 的方式。

4

1 回答 1

0

看起来您的问题可能是由 Quickbase 已经加载了不同版本的 moment.js 引起的。具体来说,Quickbase 表单在一定程度上使用Moment 2.8.4,并且正在使用RequireJS加载。按照 Moment 使用 RequireJS 的示例,我能够使用以下代码获得一个 moment 对象:

require(['moment'], function(){
    moment();
});

rightNow = moment();

我希望这可以帮助您解决问题。

于 2015-10-13T21:21:24.097 回答