我一直在为 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 的方式。