0

I am using ASP.NET MVC to build a web application, and I have fallen in love with the library Q.js, available here:

Q.js

Right now, I am using version 1.0, I load it up in the way that is most natural to me as an ASP.NET MVC developer. I have a place in my BundleConfig that just loads the script with my other scripts.

BundleConfig.cs

public static class BundleConfig {
   public static void RegisterBundles(BundleCollection bundles){
      bundles.Add(new ScriptBundle("~/bundles/scripts")
         .Include("~/content/scripts/jquery.js")
         // lots of other includes
         .Include("~/content/scripts/q.js"));
   }
}

So then in my view _Layout.cshtml, it's the normal simple process...

_Layout.cshtml

<head>
   @Scripts.Render("~/bundles/scripts")
</head>

Easy enough, right? Yes, it works fine. But I notice that Q.js has another branch labelled v2.

Now, from what I can immediately tell, they are not that much different, but I do not believe the creator would have made a version 2 if they were not doing it to improve the product. I'd like to try it out, but this is where my experience fails me.

version 2 seems fundamentally different than version 1. Here is a link to it for quicker reference; Qv2

The q.js file starts out with this at line 43.

require("collections/shim");
var WeakMap = require("collections/weak-map");
var Iterator = require("collections/iterator");
var asap = require("asap");

I am accustomed to the require function being a part of requirejs, but I don't believe that is the purpose being served here. I in fact think this is intended to be consumed/run/used by node.js.

Now, since I am using ASP.NET MVC, I won't be using node.js. I've attempted to just put the expected folders and files in the right place so that they would be path relative to q.js, but that does not seem to satisfy it.

The Actual Question

Is there a way I can 'compile' Q.js 2.0 into a .js file that will not require node.js, and can be loaded normally within my ASP.NET MVC project? Can I use node.js to actually create an output .js file that has everything I need?

Community
  • 1
  • 1
Ciel
  • 4,290
  • 8
  • 51
  • 110
  • 1
    Are you looking for [browserify](http://browserify.org/)? – Aadit M Shah Jul 21 '14 at 15:45
  • 1
    http://browserify.org/ is what you are looking for. – m90 Jul 21 '14 at 15:46
  • Hey, thanks for the very rapid feed back. I'll check this out. In the meantime, could I trouble one or both of you to actually post this as an official "answer", so that if you are right I can award you appropriate credit? – Ciel Jul 21 '14 at 15:47
  • possible duplicate of [How can I share code between Node.js and the browser?](http://stackoverflow.com/questions/3225251/how-can-i-share-code-between-node-js-and-the-browser) – m90 Jul 21 '14 at 15:48
  • Thanks. I will check that out too. I'm reading browserify right now and I am a bit confused. It looks to me like it is just another javascript library that runs in your code. So it's not something I use outside of my code to package it up? – Ciel Jul 21 '14 at 15:49
  • 1
    `browserify` will enable you to use node/CommonJS style `require` calls and using `module.exports` to define modules yourself in a browser environment. This: https://github.com/substack/browserify-handbook is a pretty good reference, also this:http://www.myeyeson.net/gulp-js-and-browserify-with-asp-net/ may help you getting stuff set up in ASP.NET – m90 Jul 21 '14 at 15:51
  • Thanks again, I am sorry I seem so stupid with this. `node.js` confuses the hell out of me, to be honest. – Ciel Jul 21 '14 at 15:53
  • I don't understand how browserify actually becomes aware of my project. Do I put a specific file in my ASP.NET MVC project? Or do I put this on my server, or my dev machine, or what? Is it just always there and looking at my javascript? – Ciel Jul 21 '14 at 15:54
  • 1
    Apparently (since you seem to be using require.js) you can just shim `Q`'s export like: http://stackoverflow.com/a/18827459/797194 – m90 Jul 21 '14 at 15:56
  • Thank you all for all of your help. This actually gets me much further than anything I've tried previously. – Ciel Jul 21 '14 at 16:17

1 Answers1

1

browserify is a tool for Node that takes all of the require()d dependencies, resolves them, and packages them into a single JavaScript file servable to the browser.

Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • FWIW, i thought of it on my own, before seeing the comments on the OP. – Scimonster Jul 21 '14 at 15:51
  • I am reading up on it right now, but I am still very confused about how to use it. Trying to see if I can find more examples. It seems to expect a lot of knowledge about node.js – Ciel Jul 21 '14 at 15:53
  • Basic usage is actually pretty simple. Just create a Node.js file with `require()`s, run the `browserify` command from a CLI, and be done with it. – Scimonster Jul 21 '14 at 15:54
  • So I would download Q.js v2 from the github, and then open a command line, go to where that file is stored, and run `browserify` on it, and it will find the dependencies and output a working file? – Ciel Jul 21 '14 at 15:55
  • Yep, provided all of Q's dependencies are available. – Scimonster Jul 21 '14 at 15:56
  • Well, I did it, and it did output a file, but it doesn't seem to work. I get the error `Q is not defined` when I try to simply run `Q()`. – Ciel Jul 21 '14 at 16:08
  • I've never used Q, so is it supposed to write to the global object? – Scimonster Jul 21 '14 at 16:13
  • I believe so, yes. That is at least present in a lot of its documentation – Ciel Jul 21 '14 at 16:14
  • Hm. Sorry, I don't think I can help you further. – Scimonster Jul 21 '14 at 16:15
  • Understandable. This is further than I've gotten before! Thank you very much! – Ciel Jul 21 '14 at 16:16