How do I use the .link plugin on object methods?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How do I use the .link plugin on object methods?

robini
Hi,

I have a javascript object with several properties.  The object also contains an instance method that returns the result of a calculation on two of the object's properties.

I'm using the new Jquery .link() plugin (http://api.jquery.com/link/), to update the UI and form elements with the object property values and vice versa, whenever either the form or the object is updated.

This is all working except for the form elements, which should contain the result of the object's instance method but never gets updated.

I've put a button on the form to manually check the value of Discrepancy, and this works but the jquery link plugin is not populating the input elements automatically, although it is populating the input elements linked to the object properties.  All elements have an id and name attributes.

Can anyone tell me where I'm going wrong?

My code:

function Product()
{
      this.InStock = 0;
      this.Ordered = 0;

}

// object's instance method
Product.protoype.Discrepancy = ComputeDiscrepancy;

function ComputeDiscrepancy()
{
      return this.InStock - this.Ordered;
}

$(document).ready(function () {
 
 
           var products= new Array();
 
           products[0] = new Product();
           products[1] = new Product();
 
           $("form").link(products[0], {
               InStock: "product0_InStock",
               Ordered: "product0_Ordered",
               Discrepancy: "product0_Discrepancy"
           });
 
          $("form").link(products[1], {
               InStock: "product1_InStock",
               Ordered: "product1_Ordered",
               Discrepancy: "product1_Discrepancy"
           });
            // button for me to manually check discrepancy method works
           $("#dataLinkBtn").click(function () {

               $("#product0_Discrepancy").val(products[0].Discrepancy());
           });
 
       });