Note that there are some explanatory texts on larger screens.

plurals
  1. POCall a function declared inside a variable by a parameter
    primarykey
    data
    text
    <p>I would like to be able to run a function declared within my variable "Table". The reason is to avoid a string based parsing to run a function... </p> <pre><code> var Table = function( params ){ var rowTemplate = params.rowTemplate; var table = params.table; function GetRow() { return $( rowTemplate ).html(); } function AddRow() { $( table ).append( GetRow() ); } function BindEvents() { for( a in params.bind ) { params.bind[ a ].action(); } } function Construct() { BindEvents(); } Construct(); return { AddRow: function() { AddRow(); } } }; var ProductsTable = new Table( { table: "#ProductsTable tbody", rowTemplate: "#ProductsTable_TemplateRow", bind: [ { action: AddRow, element: "#productsAddRowButton" } ] } ); </code></pre> <p>Can you see that my ProductsTable passes into my Construct an object with a property called "bind" which is an array of the actions and the element that instantiates them.</p> <p>AddRow is obviously not yet declared. so it throws an error. What I do not want to do is pass in a string "AddRow" because then i'll be parsing a string... to then run the function from within the Table scope and that's ugly.</p> <p>Any solutions to this?</p> <p>Here is my solution, thanks to the answer of "No it cannot be done" which put me on the track of the switch statement :( ... :) :</p> <pre><code> var Table = function( params ){ var rowTemplate = params.rowTemplate; var table = params.table; function GetRow() { return $( rowTemplate ).html(); } function AddRow() { $( table ).append( GetRow() ); } function BindEvents() { for( a in params.bind ) { switch( params.bind[ a ].action ) { case "AddRow": $( params.bind[ a ].element ).click( function(){ AddRow(); } ); break; } } } function Construct() { BindEvents(); } Construct(); return { AddRow: function() { AddRow(); } } }; var ProductsTable = new Table( { table: "#ProductsTable tbody", rowTemplate: "#ProductsTable_TemplateRow", bind: [ { action: "AddRow", element: "#productsAddRowButton" } ] } ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload