2

I'm trying to make my own model but I don't know how.

I've been looking at the ListControllerWithObjects demo but I can only see:

var person = new demobrowser.demo.data.model.Person(); 

but don't show where it comes from or how is made. So this tutorial is not useful.

Why I want a custom model? I want to have custom class just for the model, to know the structure and to put custom methods in it.

I have: [{a: 2, b: 4}, {a: 1, b: 9}];

And I want to put it on a list, but using this

var model = this._model = new qx.data.Array([{id: 1, name: "Victor"}]);    
this._listController = new qx.data.controller.List(model, this._list, 'name');

doesn't work. The real error is SingleValueBinding:

"Binding property " + propertyname + " of object " + source + " not possible: No event available. ";

It cannot find the event.

The error in the console:

Uncaught qx.core.AssertionError: error

Bart Jolling
  • 595
  • 6
  • 21
Totty.js
  • 15,563
  • 31
  • 103
  • 175
  • 1
    Cross-post from the qooxdoo mailing list, http://qooxdoo.678.n2.nabble.com/How-to-create-my-own-model-I-cannot-find-tp7357709p7357709.html – ThomasH Mar 10 '12 at 00:01

2 Answers2

4

Use qx.data.marshal.Json.createModel

-> Your code should look like this:

var model = this._model = qx.data.marshal.Json.createModel([{id: 1, name: "Victor"}]);    
this._listController = new qx.data.controller.List(model, this._list, 'name');

-> Mapping two model elements to the same property ("a" and "secondA"):

var data = [{a: 2, b: 4}, {secondA: 1, b: 9}];
var delegate = {
    getPropertyMapping : function(property, properties) {
        if (property === "secondA") {
            return "a";
        }

        return property;
    }
};

var marshaler = new qx.data.marshal.Json(delegate);
marshaler.toClass(data);
var model = marshaler.toModel(data);

this.assertEquals("2", model.toArray()[0].getA());
this.assertEquals("1", model.toArray()[1].getA());

Take at look at my Playground example or the unit test of the JSON marshaler (search for "testGetPropertyMapping")

Sandro
  • 491
  • 7
  • 20
  • 1
    I don't understand how qooxdoo attributes a name to the model based on the model you gived in the example. What if I had 2 models with the same data but different names? how would it map them? strange, but thanks for your answer – Totty.js May 25 '12 at 10:51
  • 1
    The name of the model is based on the hash of the model keys. You can make use of the http://demo.qooxdoo.org/current/apiviewer/#qx.data.marshal.IMarshalerDelegate to set an explicit model class based on the given properties (IMarshalerDelegate#getModelClass()). – Sandro May 25 '12 at 12:30
  • can you give an example of how to really do, from the api I can't figure out how.. thanks ;) – Totty.js Jul 16 '12 at 08:01
  • 1
    @Totty I've updated my answer. Note, I didn't test the mapping stuff as I've no qx environment atm so it might need some tweaking ;-) but you should get the basic idea.. – Sandro Jul 17 '12 at 10:15
  • @Totty this playground example works as a charm (you have to open the "Log" pane, in order to see the output): http://goo.gl/QYqgC – Sandro Jul 17 '12 at 11:25
1

The answer to the first question is here:

https://github.com/qooxdoo/qxl.demobrowser/blob/master/source/class/qxl/demobrowser/demo/data/model/Person.js

This file contains the definition of the missing Person class

Bart Jolling
  • 595
  • 6
  • 21
  • Thanks... I can't confirm that it works, because I'm leaving qooxdoo, but I will accept this answer as it was what I wanted at the time. – Totty.js Jul 15 '13 at 08:21