4

I am trying to set an object (summary) inside another object's callback method

returnObj.beforeLoadComplete = function (records) {
    var someObj = {
        total: {
            label: 'Items',
            value: '15'
        },
        additional: [{
            label: 'Item1 Total',
            value: '25000'
        }]
    };

    returnObj.summary = summaryObj;
    // some other code which finally returns an object
}

The above code does not work (i.e. summary is not set on returnObj)

However if I have the same code outside the callback method, it works as in code snippet below:

var someObj = {
    total: {
        label: 'Items',
        value: '15'
    },
    additional: [{
        label: 'Item1 Total',
        value: '25000'
    }]
};

returnObj.summary = summaryObj;
returnObj.beforeLoadComplete = function (records) {
    // some other code which finally returns an object
}

Not sure why is it so.

Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
copenndthagen
  • 49,230
  • 102
  • 290
  • 442

2 Answers2

5

You have to access your object with this statement, also i've correct some typo:

var returnObj = {};
returnObj.beforeLoadComplete = function (records) {
    var someObj = {
        total: {
            label: 'Items',
            value: '15'
        },
        additional: [{
            label: 'Item1 Total',
            value: '25000'
        }]
    };
    // Access object with this
    this.summary = someObj;
    // some other code which finally returns an object
}
returnObj.beforeLoadComplete('records');
console.log(returnObj.summary);

Update: Added code snippet to verify that returnObj could be accessed via this in callback handler.

var returnObj = {};
returnObj.beforeLoadComplete = function () {
  var someObj = {
    total: {
      label: "Items",
      value: "15"
    },
    additional: [{
      label: 'Item1 Total',
      value: '25000'
    }]
  };
  this.summary = someObj;
  // some other code which finally returns an object
}
//returnObj.beforeLoadComplete();

function verifyObjectUpdated(){
   alert(returnObj.summary);
}
<select onChange="returnObj.beforeLoadComplete()">
  <option>Trigger onChange to add summary to your returnObj</option>
  <option>Trigger onChange to add summary to your returnObj</option>
</select>

<select onChange="verifyObjectUpdated()">
  <option>Trigger onChange to alert summary of returnObj ( do it after adding summary)</option>
  <option>Trigger onChange to alert summary of returnObj ( do it after adding summary)</option>
</select>
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
  • Thx a lot...But am getting "this" is undefined inside the callback – copenndthagen Jan 12 '16 at 11:06
  • @testndtv i've added working snippets with alerts. See value of select, they describe action ... Also you could play with alert to display some particular properties of your summary object... – Andriy Ivaneyko Jan 12 '16 at 11:16
1

Just use this inside your object:

var res = {
  foo: 'bar',
  setSmth: function(data) {
    this.summary = data
  }
}

res.setSmth({bar: 'foo'})

console.log(res.summary)

See jsfiddle

soyuka
  • 8,839
  • 3
  • 39
  • 54