When you want to include a third-party JavaScript library in your project, the best choice is to install the library from npm and use it through the MontageJS module loader. In cases where the library isn’t available from npm, you can just add a script tag to your project's base index.html file.
If you are using Montage Studio to work on your project, you can use the following steps to add Moment.js to your project:
1) Click the package.json in the project pane, which will open it in the editor
2) Type “moment” in the search field and hit enter
3) Click the “install” button for the Moment package
4) Save the package.json file so that the change will take effect
If you are not using Montage Studio, you can do the equivalent of the previous steps at the command line:
$ npm install moment —save
After you have added Moment.js to your project, you can use it via the module loader. Say that you have a component with a JavaScript file where you want to use the library… just put this line near the other require statements at the top of the file:
var moment = require("moment”);
After you have done that, you can use the “moment” variable anywhere in your script to reference the module. For example:
exports.Main = Component.specialize(/** @lends Main# */ {
templateDidLoad: {
value: function() {
var day = moment("Dec 25, 1995");
console.log("Test:", day);
}
}
}
Regarding your second question, about XHR requests, I’ve been really happy with a library called Reqwest:
https://github.com/ded/reqwest You can install it in your project through npm, following the same instructions that I provided above for Moment. Once you have added Reqwest to your project, you can do things like this: