0

Sorry for the question but I am new to JavaScript i have defined an object

define(['sharedServices/pubsub', 'sharedServices/topics'], function (pubsub,topics) {
    'use strict';

    function Incident() {
        var that = this;
        this._dtasks = [];

        this.handlePropertyGet = function(enstate, ename) {
            if (!this.entityAspect || !this.entityAspect.entityManager || !this.entityAspect.entityManager.isReady) {
                enstate = [];
            } else {
                enstate = this.entityAspect.entityManager.executeQueryLocally(new breeze.EntityQuery(ename).where('IncidentID', 'eq', this.IncidentID));
            }

            return enstate;
        };

        Object.defineProperty(this, 'DTasks', {
            get: function () {
                return this.handlePropertyGet(this._dtasks, "DTasks");
            },
            set: function (value) { //used only when loading incidents from the server
                that.handlePropertySet('DTask', value);
            },
            enumerable: true
        });
    }

    return {
        Incident: Incident
    };
});

when I am calling the property DTasks the inner member _dtask is equal to [], even when i enter the get property and i see that the enstate is filled with the objects when the handlePropertyGet is finished and returned to the get scope the _dtasks remains empty, doesn't it supposed to pass as reference?

li-raz
  • 1,678
  • 2
  • 29
  • 57
  • Not sure why you start off declaring var that = this; . I don't see you using that variable? – DOK Feb 08 '14 at 22:45
  • Can you show how you are using your object? A jsFiddle would also be nice. Where is `handlePropertySet`? – Xotic750 Feb 08 '14 at 22:46
  • I have deleted most of the code (and the use of the that). the entity is updating it self when getting notifications, i dont under stand why when i pass the array to the function it isnt being updated when i assign it – li-raz Feb 08 '14 at 22:54

2 Answers2

0

this._dtasks "points" to an array. If you pass it as a parameter to this.handlePropertyGet, you make enstate refer to the same array.

If you change the array (as in enstate.push("bar")), the change affects this._dtasks too: you are actually changing neither of them, just the array they both point to.

However, the lines

enstate = []

and

enstate = this.entityAspect.entityManager.executeQueryLocally(new breeze.EntityQuery(ename).where('IncidentID', 'eq', this.IncidentID));

don't modify the array you already have. Instead, they create new arrays and change enstate so that it points to them. this._dtasks, however, remains unchanged.

An easy way to fix it would be to change the code inside the getter to

return this.handlePropertyGet("_dtasks", "DTasks");

and handlePropertyGet to

this.handlePropertyGet = function(enstate, ename) {
    if (!this.entityAspect || !this.entityAspect.entityManager || !this.entityAspect.entityManager.isReady) {
        this[enstate] = [];
    } else {
        this[enstate] = this.entityAspect.entityManager.executeQueryLocally(new breeze.EntityQuery(ename).where('IncidentID', 'eq', this.IncidentID));
    }
    return this[enstate];
};

That way, you would be changing the value of this._dtasks directly.

As an alternative, you could achieve the same result by changing enstate = [] to enstate.length = 0 (which clears the array instead of changing the variable. See https://stackoverflow.com/a/1232046/3191224) and enstate = this.entityAspect.[...] to

var newContent = enstate = this.entityAspect.entityManager.executeQueryLocally(new breeze.EntityQuery(ename).where('IncidentID', 'eq', this.IncidentID));
enstate.length = 0;
Array.prototype.push.apply(enstate, newContent);

which clears the array and then pushes all the elements from the other array, effectively replacing the whole content without changing enstate itself.

Community
  • 1
  • 1
0

My guess is that you are trying to do something like this

Javascript

function Incident() {
    var reset = false;

    this._dtasks = [];

    this.handlePropertyGet = function (ename) {
        if (reset) {
            this._dtasks = [];
        } else {
            this._dtasks = [1, 2, 3];
        }

        return this._dtasks;
    };

    Object.defineProperty(this, 'DTasks', {
        get: function () {
            return this.handlePropertyGet("DTasks");
        },

        enumerable: true
    });
}

var x = new Incident();

console.log(x.DTasks);

Output

[1, 2, 3] 

On jsFiddle

So you can then use this simplified example with the ideas given by @user3191224

Xotic750
  • 22,914
  • 8
  • 57
  • 79