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.

6
  • At what point are you checking if it's set? Could be a dupe of this? Commented Jan 12, 2016 at 10:53
  • What is the API here? Commented Jan 12, 2016 at 10:54
  • i am checking after the callback method get's executed Commented Jan 12, 2016 at 10:54
  • you mean returnObj.summary = someObj ? Commented Jan 12, 2016 at 10:57
  • how returnObj is defined ? is it global ? Commented Jan 12, 2016 at 10:58

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

2 Comments

Thx a lot...But am getting "this" is undefined inside the callback
@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...
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.