The following are frequently used with the codebase, for manipulation
of JSON data and objects:
-------------


$.extend(

{ a: 1, b: 2, c: 3 }
,

{ d: 4 }
, n...)

-Merges the contents of two or more objects together

-Often used to override or add to the structure of existing CloudStack
 objects, such as the context object, list views, etc. It can be used
 to create pseudo-'inheritance'

-Most of the time, you want to do a 'deep copy' so that an entirely
 new array is formed, avoiding altering existing objects, thus
 breaking state.

 This is done by adding 'true, {}' as the first two arguments:

$.extend(true, {}, cloudstack.someObject,

{ a: 1, b: 2 }
)

-------------


$.map(array, function(item) {
  return

{ fieldA: item.fieldA, fieldB: item.fieldB }
;
});

-Used to transform the contents of array items

-Useful when the API returns something, but the UI widgets accept it
 in a different format. This is used frequently when getting contents
 for <select>s on input forms:

...

select: function(args) {
  $.ajax({
     url: createURL('listZones'),
     ...,
     success: function(json) {
       args.response.success({
         data: $.map(json.listzonesresponse.zone, function(zone) {
           return

{              id: zone.id,              description: zone.name            }
;
         })
       })
     }
  });
},

--Here, the UI's select generator wants an array of objects with an id
  and description field. Because some objects from the API do not have
  these exact names, you want to to a $.map to only return what the
  widget needs.
.


  • No labels