Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  82] [ 6]  / answers: 1 / hits: 48309  / 10 Years ago, fri, november 14, 2014, 12:00:00

I have a TS code like this:





class MicrositeRequest {
micrositeName: string;
micrositeTemplate: string;

constructor() {
this.micrositeName = $(#micrositeNameId).val();
this.micrositeTemplate = $(#templateId option:selected).text();
}

public IsMicrositeRequestValid() {
if (this.checkForName() && this.checkForTemplate()) {
return true;
}
else {
return false;
}
}

checkForName() {
if (this.micrositeName != null && this.micrositeName.length != 0) {
return true;
}
else {
return false;
}
}

checkForTemplate() {
if (this.micrositeTemplate != null && this.micrositeTemplate.length != 0) {
return true;
}
else {
return false;
}
}
}


Here's the converted JS:



/// <reference path=scripts/typings/jquery/jquery.d.ts />
var MicrositeRequest = (function () {
function MicrositeRequest() {
this.micrositeName = $(#micrositeNameId).val();
this.micrositeTemplate = $(#templateId option:selected).text();
}
MicrositeRequest.prototype.IsMicrositeRequestValid = function () {
if (this.checkForName() && this.checkForTemplate()) {
return true;
}
else {
return false;
}
};
MicrositeRequest.prototype.checkForName = function () {
if (this.micrositeName != null && this.micrositeName.length != 0) {
return true;
}
else {
return false;
}
};
MicrositeRequest.prototype.checkForTemplate = function () {
if (this.micrositeTemplate != null && this.micrositeTemplate.length != 0) {
return true;
}
else {
return false;
}
};
return MicrositeRequest;
})();

//# sourceMappingURL=Microsite.js.map


On Click of a button I want to call the IsMicrositeRequestValid() method.



Here's the HTML:



<div>
<input type=submit name=submit value=Get onclick=IsMicrositeRequestValid() />
</div>


The Console says IsMicrositeRequestValid is not defined.



Any clues why this is happening and how I can fix it?


More From » typescript

 Answers
155

The call to IsMicrositeRequestValid in the onclick attribute requires that the function be part of the global namespace (window). Further, I'm pretty sure you'll need to instantiate MicrositeRequest object before the call to IsMicrositeRequestValid work (because it relies on this).



function validateRequest() { // declare a function in the global namespace
var mr = new MicrositeRequest();
return mr.IsMicrositeRequestValid();
}

<input type=submit name=sumbit value=Get onclick=validateRequest() />


is the quick & dirty way which should get it working.



You could also do this:



window.validateRequest = function () { // declare a function in the global namespace
var mr = new MicrositeRequest();
return mr.IsMicrositeRequestValid();
}


which I think is more readable.



Also, look into the Element.addEventListener method. It allows much more flexibility.



var submit = document.getElementById('my-submit');
submit.addEventListener('click', function () {
var mr = new MicrositeRequest();
return mr.IsMicrositeRequestValid();
});

<input type=submit id=my-submit name=sumbit value=Get />

[#68805] Wednesday, November 12, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stacie

Total Points: 476
Total Questions: 92
Total Answers: 102

Location: Bosnia and Herzegovina
Member since Tue, Mar 29, 2022
2 Years ago
stacie questions
Fri, Jun 26, 20, 00:00, 4 Years ago
Thu, Jan 23, 20, 00:00, 4 Years ago
Fri, Aug 30, 19, 00:00, 5 Years ago
Fri, Aug 2, 19, 00:00, 5 Years ago
;