This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

DEVELOPERS FORUM - Puzzled over some Javascript

Brian - Tuesday, September 7, 2010 6:38 AM:

I'm refreshing my javascript and looking at creating some objects to do some work for me.

The code that I have written works in an HTML page. Works in an HTML field of of an Innovator Form. Fails miserably as a Client Side method attached to the OnClick event for a button in an Innovator form.

This is the code that works in the HTML page.

<html>
<body>
 
<script type="text/javascript">
function bom() {
    this.m_bomlist = new Array();
}
 
bom.prototype.addBomItem = function fnAddBomItem(itemNumber, description, qty)
{
    var tmpBomItem = new bomItem(itemNumber, description, qty );
    this.m_bomlist.push( tmpBomItem );
};
 
bom.prototype.getBomItem = function getBomItem(itemNumber)
{
    for (var j=0; j < this.m_bomlist.length; j++)
    {
         if ( this.m_bomlist[j].isItemNumber(itemNumber) )
         {
             return this.m_bomlist[j];
         }
    }
    return null;
};
 
function bomItem(itemNumber, description, qty) {
    this.m_itemNumber = itemNumber;
    this.m_description = description;
    this.m_qty = qty;
}
 
bomItem.prototype.getItemNumber = function fnGetItemNumber()
{    return this.m_itemNumber; };
bomItem.prototype.getDescription = function getDescription()
{    return this.m_description; };
bomItem.prototype.getQty = function getQty()
{     return this.m_qty; };
bomItem.prototype.isItemNumber = function isItemNumber(itemNumber)
{    return (this.m_itemNumber == itemNumber); };
bomItem.prototype.isQty = function isQty(qty)
{    return (this.m_qty == qty); };
 
 
var myBOM = new bom();
myBOM.addBomItem( "1234", "an item description", 5);
myBOM.addBomItem("1235","another description",3);
myBOM.addBomItem("1236","more interesting descriptions",1);
 
var rBom = myBOM.getBomItem("1234");
if (rBom === null)
{
    document.write("BOM getItem failed");
    document.write('<BR>');
}
else
{
    document.write( "myBOM.getBomItem Success " + rBom.getItemNumber() + "   " + rBom.getDescription() + "  " + rBom.getQty());
    document.write('<BR>');
}
 
for (var i=0; i<myBOM.m_bomlist.length;i++)
{
    document.write("Element " + i + "  " + myBOM.m_bomlist[i].getItemNumber() + "   " + myBOM.m_bomlist[i].getDescription() + "  " + myBOM.m_bomlist[i].getQty());
     document.write('<BR>');
}
 
 
var r1Bom = myBOM.getBomItem("4321");
if (r1Bom === null)
{
    document.write("BOM getItem failed");
    document.write('<BR>');
}
else
{
    document.write(r1Bom.getItemNumber() + "   " + r1Bom.getDescription() + "  " + r1Bom.getQty());
    document.write('<BR>');
}
 
var myBOMItem = new bomItem( "1222", "a useful description", 5);
document.write( "myBOMItem readback " +  myBOMItem.getItemNumber() + " " + myBOMItem.getDescription() + " " + myBOMItem.getQty() );
    document.write('<BR>');
document.write("Rewrite of getBomItem object " + rBom.getItemNumber() + "   " + rBom.getDescription() + "  " + rBom.getQty());
    document.write('<BR>');
 
document.write("End");
</script>
 
</body>
</html>

This is the same code except that the document.write statements have been replaced with top.aras.AlertSuccess statements. This code is in the onClick event of a button on an Innovator form.

function bom() {
    this.m_bomlist = new Array();
}
 
bom.prototype.addBomItem = function fnAddBomItem(itemNumber, description, qty)
{
    var tmpBomItem = new bomItem(itemNumber, description, qty );
    this.m_bomlist.push( tmpBomItem );
};
 
bom.prototype.getBomItem = function getBomItem(itemNumber)
{
    for (var j=0; j < this.m_bomlist.length; j++)
    {
         if ( this.m_bomlist[j].isItemNumber(itemNumber) )
         {
             return this.m_bomlist[j];
         }
    }
    return null;
};
 
function bomItem(itemNumber, description, qty) {
    this.m_itemNumber = itemNumber;
    this.m_description = description;
    this.m_qty = qty;
}
 
 
bomItem.prototype.getItemNumber = function fnGetItemNumber()
{
    return this.m_itemNumber;
};
 
bomItem.prototype.getDescription = function getDescription()
{
    return this.m_description;
};
bomItem.prototype.getQty = function getQty()
{
    return this.m_qty;
};
bomItem.prototype.isItemNumber = function isItemNumber(itemNumber)
{
    return (this.m_itemNumber == itemNumber);
};
bomItem.prototype.isQty = function isQty(qty)
{
    return (this.m_qty == qty);
};
 
 
var myBOM = new bom();
myBOM.addBomItem( "1234", "an item description", 5);
myBOM.addBomItem("1235","another description",3);
myBOM.addBomItem("1236","more interesting descriptions",1);
 
var rBom = myBOM.getBomItem("1234");
if (rBom === null)
{
    top.aras.AlertError("BOM getItem failed");
}
else
{
    top.aras.AlertSuccess(rBom.getItemNumber() + "   " + rBom.getDescription() + "  " + rBom.getQty());
}
 
for (var i=0; i<myBOM.m_bomlist.length;i++)
{
    top.aras.AlertSuccess(myBOM.m_bomlist[i].getItemNumber() + "   " + myBOM.m_bomlist[i].getDescription() + "  " + myBOM.m_bomlist[i].getQty());
}
 
 
var r1Bom = myBOM.getBomItem("4321");
if (r1Bom === null)
{
    top.aras.AlertError("BOM getItem failed");
}
else
{
    top.aras.AlertSuccess(r1Bom.getItemNumber() + "   " + r1Bom.getDescription() + "  " + r1Bom.getQty());
}
 
 
 
var myBOMItem = new bomItem( "1222", "a useful description", 5);
top.aras.AlertSuccess( myBOMItem.getItemNumber() + " " + myBOMItem.getDescription() + " " + myBOMItem.getQty() );
 
top.aras.AlertSuccess(rBom.getItemNumber() + "   " + rBom.getDescription() + "  " + rBom.getQty());
 
 
top.aras.AlertSuccess("End");
return;

I would be overjoyed if someone could tell me why the code doesn't work as the onClick button event but works perfectly in an HTML window or HTML field.

Regards,

Brian.



Brian - Tuesday, September 7, 2010 7:23 AM:

Don't worry. I worked it out.

The "this" item in the object/function definitions was changed by Innovator to be the button onClick context. Therefore not being the "current item" as expected by JavaScript.

To test I included the object definition code in the widget_events.js which is included as part of innovator.aspx

It works fine now.

I guess the next question is where is the "approved" place to put JavaScript object/function code.

Cheers,

Brian.



Brian - Tuesday, September 7, 2010 11:51 PM:

Alright. I think I have that worked out too.

Code goes in a .js file in the ..Innovator/Server/JavaScript directory.

Reference to this new file goes in "JSNamespaceConfig.xml"

Then it really works as expected without upsetting the upgradability of the system.

Cheers,

Brian.