I'm using requirejs to bundle my files. I've used p5.js for some time, but I need to add p5.dom now. The problem is I don't know how to do it with my requirejs. I couldn't find any information how to do it with bundling.
I have something like this:
require(['lib/p5js/lib/p5'], function (p5) {
var myp5 = new p5(function (_sketch) {
_sketch.setup = function() {
????.createButton('click me'); // I need to access p5.dom here.
}
});
});
EDIT: When I try to use it like a variable I get undefined.
require(['lib/p5js/lib/p5', 'lib/p5js/lib/addons/lib/p5.dom'], function (p5, p5Dom) {
var myp5 = new p5(function (_sketch) {
_sketch.setup = function() {
p5Dom.createButton('click me'); // p5Dom is undefined.
}
});
});
I used shim to ensure that p5.js is loaded before p5.dom is loaded, but it didn't help:
shim: {
'lib/p5js/lib/addons/p5.dom': {
deps: ['lib/p5js/lib/p5']
}
},
I found a solution. Unfortunately I wasn't able to do it with requirejs, but I simply included script tags before requirejs and it solved the problem. So it looks like this:
<script src="~/lib/p5js/lib/p5.js"></script>
<script src="~/lib/p5js/lib/addons/p5.dom.js"></script>
<script src="~/lib/requirejs/require.js" data-main="/js/scripts/tetromino-client/client.js"></script>
I hoped to do this in requirejs, but I don't understand why it didn't work.
Using jQuery as suggested by Wordpress, I have wrapped my code in an anonymous function so that jQuery will not conflict with other javascript libraries:
(function($) {
// Inside of this function, $() will work as an alias for jQuery()
// and other libraries also using $ will not be accessible under this shortcut
})(jQuery);
The problem is that I want to split my code into two files: 1) main.js and 2) utility.js.
How can the main program (main.js) call functions within the other file (utility.js) when both are encapsulated?
utility.js
(function($) {
function doSomething() {
/* code here */
}
})(jQuery);
main.js
(function($) {
$(document).ready(function(){
doSomething();
}
})(jQuery);
Thanks
You can use to return an object out of this utility.js:
(function($, win) {
win.util = function(){
this.doSomething = function() {
$('pre').append('util.js');
}
};
})(jQuery, window);
(function($, $U) { // <----referred it with $U
$(document).ready(function() {
$U.doSomething();
});
})(jQuery, new util()); //<----pass the util object here.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre></pre>
Actually i like the way to use it in OOJS way. Try creating a constructor and pass a new object.
The simplest solution is to assign all functions in utility.js to some global object. Assuming your code works in the browser you could do something like this:
utility.js
(function($, context) {
context.Utility = {
doSomething: function() {
/* code here */
}
};
})(jQuery, window);
main.js
(function($, Utility) {
$(document).ready(function(){
Utility.doSomething();
}
})(jQuery, Utility);
A more general solution would be to use asynchronous module loading (http://requirejs.org/) or a tool like JSPM to manage modules in your application.
Assume I have a basic Rails app with three resources: authors, blogs, and books. I created the resources with the scaffold command, so each resource has their own JavaScript file: authors.js, blogs.js, and books.js.
I saw a couple different implementations on how we can keep the global namespace clean for our JavaScript, as well as keep our page-specific JavaScript separate from each other so they don't clash.
Here is my devised implementation. It appears to do what I want, but I wanted to get feedback if this would be considered a bad implementation/bad practice for some reason. I would also welcome other implementations if people have a better way.
For all layouts within app/views/layouts specify this for the body tag:
<body class="<%= controller_name %>">
For the JavaScript code: As we know, all JavaScript in Rails is eventually pulled together into one big JavaScript file. For readability purposes here I pulled all the JavaScript from each of the separate JavaScript files and put them together (within the app I would keep the JavaScript code separate within each of their corresponding js files):
var authorsjs = {
init: function(){
alert("hello from authorjs");
// all author bindings specified here
$("#oneEl").on('click', function(){
alert("oneEl was clicked");
});
},
someAuthorFunc: function(){
alert("someAuthorFunc run");
}
};
$(document).on('page:change', function(){
if($('body').hasClass("authors") == true ){
authorsjs.init();
}
});
var booksjs = {
init: function(){
alert("init booksjs run");
// all books bindings specified here
$("#twoEl").on('click', function(){
alert("twoEl was clicked");
});
},
someBookFunc: function(){
alert("someBookFunc run");
}
};
$(document).on('page:change', function(){
if($('body').hasClass("authors") == true ){
authorsjs.init();
}
});
So as expected, these click events will not work for views associated to blogs, the event bound to #oneEl only works on views associated with authors, and the event bound to #twoEl only works on views associated with books.
Answer based on feedback
The following works. I am using the jquery-turbolinks gem. Each of the controller's js is encapsulated in an object. For each page request the body class is checked and the appropriate object's init method is run. I also went ahead and created a someGlobals object which provides common js code across all controllers.
This code works as intended but is quite messy. I wish there was some way to clean up front-end code. If you have any suggestions let me know:
$(document).ready(function(){
var authorsjs = {
init: function(){
alert("hello from authorjs");
// all author bindings specified here
$("#oneEl").on('click', function(){
alert("oneEl was clicked");
});
},
someAuthorFunc: function(){
alert("someAuthorFunc run");
}
};
if($('body').hasClass("authors") == true ){
authorsjs.init();
}
});
$(document).ready(function(){
var booksjs = {
init: function(){
alert("init booksjs run");
// all books bindings specified here
$("#twoEl").on('click', function(){
alert("twoEl was clicked");
booksjs.someBookFunc();
});
},
someBookFunc: function(){
alert("someBookFunc run");
}
};
if($('body').hasClass("booksjs") == true ){
booksjs.init();
}
});
$(document).ready(function(){
var blogsjs = {
init: function(){
blogsjs.formValidations();
},
formValidations: function(){
$('#blog_title').on("click", function(){
console.log($(this).val());
$('p').css("color", "green");
$(this).css("color", "blue");
alert("hello");
});
$('#blog_title').on("keypress", function(){
var value = $(this).val();
$("#preview").text(value);
});
}
};
if($('body').hasClass("blogs") == true){
blogsjs.init();
}
});
$(document).ready(function(){
var someGlobals = {
init: function(){
$('p').on('click', function(){
alert("Global: P was clicked somehwere!");
})
}
};
someGlobals.init();
});
Hello everyone,
I have just recently started learning the require.js , but I don't seem to understand as how does the whole mechanism work...and especialy how do I access the variables/objects created using this javascript library.
Let me explain it further in the example below..
webroot/page.jsp
<html>
<script data-main="js/page" src="js/lib/require.js"></script>
</html>
webroot/js/common.js
requirejs.config({
baseUrl: 'js/lib',
paths: {
app: '../app',
}
});
webroot/js/app/page.js
require(['./common'], function (common) {
require(['./app/page/init'], function(page)
{
page.load(1);
});
});
webroot/js/app/page/init.js
define(['./load'], function (load) {
var LoadPage = new load();
return LoadPage;
});
webroot/js/app/page/load.js
define(function () {
function loadPage() {
}
loadPage.prototype = {
load: function (page_id) {
console.log("Opening page "+page_id+" ");
}
};
return loadPage;
});
now when I run the page everything works perfectly, but now I'd like to execute the load method by clicking on a button.
<div onclick="page.load(2);"> </div>
but this does not work..So I'd like to know if there is any possibility to bind this action to any button or link...whatever..
(apologies for my bad english)
Thanks,
Alex
How about as below assuming you are using JQuery:
<div id="divPageLoad"></div>
<script language="javascript" type="text/javascript">
$("#divPageLoad").on("click", function() {
page.load(2);
});
</script>
I first want to say that I am new to RequireJS and even newer to Jasmine.
I am having some issues with the SpecRunner and require JS. I have been following the tutorials of Uzi Kilon and Ben Nadel (along with some others) and they helped some but I am still having some issues.
It seems that, if there is an error that is thrown in the test (I can think of one in particular, a type error) the spec runner html will display. This tells me that I have some issues in the javascript. However, after I fix those error no HTML is displayed anymore. I cannot get the test runner to display at all. Can someone find something wrong with my code that would cause this issue?
Here is my directory structure:
Root
|-> lib
|-> jasmine
|-> lib (contains all of the jasmine lib)
|-> spec
|-> src
|-> jquery (jquery js file)
|-> require (require js file)
index.html (spec runner) specRunner.js
Here is the SpecRunner (index) HTML:
<!doctype html>
<html lang="en">
<head>
<title>Javascript Tests</title>
<link rel="stylesheet" href="lib/jasmine/lib/jasmine.css">
<script src="lib/jasmine/lib/jasmine.js"></script>
<script src="lib/jasmine/lib/jasmine-html.js"></script>
<script src="lib/jquery/jquery.js"></script>
<script data-main="specRunner" src="lib/require/require.js"></script>
<script>
require({ paths: { spec: "lib/jasmine/spec" } }, [
// Pull in all your modules containing unit tests here.
"spec/notepadSpec"
], function () {
jasmine.getEnv().addReporter(new jasmine.HtmlReporter());
jasmine.getEnv().execute();
});
</script>
</head>
<body>
</body>
</html>
Here is the specRunner.js (config)
require.config({
urlArgs: 'cb=' + Math.random(),
paths: {
jquery: 'lib/jquery',
jasmine: 'lib/jasmine/lib/jasmine',
'jasmine-html': 'lib/jasmine/lib/jasmine-html',
spec: 'lib/jasmine/spec/'
},
shim: {
jasmine: {
exports: 'jasmine'
},
'jasmine-html': {
deps: ['jasmine'],
exports: 'jasmine'
}
}
});
Here is a spec:
require(["../lib/jasmine/src/notepad"], function (notepad) {
describe("returns titles", function() {
expect(notepad.noteTitles()).toEqual("");
});
});
The notepad source:
define(['lib/jasmine/src/note'], function (note) {
var notes = [
new note('pick up the kids', 'dont forget to pick up the kids'),
new note('get milk', 'we need two gallons of milk')
];
return {
noteTitles: function () {
var val;
for (var i = 0, ii = notes.length; i < ii; i++) {
//alert(notes[i].title);
val += notes[i].title + ' ';
}
return val;
}
};
});
And the Note source (JIC):
define(function (){
var note = function(title, content) {
this.title = title;
this.content = content;
};
return note;
});
I have made sure that, as far as the app is concerned, the paths are correct. Once I get this working I can play with configuring that paths so that it isn't so yucky.
I managed to get this working with some trial and error. The main issue was that when you write specs it isn't a require that you want to create, you want to use define:
Original:
require(["/lib/jasmine/src/notepad"], function (notepad) {
describe("returns titles", function() {
expect(notepad.noteTitles()).toEqual("pick up the kids get milk");
});
});
Working:
define(["lib/jasmine/src/notepad"], function (notepad) {
describe("returns titles", function () {
it("something", function() {
expect(notepad.noteTitles()).toEqual("pick up the kids get milk ");
});
});
});
After doing some research it became clear that, when using RequireJS, Anything that you want the require() to use must be wrapped in a define (seems obvious now I guess). You can see that, in the specRunner.js file, a require is used when executing the tests (therefore the need to "define" the specs.
The other issue is that, when creating specs, the describe() AND the it() are necessary (not just the describe like I had in the posted example).
Original:
describe("returns titles", function() {
expect(notepad.noteTitles()).toEqual("pick up the kids get milk");
});
Working:
describe("returns titles", function () {
it("something", function() {
expect(notepad.noteTitles()).toEqual("pick up the kids get milk ");
});
});
I also changed around where the test runner exists but this was a refactor and did not change the outcome of the tests.
Again, here are the files and the changed:
note.js: stayed the same
notepad.js: stayed the same
index.html:
<!doctype html>
<html lang="en">
<head>
<title>Javascript Tests</title>
<link rel="stylesheet" href="lib/jasmine/lib/jasmine.css">
<script data-main="specRunner" src="lib/require/require.js"></script>
</head>
<body>
</body>
</html>
specRunner.js:
require.config({
urlArgs: 'cb=' + Math.random(),
paths: {
jquery: 'lib/jquery',
'jasmine': 'lib/jasmine/lib/jasmine',
'jasmine-html': 'lib/jasmine/lib/jasmine-html',
spec: 'lib/jasmine/spec/'
},
shim: {
jasmine: {
exports: 'jasmine'
},
'jasmine-html': {
deps: ['jasmine'],
exports: 'jasmine'
}
}
});
require(['jquery', 'jasmine-html'], function ($, jasmine) {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function (spec) {
return htmlReporter.specFilter(spec);
};
var specs = [];
specs.push('lib/jasmine/spec/notepadSpec');
$(function () {
require(specs, function (spec) {
jasmineEnv.execute();
});
});
});
notepadSpec.js:
define(["lib/jasmine/src/notepad"], function (notepad) {
describe("returns titles", function () {
it("something", function() {
expect(notepad.noteTitles()).toEqual("pick up the kids get milk");
});
});
});
Just adding this as an alternate answer for people who are you using Jasmine 2.0 standalone. I believe this can work for Jasmine 1.3 also, but the async syntax is different and kind of ugly.
Here is my modified SpecRunner.html file:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Jasmine Spec Runner v2.0.0</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine-2.0.0/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="lib/jasmine-2.0.0/jasmine.css">
<!--
Notice that I just load Jasmine normally
-->
<script type="text/javascript" src="lib/jasmine-2.0.0/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-2.0.0/jasmine-html.js"></script>
<script type="text/javascript" src="lib/jasmine-2.0.0/boot.js"></script>
<!--
Here we load require.js but we do not use data-main. Instead we will load the
the specs separately. In short we need to load the spec files synchronously for this
to work.
-->
<script type="text/javascript" src="js/vendor/require.min.js"></script>
<!--
I put my require js config inline for simplicity
-->
<script type="text/javascript">
require.config({
baseUrl: 'js',
shim: {
'underscore': {
exports: '_'
},
'react': {
exports: 'React'
}
},
paths: {
jquery: 'vendor/jquery.min',
underscore: 'vendor/underscore.min',
react: 'vendor/react.min'
}
});
</script>
<!--
I put my spec files here
-->
<script type="text/javascript" src="spec/a-spec.js"></script>
<script type="text/javascript" src="spec/some-other-spec.js"></script>
</head>
<body>
</body>
</html>
Now here is an example spec file:
describe("Circular List Operation", function() {
// The CircularList object needs to be loaded by RequireJs
// before we can use it.
var CircularList;
// require.js loads scripts asynchronously, so we can use
// Jasmine 2.0's async support. Basically it entails calling
// the done function once require js finishes loading our asset.
//
// Here I put the require in the beforeEach function to make sure the
// Circular list object is loaded each time.
beforeEach(function(done) {
require(['lib/util'], function(util) {
CircularList = util.CircularList;
done();
});
});
it("should know if list is empty", function() {
var list = new CircularList();
expect(list.isEmpty()).toBe(true);
});
// We can also use the async feature on the it function
// to require assets for a specific test.
it("should know if list is not empty", function(done) {
require(['lib/entity'], function(entity) {
var list = new CircularList([new entity.Cat()]);
expect(list.isEmpty()).toBe(false);
done();
});
});
});
Here is a link the async support section from the Jasmine 2.0 docs: http://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support
Another option for Jasmine 2.0 standalone is creating a boot.js file and setting it up to run your tests after all of your AMD modules have been loaded.
The ideal end user case for writing tests in our case was to not have to list out all of our spec files or dependencies in once explicit list, and only have the requirement of declaring your *spec files as AMD modules with dependencies.
Example ideal spec: spec/javascript/sampleController_spec.js
require(['app/controllers/SampleController'], function(SampleController) {
describe('SampleController', function() {
it('should construct an instance of a SampleController', function() {
expect(new SampleController() instanceof SampleController).toBeTruthy();
});
});
});
Ideally the background behaviour of loading the dependency in and running the specs would be totally opaque to anyone coming on to the project wanting to write tests, and they won't need to do anything other than create a *spec.js file with AMD dependencies.
To get this all working, we created a boot file and configured Jasmine to use it (http://jasmine.github.io/2.0/boot.html), and added some magic to wrap around require to temporarily delay running tests until after we have our deps loaded:
Our boot.js' "Execution" section:
/**
* ## Execution
*
* Replace the browser window's `onload`, ensure it's called, and then run all of the loaded specs. This includes initializing the `HtmlReporter` instance and then executing the loaded Jasmine environment. All of this will happen after all of the specs are loaded.
*/
var currentWindowOnload = window.onload;
// Stack of AMD spec definitions
var specDefinitions = [];
// Store a ref to the current require function
window.oldRequire = require;
// Shim in our Jasmine spec require helper, which will queue up all of the definitions to be loaded in later.
require = function(deps, specCallback){
//push any module defined using require([deps], callback) onto the specDefinitions stack.
specDefinitions.push({ 'deps' : deps, 'specCallback' : specCallback });
};
//
window.onload = function() {
// Restore original require functionality
window.require = oldRequire;
// Keep a ref to Jasmine context for when we execute later
var context = this,
requireCalls = 0, // counter of (successful) require callbacks
specCount = specDefinitions.length; // # of AMD specs we're expecting to load
// func to execute the AMD callbacks for our test specs once requireJS has finished loading our deps
function execSpecDefinitions() {
//exec the callback of our AMD defined test spec, passing in the returned modules.
this.specCallback.apply(context, arguments);
requireCalls++; // inc our counter for successful AMD callbacks.
if(requireCalls === specCount){
//do the normal Jamsine HTML reporter initialization
htmlReporter.initialize.call(context);
//execute our Jasmine Env, now that all of our dependencies are loaded and our specs are defined.
env.execute.call(context);
}
}
var specDefinition;
// iterate through all of our AMD specs and call require with our spec execution callback
for (var i = specDefinitions.length - 1; i >= 0; i--) {
require(specDefinitions[i].deps, execSpecDefinitions.bind(specDefinitions[i]));
}
//keep original onload in case we set one in the HTML
if (currentWindowOnload) {
currentWindowOnload();
}
};
We basically keep our AMD syntax specs in a stack, pop them off, require the modules, execute the callback with our assertions in it, then run Jasmine once everything is done loading in.
This set up allows us to wait until all of the AMD modules required by our individual tests are loaded, and doesn't break AMD patterns by creating globals. There's a little hackery in the fact that we temporarily override require, and only load our app code using require (our `src_dir: in jasmine.yml is empty), but the overall goal here is to reduce the overhead of writing a spec.
you can use done in combo with before filters to test asynchronous callbacks:
beforeEach(function(done) {
return require(['dist/sem-campaign'], function(campaign) {
module = campaign;
return done();
});
});
This is how I do to run a jasmine spec in a html using AMD/requirejs for all my sources and specs.
This is my index.html file that loads jasmine and then my 'unit test starter' :
<html><head><title>unit test</title><head>
<link rel="shortcut icon" type="image/png" href="/jasmine/lib/jasmine-2.1.3/jasmine_favicon.png">
<link rel="stylesheet" href="/jasmine/lib/jasmine-2.1.3/jasmine.css">
<script src="/jasmine/lib/jasmine-2.1.3/jasmine.js"></script>
<script src="/jasmine/lib/jasmine-2.1.3/jasmine-html.js"></script>
<script src="/jasmine/lib/jasmine-2.1.3/boot.js"></script>
</head><body>
<script data-main="javascript/UnitTestStarter.js" src="javascript/require.js"></script>
</body></html>
and then my UnitTestStarter.js is something like this:
require.config({
"paths": {
....
});
require(['MySpec.js'], function()
{
jasmine.getEnv().execute();
})