Reputation: 993
ScriptSharp's produced Javascript can't find my class method "LoadContent()". Where am I going wrong with this?
It seems to be putting the method into a prototype, but I'm not sure this is what I need. Any help would be greatly appreciated.
namespace Echo.Web.JScript
{
[ScriptNamespace("Scripts")]
public class MasterTrackerActions
{
private jQueryObject _source;
public MasterTrackerActions(string fundCodeSourceId)
{
_source = jQuery.Select("#" + fundCodeSourceId);
_source.Change(delegate
{
Script.Alert("Here we go"); //We see this fine.
LoadContent();
});
}
public void LoadContent()
{
Script.Alert("Do we get here?"); //Answer is no
}
}
}
Upvotes: 1
Views: 919
Reputation: 4092
I tried this in 0.8 and it works as expected (via the AMD pattern). I made slight changes to your code to make it compile in 0.8.
The S# class
using System.Html;
using jQueryApi;
namespace Echo.Web.JScript {
public class MasterTrackerActions {
private jQueryObject _source;
public MasterTrackerActions(string fundCodeSourceId) {
_source = jQuery.Select("#" + fundCodeSourceId);
_source.Change(delegate {
Window.Alert("Here we go"); //We see this fine.
LoadContent();
});
}
public void LoadContent() {
Window.Alert("Do we get here?"); //Answer is no
}
}
}
The resulting JS file is
/*! Scripts.js 1.0.0.0
*
*/
"use strict";
define('Scripts', ['ss', 'jquery'], function(ss, $) {
var $global = this;
// Echo.Web.JScript.MasterTrackerActions
function MasterTrackerActions(fundCodeSourceId) {
var $this = this;
this._source = $('#' + fundCodeSourceId);
this._source.change(function() {
alert('Here we go');
$this.loadContent();
});
}
var MasterTrackerActions$ = {
loadContent: function() {
alert('Do we get here?');
}
};
var $exports = ss.module('Scripts', null,
{
MasterTrackerActions: [ MasterTrackerActions, MasterTrackerActions$, null ]
});
return $exports;
});
Now to use this in the HTML file, you will need to either use the provided ModuleLoader SSLoader.js or RequireJS (preferred). In this case, you need the RequireJS module with JQuery. There are different ways to include JQuery as well (which is out of scope here).
The HTML file
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script type="text/javascript" data-main="Scripts/MyScript.js" src="Scripts/require-jquery.js"></script>
<script type="text/javascript">
require(['MyScript'], function (ms) {
var tracker = new ms.MasterTrackerActions('sampleText');
console.log(tracker);
});
</script>
<textarea id="sampleText">TEST123</textarea>
</body>
</html>
Oh one more thing, the event is triggered when the control loses focus...
Upvotes: 2