Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Requirejs implementation

Thanks for the quick answer.

What version of Windows are you using, including the SKU, x64 vs. x86, etc.? Windows 8.1 x64
What version of Visual Studio are you using? Is it Visual Studio Express? VS2013 Ultimate
What version of ClearScript are you using? Did you build it, or are you using a NuGet package? Have you made any modifications? Nuget <package id="ClearScript.V8" version="" targetFramework="net45" />
What kind of project are you building - web, console, WPF, etc.? WPF Application
What version of the .NET framework is your project targeting? 4.5
Is your ModuleLoader class defined exactly as above? Yes
Can you share more of your code? We'd like to see how you're creating the script engine, what other objects you're exposing, etc.

Trying to load in jQuery or other libs that require a DOM, I am following the sample starter code given at http://lab.arc90.com/2010/11/22/jsdom/

Here is the code I have so far, more or less.
var engine = new V8ScriptEngine("engine", V8ScriptEngineFlags.EnableDebugging);
var typeCollection = new HostTypeCollection("mscorlib", "System", "System.Core");
engine.AddHostObject("clr", typeCollection);
//Now all the types defined in the standard mscorlib, System, and System.Core assemblies are exposed. Host type collections are hierarchical data structures where leaf nodes represent host types and parent nodes represent namespaces:
engine.Execute(@"var guid = clr.System.Guid.NewGuid();");
engine.Execute(@"var today = clr.System.DayOfWeek.Friday;");
//Note that, unlike C# types and namespaces, the nodes of a host type collection are objects. A script can copy them to the root level for more convenient access:
engine.Execute(@"var System = clr.System;");
engine.Execute(@"var System = clr.System;");
engine.Execute(@"var Guid = System.Guid;");
engine.Execute(@"var String = System.String;");
engine.AddHostType("Console", typeof(Console));
Type t = typeof (ModuleLoader);
dynamic moduleLoaderDefinedInClassLib = Activator.CreateInstance(t, engine);
engine.Script.moduleLoader = moduleLoaderDefinedInClassLib;
string currentDir = FileOperations.GetCurrentlyExecutingAssemblyLocation();
engine.Execute(File.ReadAllText(currentDir + @"\Scripts\require.js"));
engine.Execute(@"require.load = function (context, name, url) {
moduleLoader.LoadModuleAsync(context, name, url);
string htmlTest = "<html><head></head><body><h1>Hello World</h1><p>This is a <em>stupid simple</em> example</p></body></html>";
engine.Script.htmlTest = htmlTest;
engine.Execute(@"var html = htmlTest");

// create a new window, with the above markupstring jsdomPath = currentDir + @"\Scripts\jsdom" + ".js";;
engine.Script.jsdomPath = jsdomPath;
engine.Execute(@"moduleLoader.LoadModuleAsync(this, 'jsdom', jsdomPath);");
engine.Execute(@"var window = require(jsdomPath).jsdom(html).createWindow();");

// create a new script tag to load up jQuery
engine.Execute(@"script = window.document.createElement('script');");

engine.Execute(@"script.src = 'http://code.jquery.com/jquery-1.4.2.js';");

// when jQuery finishes loading
engine.Execute(@"script.onload = function() {
// Since this is not a browser we need to specify the window that jQuery has
// been added to
// Output: 'H1 Contents: Hello World'

// do a simple replace on the `em` text above
window.jQuery('p em').text('silly');

// Output: 'P Contents: This is a silly example'
// Append the element to the head element of the document
How are you getting to the line that executes require.js if you're hitting an exception two lines before? I have a first chance exception handler, code still runs. Not 100% sure why the code behaves like this!

Viewing all articles
Browse latest Browse all 2297