Totty.js
Totty.js

Reputation: 15831

How to create a custom Model in Qooxdoo? (for a list)

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

Upvotes: 2

Views: 1743

Answers (2)

Bart Jolling
Bart Jolling

Reputation: 675

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

Upvotes: 1

Sandro
Sandro

Reputation: 491

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")

Upvotes: 4

Related Questions