-2

I have javascript function object EmployeeClass. Field of this objects are mapped by hard-coding attributes.

An event is bind on jqgrid jqGridAddEditAfterSubmit. Inside this function i am able to read json string object presenting a row being updated/added. The json string is represented something like following:

{
"Id":"xxx",
"FirstName":"Mr. B",
"LastName":"Ross",
...

}

Function object definition:

//EMPLOYEE 
function EmployeeClass(empJson){
    this.Id = empJson.Id;
    this.FirstName = empJson.FirstName;
    this.LastName = empJson.LastName;
    ...
    ...

}

The sample of code used inside binding function is following:

$("gridid").bind("jqGridAddEditAfterSubmit", function (e, rowid, event) {
   var rowData = ...
   ..
   ..
   //construct object for server
   var oObject = new EmployeeClass(rowData);

   //CALL SERVER SIDE API for store
   SERVERREMOTE.call(oOject,'saveEmployee',...);
});

Question

Above code works great if no fields are added dynamically. Meaning, serverside can add custom fields and those fields are dynamically added into jqgrid, which manages display/edit/add functionally beautifully. But, my issue is now, how to submit my EmployeeClass object to server with dynamically added attributes? I need some how extend my EmployeeClass inside my binding function jqGridAddEditAfterSubmit so that i capture all the attributes passed by jqgrid and transform to EmployeeClass

Hope this scenario explains my issue. It is all about extending javascript function object dynamically at run-time.

ANSWER

https://jsfiddle.net/SalesforceDev/cwaek2Ly/

Oleg
  • 220,925
  • 34
  • 403
  • 798
dev.sforce
  • 39
  • 1
  • 7
  • 2
    there's no such thing as a *"json object"* – JSON is *always* a string, or it is not JSON. – Mulan Mar 13 '17 at 04:07
  • Did you understand my question? Without understanding the question how can the high reputation person can do down vote without valid reason? – dev.sforce Mar 13 '17 at 04:13
  • I don't understand your question, nor is it a [minimum, complete, verifiable example](http://stackoverflow.com/help/mcve) – so I down-voted it and voted to close it as "unclear what you're asking" because ... it's unclear what you're asking. My reputation doesn't have anything to do with it. – Mulan Mar 13 '17 at 04:14
  • In that case you should have requested more detail explanation. – dev.sforce Mar 13 '17 at 04:20
  • It's actually not my responsibility to teach you [how to ask a good question](http://stackoverflow.com/help/how-to-ask) – Related: [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) is explaining your problem in great detail to a rubber duck. The idea is that the act of explaining your problem completely and thoroughly will often times solve the problem. Your question uses incorrect terminology, incomplete code pastes, poor formatting, and spelling mistakes. You demonstrate minimal effort to solicit help, but expect a quality answer. I'm disappointed. – Mulan Mar 13 '17 at 04:21
  • Possible duplicate of [Iterate through object properties](http://stackoverflow.com/questions/8312459/iterate-through-object-properties) – t.niese Mar 13 '17 at 04:26
  • If the question really just about iterating over the properties, then look at the duplicate I linked to. If not then you need to clarify the question. If you have your data as JSON representation (all data encoded in one string) then you have to parse it into a javascript Object first using `JSON.parse`. – t.niese Mar 13 '17 at 04:30
  • @dev.sforce: You wrote that you extend some the data, which jqGrid sends to the server. What exactly you need? – Oleg Mar 13 '17 at 06:25
  • Hi Oleg, My code is binding an event 'after save' with jqgrid. In this event function i am making server side 'Salesforce' object call. This API needs exact name of the pojo match with apex class name. Jqgrid provides me all the fields but i have to dynamically construct this 'Function' object that is mapped to serverside class. – dev.sforce Mar 13 '17 at 15:50

1 Answers1

0

based on new fields present into JSON string, I need to add corresponding fields into my function object

Use something like var keys = Object.keys(empJson) to get the keys from the JSON data as an array of symbols (strings).

Then use a loop to check whether your function has those keys using .hasOwnProperty(keys[x]).

Missing properties can easily be added with something like emp[keys[x]] = empJson[keys[x]] where x is the index of the new key. Note that keys[x] is just a string.

david25272
  • 976
  • 6
  • 12
  • `[...]to get the keys from the JSON data[...]` If `empJson` is really JSON then it has to be parsed first using `JSON.parse(empJson)` to get a regular JavaScript Object on which you can call `Object.keys`. If `empJSON` is just a regular JavaScript Object, then it is not JSON and `Object.keys(empJson)` can be used directly, but then the naming of JSON is wrong. – t.niese Mar 13 '17 at 05:02
  • In this case jqGrid should have handled the parsing, assuming the correct `datatype:` option was specified. – david25272 Mar 13 '17 at 05:25
  • That might be true. But the wording `[...]to get the keys from the JSON data[...]` in combination with `Object.keys(empJson)` is wrong. If `empJson` really contains JSON data then it would not return the keys of the object encoded as JSON, because JSON data is a string. Either it is a JavaScript Object, then `Object.keys(empJson)` will work, or it is JSON and then it has to be parsed. – t.niese Mar 13 '17 at 05:28
  • While you are quite correct, I am assuming that what dev.sforce, _meant_ by "JSON object" was an object created from JSON data. Ironically, Javascript is about the only language these days that doesn't have a JSONObject class (since it doesn't need one). – david25272 Mar 13 '17 at 05:42
  • I'm also pretty sure that OP is talking about a JavaScript object. Nevertheless mixing up json encoded data and javascript object is often the root of the problem for many questions here on SO that are about JSON. And if a JS documentation talks about JSON data then it will always be a string. So if you write an answer you always have to use the right words. – t.niese Mar 13 '17 at 05:54