Yaser Moradi
Yaser Moradi

Reputation: 3327

What's a best practice for using custom entities in jayData?

I've a customized mechanism to define classes in JavaScript as below:

Tools.User = Tools.Class.define("Tools.User", Tools.Entity, {
    Id: { type: "int", key: true, computed: true },
    IsActive: { type: Boolean },
    IsAdmin: { type: Boolean },
    LName: { type: String },
    FName: { type: String },
    FullName: { get: function () { return this.FName + " " + this.LName; }, Type: String },
    init: function (name, lname) {
        this.IsAdmin = false;
        this.IsActive = true;
        this.FName = name;
        this.LName = lname;
        Tools.Entity.call(this, arguments);
    },
    onEndEdit: function () {

        if (this.IsActive == false && this.IsAdmin == true) {
            throw new Error(Messages.AdminCanNotBeDisabled);
        }

        this.parentClass.onEndEdit();

    },
    deActivate: function () {

        if (this.IsAdmin == true) {
            throw new Error(Messages.AdminCanNotBeDisabled);
        }

        this.IsActive = false;
    }
});

As you can see, it supports virtual/override, auto implemented properties, constructor and so on.

I want to use my own classes in JayData. To do that I developed this lines:

$data.EntityContext.extend("Tools.AppDb", {
    Users: { type: $data.EntitySet, elementType: Tools.User }
});

var appDb = new Tools.AppDb({
    provider: 'indexedDb', databaseName: 'AppDb'
});

appDb.onReady(function () {

});

It's clear that it's impossible to work with Jaydata in this way. But how can I achieve this goal with minimum changes in my codes ?

I think that error message of this is not useful but this is an error message:

Uncaught TypeError: Cannot call method 'getPublicMappedProperties' of undefined 

Upvotes: 1

Views: 266

Answers (1)

Robesz
Robesz

Reputation: 1646

JayData can operate on JayData entities in order to track changes and make the storage-independent data management possible.

So the elementType of the Users EntitySet should be derived from $data.Entity:

$data.Entity.extend("Todo", {
    Id: { type: "int", key: true, computed: true },
    Task: { type: String, required: true, maxLength: 200 },
    DueDate: { type: Date },
    Completed: { type: Boolean }
});

Upvotes: 1

Related Questions