Commit cc24ca55 by Andy Armstrong

Support local debugging of XBlock JavaScript

It turns out that loading JavaScript with $.getScript
causes Chrome to treat the file as an XHR request
and not JS. I've switched over to using RequireJS
to load the URL which already has the ability to
dynamically load files and have the browser
recognize them.
parent c14c146d
define ["jquery", "js/spec_helpers/edit_helpers", "coffee/src/views/module_edit", "js/models/module_info", "xmodule"], ($, edit_helpers, ModuleEdit, ModuleModel) -> define ["jquery", "common/js/components/utils/view_utils", "js/spec_helpers/edit_helpers",
"coffee/src/views/module_edit", "js/models/module_info", "xmodule"],
($, ViewUtils, edit_helpers, ModuleEdit, ModuleModel) ->
describe "ModuleEdit", -> describe "ModuleEdit", ->
beforeEach -> beforeEach ->
...@@ -60,7 +62,7 @@ define ["jquery", "js/spec_helpers/edit_helpers", "coffee/src/views/module_edit" ...@@ -60,7 +62,7 @@ define ["jquery", "js/spec_helpers/edit_helpers", "coffee/src/views/module_edit"
spyOn(@moduleEdit, 'loadDisplay') spyOn(@moduleEdit, 'loadDisplay')
spyOn(@moduleEdit, 'delegateEvents') spyOn(@moduleEdit, 'delegateEvents')
spyOn($.fn, 'append') spyOn($.fn, 'append')
spyOn($, 'getScript').andReturn($.Deferred().resolve().promise()) spyOn(ViewUtils, 'loadJavaScript').andReturn($.Deferred().resolve().promise());
window.MockXBlock = (runtime, element) -> window.MockXBlock = (runtime, element) ->
return { } return { }
...@@ -150,7 +152,7 @@ define ["jquery", "js/spec_helpers/edit_helpers", "coffee/src/views/module_edit" ...@@ -150,7 +152,7 @@ define ["jquery", "js/spec_helpers/edit_helpers", "coffee/src/views/module_edit"
expect($('head').append).toHaveBeenCalledWith("<script>inline-js</script>") expect($('head').append).toHaveBeenCalledWith("<script>inline-js</script>")
it "loads js urls from fragments", -> it "loads js urls from fragments", ->
expect($.getScript).toHaveBeenCalledWith("js-url") expect(ViewUtils.loadJavaScript).toHaveBeenCalledWith("js-url")
it "loads head html", -> it "loads head html", ->
expect($('head').append).toHaveBeenCalledWith("head-html") expect($('head').append).toHaveBeenCalledWith("head-html")
......
define([ "jquery", "common/js/spec_helpers/ajax_helpers", "URI", "js/views/xblock", "js/models/xblock_info", define(["jquery", "URI", "common/js/spec_helpers/ajax_helpers", "common/js/components/utils/view_utils",
"xmodule", "coffee/src/main", "xblock/cms.runtime.v1"], "js/views/xblock", "js/models/xblock_info", "xmodule", "coffee/src/main", "xblock/cms.runtime.v1"],
function ($, AjaxHelpers, URI, XBlockView, XBlockInfo) { function ($, URI, AjaxHelpers, ViewUtils, XBlockView, XBlockInfo) {
"use strict";
describe("XBlockView", function() { describe("XBlockView", function() {
var model, xblockView, mockXBlockHtml; var model, xblockView, mockXBlockHtml;
...@@ -89,11 +89,11 @@ define([ "jquery", "common/js/spec_helpers/ajax_helpers", "URI", "js/views/xbloc ...@@ -89,11 +89,11 @@ define([ "jquery", "common/js/spec_helpers/ajax_helpers", "URI", "js/views/xbloc
it('aborts rendering when a dependent script fails to load', function() { it('aborts rendering when a dependent script fails to load', function() {
var requests = AjaxHelpers.requests(this), var requests = AjaxHelpers.requests(this),
mockJavaScriptUrl = "mock.js", missingJavaScriptUrl = "no_such_file.js",
promise; promise;
spyOn($, 'getScript').andReturn($.Deferred().reject().promise()); spyOn(ViewUtils, 'loadJavaScript').andReturn($.Deferred().reject().promise());
promise = postXBlockRequest(requests, [ promise = postXBlockRequest(requests, [
["hash5", { mimetype: "application/javascript", kind: "url", data: mockJavaScriptUrl }] ["hash5", { mimetype: "application/javascript", kind: "url", data: missingJavaScriptUrl }]
]); ]);
expect(promise.isRejected()).toBe(true); expect(promise.isRejected()).toBe(true);
}); });
...@@ -104,7 +104,7 @@ define([ "jquery", "common/js/spec_helpers/ajax_helpers", "URI", "js/views/xbloc ...@@ -104,7 +104,7 @@ define([ "jquery", "common/js/spec_helpers/ajax_helpers", "URI", "js/views/xbloc
postXBlockRequest(AjaxHelpers.requests(this), []); postXBlockRequest(AjaxHelpers.requests(this), []);
xblockView.$el.find(".notification-action-button").click(); xblockView.$el.find(".notification-action-button").click();
expect(notifySpy).toHaveBeenCalledWith("add-missing-groups", model.get("id")); expect(notifySpy).toHaveBeenCalledWith("add-missing-groups", model.get("id"));
}) });
}); });
}); });
}); });
define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"], define(["jquery", "underscore", "common/js/components/utils/view_utils", "js/views/baseview", "xblock/runtime.v1"],
function ($, _, BaseView, XBlock) { function ($, _, ViewUtils, BaseView, XBlock) {
'use strict';
var XBlockView = BaseView.extend({ var XBlockView = BaseView.extend({
// takes XBlockInfo as a model // takes XBlockInfo as a model
...@@ -83,7 +84,7 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"], ...@@ -83,7 +84,7 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"],
* may have thrown JavaScript errors after rendering in which case the xblock parameter * may have thrown JavaScript errors after rendering in which case the xblock parameter
* will be null. * will be null.
*/ */
xblockReady: function(xblock) { xblockReady: function(xblock) { // jshint ignore:line
// Do nothing // Do nothing
}, },
...@@ -95,7 +96,7 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"], ...@@ -95,7 +96,7 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"],
* represents this process. * represents this process.
* @param fragment The fragment returned from the xblock_handler * @param fragment The fragment returned from the xblock_handler
* @param element The element into which to render the fragment (defaults to this.$el) * @param element The element into which to render the fragment (defaults to this.$el)
* @returns {jQuery promise} A promise representing the rendering process * @returns {Promise} A promise representing the rendering process
*/ */
renderXBlockFragment: function(fragment, element) { renderXBlockFragment: function(fragment, element) {
var html = fragment.html, var html = fragment.html,
...@@ -131,7 +132,7 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"], ...@@ -131,7 +132,7 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"],
* Dynamically loads all of an XBlock's dependent resources. This is an asynchronous * Dynamically loads all of an XBlock's dependent resources. This is an asynchronous
* process so a promise is returned. * process so a promise is returned.
* @param resources The resources to be rendered * @param resources The resources to be rendered
* @returns {jQuery promise} A promise representing the rendering process * @returns {Promise} A promise representing the rendering process
*/ */
addXBlockFragmentResources: function(resources) { addXBlockFragmentResources: function(resources) {
var self = this, var self = this,
...@@ -171,7 +172,7 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"], ...@@ -171,7 +172,7 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"],
/** /**
* Loads the specified resource into the page. * Loads the specified resource into the page.
* @param resource The resource to be loaded. * @param resource The resource to be loaded.
* @returns {jQuery promise} A promise representing the loading of the resource. * @returns {Promise} A promise representing the loading of the resource.
*/ */
loadResource: function(resource) { loadResource: function(resource) {
var head = $('head'), var head = $('head'),
...@@ -189,8 +190,7 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"], ...@@ -189,8 +190,7 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"],
if (kind === "text") { if (kind === "text") {
head.append("<script>" + data + "</script>"); head.append("<script>" + data + "</script>");
} else if (kind === "url") { } else if (kind === "url") {
// Return a promise for the script resolution return ViewUtils.loadJavaScript(data);
return $.getScript(data);
} }
} else if (mimetype === "text/html") { } else if (mimetype === "text/html") {
if (placement === "head") { if (placement === "head") {
...@@ -202,11 +202,11 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"], ...@@ -202,11 +202,11 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"],
}, },
fireNotificationActionEvent: function(event) { fireNotificationActionEvent: function(event) {
var eventName = $(event.currentTarget).data("notification-action"); var eventName = $(event.currentTarget).data("notification-action");
if (eventName) { if (eventName) {
event.preventDefault(); event.preventDefault();
this.notifyRuntime(eventName, this.model.get("id")); this.notifyRuntime(eventName, this.model.get("id"));
} }
} }
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment