From Electron Cloud
Revision as of 08:08, 10 November 2011 by Ecloud (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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]]);
}

Javascript stack trace

function printStackTrace() {try { heresYourStackDump.i+=0; } catch(e) {console.log(e.stack) }}