-2

I am creating an object dynamically and would like to set a property (also an object) to be the value of a variable.

My code:

var guid, block, lot, muni, county, owner, prop_loc;

guid = 'Some unique value';
//code to set the other variable values here...

var menuItem = new MenuItem({
    id: 'basic',
    label: 'Report',
    onClick: lang.hitch(this, function () {
        topic.publish('basicReportWidget/reportFromIdentify', {
            guid : {  //set this to the guid string value
                block: block,
                lot: lot,
                muni: muni,
                county: county,
                owner: owner,
                prop_loc: prop_loc,
                type: 'basic'
            }
        });
    })
});
its30
  • 253
  • 3
  • 17
  • 1
    Read about [___`bracket notation`___](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Objects_and_properties) – Rayon Sep 21 '16 at 20:02

2 Answers2

0

If all of this is your code, it could be as simple as replacing the object with guid key to your guid variable, like this:

var guid, block, lot, muni, county, owner, prop_loc;

guid = 'Some unique value';
//code to set the other variable values here...

var menuItem = new MenuItem({
    id: 'basic',
    label: 'Report',
    onClick: lang.hitch(this, function () {
        topic.publish('basicReportWidget/reportFromIdentify', {
            guid : guid
        });
    })
});

Otherwise, if you can't modify that menuItem object for whatever reason, you could potentially overwrite the onClick function via something like:

menuItem.onClick = function() {
    lang.hitch(this, function () {
        topic.publish('basicReportWidget/reportFromIdentify', {
            guid : guid
        });
    };
Rebecca Close
  • 890
  • 7
  • 11
0

As Rayon said, You can use bracket notation to dynamically set object properties.

Something like this should work...

var guid, block, lot, muni, county, owner, prop_loc;

guid = 'Some unique value';

var menuItem = new MenuItem({
  id: 'basic',
  label: 'Report',
  onClick: lang.hitch(this, function () {
    var obj = {};
    obj[guid] = { 
      block: block,
      lot: lot,
      muni: muni,
      county: county,
      owner: owner,
      prop_loc: prop_loc,
      type: 'basic'
    };
    topic.publish('basicReportWidget/reportFromIdentify', obj);
  })
});
Tom Coughlin
  • 453
  • 3
  • 13