View Full Version : Creating datasets into classes
this what i wanna do: i want to create a temporary XML file into a class instance
since i cannot put any dataset tag inside a class and "new LzDataset()" doesn't seem to work, i'm currently adding a temporary node to the class dataset (set through 'datapath') but i really don't like this solution
any idea to have it work without the trick i'm using ?
vfunshteyn
05-03-2004, 02:03 PM
Instantiating LzDataset programmatically is probably what you want. For that to work, you need to instantiate a Datasource object inside your class, then pass it to the constructor for LzDataset, like this:
var dsrc = new LzDatasource(canvas, {name: 'myDatasource'});
this.dset = new LzDataset(dsrc, {name: 'mydata'});
Then you can obtain a datapointer to the dataset's top by calling this.dset.getPointer() and invoke the regular datapointer API methods on it to populate your temp dataset with data, such as addNode().
One caveat is that after you are done with the dataset, it will remain defined in your application, because datasets are always created at the canvas level.
i really wonder why datasets are bound to canvas level...
inside a class you may want to create a temporary 'XML data' -for example for display purposes- and so you should be allowed to create a new dataset bound to the instance of your class
any clue?
We would like to allow datasets within a view or class definition in the future.
metasarah
05-04-2004, 02:14 PM
Actually you can create a dataset without a datasource -- just pass null as the parent:
var dset = new LzDataset(null, {name: 'mydata'});
** i've created an attribute 'dset' in my class
** on init, i initialize dset as a dataset with name dsett
** i call a method which create nodes inside the dataset
now how can i refer to my dataset dset when i wanna add a subview whose datapath is dset ? i can't find the correct syntax...
metasarah
05-05-2004, 02:38 PM
Its declared globally. Try typing 'dsett' or 'dsett.serialize()' into the debugger to test this.
This is exactly what i do right now:
<method event="oninit">
var tmpgroup = new LzDataset(null,{name: 'tempgroup'});
var tmp=tmpgroup.getPointer();
tmp.addNode('toto',{id: 'My groups'});
Debug.write(tmpgroup.serialize());
</method>
and this is the result i obtain:
couldn't find dataset for tempgroup:/
<tempgroup><toto>[object Object]</toto></tempgroup>
i tried to replace tempgroup:/ by tmpgroup:/ in the datapath of my subview but with no success
can somebody explain me why i can use the dataset i created only in the 'oninit' method, and not elsewhere?
thx
vfunshteyn
05-06-2004, 10:57 AM
Try referencing your dataset using a fully qualified name, i.e. canvas.datasets['tempgroup'], or canvas.tempgroup. This should work from anywhere in your application.
BTW, the second line of the output you posted looks exactly like what you would expect to obtain by serializing the dataset.
and so you would write:
<view datapath="${canvas.datasets['temprgoup']+':/*'}"/>
to designate a view's datapath?
vfunshteyn
05-06-2004, 12:59 PM
Originally posted by 3.14
and so you would write:
<view datapath="${canvas.datasets['temprgoup']+':/*'}"/>
to designate a view's datapath?
This unfortunately won't work as expected due to the fact that datapaths currently need to be valid initially; if you instantiate the dataset at runtime, the constraint will not be valid until it exists and databinding will not take place (this is a known bug).
However, you should be able to set it later by calling setDatapath('tempgroup:/*') on the view itself.
metasarah
05-06-2004, 03:59 PM
Originally posted by 3.14
This is exactly what i do right now:
<method event="oninit">
var tmpgroup = new LzDataset(null,{name: 'tempgroup'});
var tmp=tmpgroup.getPointer();
tmp.addNode('toto',{id: 'My groups'});
Debug.write(tmpgroup.serialize());
</method>
and this is the result i obtain:
couldn't find dataset for tempgroup:/
<tempgroup><toto>[object Object]</toto></tempgroup>
To clarify: the above code is working correctly. The error 'couldn't find dataset for tempgroup:/' must come from a view that is declared like this:
<view datapath="tempgroup:/" />
When that view is created, there is no datapath named 'tempgroup', so you get a debugger warning and no dataset is bound to the view.
From the original question, I would guess that the datapath is set on a class. To correct this problem, you could write code like this:
<class name="foo">
<method event="oninit">
var tmpgroup = new LzDataset(null,{name: 'tempgroup'});
var tmp=tmpgroup.getPointer();
tmp.addNode('toto',{id: 'My groups'});
Debug.write(tmpgroup.serialize());
this.setDatapath('tempgroup:/');
</method>
</class>
Sarah
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.