Reputation: 1053
I need to use a library in my javascript file for one of my pages. I found that I could use:
define(["plugins/http", "someLibrary"], function (http, foo) {
/* code: never using "foo", but using methods from someLibrary */
}
Originally, I expected a value for foo, since all the other examples (like http) had values for the parameters, but that apparently isn't how someLibrary works. Having foo as a parameter seemed useless, so I removed it to see if it still worked, and it did.
Is it alright to leave the parameter off? What is the general consensus for Durandal/Require.js best practice in this case? Neither option seems particularly elegant. Is there some better way to accomplish the same goal?
Upvotes: 0
Views: 109
Reputation: 5414
You would only need an assigned variable if the plugin returns a references (like the plugins\http
case).
Some plugin attaches itself to other objects or plugins, adding functions or properties to other objects, therefore it doesn't return any references (like the someLibrary
in your example). So you don't need a variable for that.
My personal practices are order the sequence of the plugins in define(...)
according to whether they need a assigned variable or not, plugins that do not need variable, will be define later, those that need variable will be defined first. This is to align with javascript's way of function
definition where you always put the optional parameters to the right.
Upvotes: 1
Reputation: 12295
Yes. There are various libraries that set up behaviors, like bootstrap, and these can be included in the require statement but left off of the function arguments. You can also write the following require.js shorthand:
var http = require('plugins/http');
require('somelibrary');
function viewModel() {
...
}
Upvotes: 1