setting a oject in javascript

  • I'm busy toying around with javascript, and I noticed i can do:

    Code
    krpano.get('preview');

    to get the complete preview xml node as an object.

    But if I make my own custompreview object, I can not set it directly.

    Code
    var cview = {};
    cview.fov = 40;
    krpano.set('view', cview);

    I get an "Error in Actionscript. Use a try/catch block to find error."

    Would setting an object directly be possible? Or should I create a custom setter which loops all the properties of the object?

    this works:

    Code
    var cview = {};
    cview.fov = 40;
    krpano.set('view.fov', cview.fov);
  • I solved it with a custom function

    Code
    krpano.set = function(property, value){
    		if(typeof(value) === 'object'){			
    			for(key in value){
    				krpano.set(property+'.'+key, value[key]);				
    			};
    		} else {
    			_krpano.set(property, value); /* _krpano = krpanoSWFObject */
    			krpano.trace('set:' + property + ' ->' + value);
    		}
        }

    Edited once, last by Zephyr (November 16, 2011 at 6:17 PM).

  • Hi,

    I must admit I have never tried passing a whole Javascript Object from JS to Flash...
    but it may work for 'normal' object, but not for some special predefined krpano objects like view, display, image, plugin ...
    these objects have internal 'base classes' and are partially 'not dynamic' so that adding custom attributes to them is not possible,

    best regards,
    Klaus

  • Well actually it works through my custom function I posted above.

    for example:

    Code
    var theview = krpano.get('view'); //gets the current view
    console.log(JSON.stringify(theview));
    theview.fov = 40; //set view.fov to 40
    krpano.set('view', theview); //pass the view back to the custom function which loops alll the props);

    console.log(JSON.stringigy(theview)); returns:

    Code
    "{"maxpixelzoom":null,"limitfov":true,"r_archi":0,"hlookatmin":null,"r_screenwidth":1477,"r_zoom":2.7474774194546225,"vlookatmin":null,"_vfovmin":1,"pannini":false,"fovmin":1,"vlookatmax":null,"r_screenheight":355,"r_zdistance":0,"hlookatmax":null,"r_matrix":{"m43":0,"m21":0,"m44":1,"m22":2.7474774194546225,"m23":0,"m24":0,"m31":2.7474774194546225,"m32":0,"m33":6.123233995736766e-17,"m11":1.6823447137323667e-16,"m34":0,"m12":0,"m41":0,"m13":-1,"m42":0,"m14":0},"fovmaxfish":179,"fovmax":179,"fovmaxfish2":170,"fisheye":0,"r_rmatrix":{"m43":0,"m21":0,"m44":1,"m22":1,"m23":0,"m24":0,"m31":1,"m32":0,"m33":6.123233995736766e-17,"m11":6.123233995736766e-17,"m34":0,"m12":0,"m41":0,"m13":-1,"m42":0,"m14":0},"r_pmatrix":{"m43":0,"m21":0,"m44":1,"m22":2.7474774194546225,"m23":0,"m24":0,"m31":0,"m32":0,"m33":1,"m11":2.7474774194546225,"m34":0,"m12":0,"m41":0,"m13":0,"m42":0,"m14":0},"r_aspect":4.16056338028169,"fovtype":"VFOV","r_znear":20,"r_yoff":0,"architectural":0,"r_fov":40,"stereographic":false,"vfovmaxlimit":179,"haschanged":false,"_rcmpz":false,"hlookat":0,"vfov":40,"architecturalonlymiddle":false,"vlookat":0,"camroll":0,"fov":40,"limitview":"auto","r_stereographic":false,"fisheyefovlink":0.5,"_defaultfisheye":0,"hfov":113.12149737730412,"isdynamic":false}"

    The fov is set to 40 in krpano :)

    There are lots of properties that get set, which wont need setting (perhaps I should create a whitelist).

    Im just experimenting. Cleanest way would ofcourse be updating the part you change instead of the whole object. But this is nice quick and dirty. Alsoo the special set function I created allows for nexted objects.

    btw what does r_ prefix mean? readonly? _ is private (although its not private through the js object)

    ow and if your curious for what this is... I'm atm experimenting in a javascript "framework", using BackboneJS and jquery. Backbone is a framework that helps communicating with a restfull backend, but could alsoo use JSON as backend. Basicly Im trying to make stuff easier while developing. For instance, I often use a krpano xml array to create galleries, thumbnails or a location state manager. With a javascript framework in the backend, this would be a lot easier and later potentially easier to integrate in a backend cms or even a local json file with hotspot information.

  • Quote

    Well actually it works through my custom function I posted above.

    yes, of course, just setting one property will work, I just want to explain that setting whole objects may not work

    btw what does r_ prefix mean? readonly? _ is private (although its not private through the js object)

    these are internal variables for the internal renderer - so 'r_' for renderer if you want ,

    as user you don't need to care about these internal variables and you should not use and especially not rely on them, they are internal and can change from version to version,

    best regards,
    Klaus

  • Would it then be handier if the external properties for user would be in a seperate scope?

    for instance

    etc

    getting the view, would only return the public attributes. But I reckon that would require a complete refactor of everything you made (and perhaps defining private internal classes)....

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!