From Electron Cloud
Jump to: navigation, search
(Created page with '==How to create a "binding" in Javascript== Usually this is done in the QML: Item { destVariable: sourceObject.sourceVariable } But if for some reason you need to do it la…')
 
Line 12: Line 12:
 
  // Create a binding
 
  // Create a binding
 
  destObject.destVariable = function() { return s.sourceVariable }
 
  destObject.destVariable = function() { return s.sourceVariable }
 +
 +
==Dump all variables of a type==
 +
This version dumps all numeric variables in an object:
 +
 +
function dumpNumberVars(object) {
 +
    console.log("dump:" + object)
 +
    var vars = new Array();
 +
    for (var member in object)
 +
        if (typeof(object[member]) == "number")
 +
            vars.push(member);
 +
    vars = vars.sort();
 +
    for(var i=0,len=vars.length; i<len; i++)
 +
        console.log("  " + vars[i] + " " + object[vars[i]]);
 +
}

Revision as of 05:09, 1 November 2011

How to create a "binding" in Javascript

Usually this is done in the QML:

Item {
   destVariable: sourceObject.sourceVariable
}

But if for some reason you need to do it later in Javascript code (e.g. an onCompleted function), you need to create a "closure" which returns the value, and assign that instead of assigning the value itself, in order to have the usual feature of bindings, that the variable being assigned will be updated when the source value changes, rather than being just a one-time assignment.

// If you have trouble with the source object being "undefined" later, 
// it just needs to be a local-enough variable to be included in the "closure", it seems
var s = sourceObject;
// Create a binding
destObject.destVariable = function() { return s.sourceVariable }

Dump all variables of a type

This version dumps all numeric variables in an object:

function dumpNumberVars(object) {
   console.log("dump:" + object)
   var vars = new Array();
   for (var member in object)
       if (typeof(object[member]) == "number")
           vars.push(member);
   vars = vars.sort();
   for(var i=0,len=vars.length; i<len; i++)
       console.log("   " + vars[i] + " " + object[vars[i]]);
}