cancel
Showing results for 
Search instead for 
Did you mean: 

Extending the Javascript library [Solved]

michaelc
Champ on-the-rise
Champ on-the-rise
This should be simple but I don't know how.

From my back end Javascript module I want to marshal and unmarshal javascript objects.

here is the very simple methods
  http://anaykamat.com/2008/05/08/using-json-in-alfresco-webscripts/

  but how can I put this into the frame work so that I don't have to include into every module that might use it ?
  if this was the front end I would include into my framework package and include that into the page.

   but I don't know how you include in back end Javascript.
2 REPLIES 2

michaelc
Champ on-the-rise
Champ on-the-rise
Extending is simple and defined in the API Cookbook.
http://wiki.alfresco.com/wiki/JavaScript_API_Cookbook

<import resource="classpath:alfresco/extension/myutils.js">
      this would equate to shared/classes/alfresco/extension/myutils.js

<import resource="/Company Home/Data Dictionary/Scripts/mylib.js">

michaelc
Champ on-the-rise
Champ on-the-rise
One last item - I found issues with the original json module, So I used one I found on http://www.sitepoint.com/javascript-json-serialization/ seems to have no issues.

/**
* Implements JSON stringify and parse functions
* v1.0
*
* By Craig Buckler, Optimalworks.net
*
* As featured on SitePoint.com
* Please use as you wish at your own risk.
*
* Usage:
*
* // serialize a JavaScript object to a JSON string
* var str = JSON.stringify(object);
*
* // de-serialize a JSON string to a JavaScript object
* var obj = JSON.parse(str);
*/

var JSON = JSON || {};

// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function (obj) {

   var t = typeof (obj);
   if (t != "object" || obj === null) {

      // simple data type
      if (t == "string") obj = '"'+obj+'"';
      return String(obj);

   }
   else {

      // recurse array or object
      var n, v, json = [], arr = (obj && obj.constructor == Array);

      for (n in obj) {
         v = obj[n]; t = typeof(v);

         if (t == "string") v = '"'+v+'"';
         else if (t == "object" && v !== null) v = JSON.stringify(v);

         json.push((arr ? "" : '"' + n + '":') + String(v));
      }

      return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
   }
};


// implement JSON.parse de-serialization
JSON.parse = JSON.parse || function (str) {
   if (str === "") str = '""';
   eval("var p=" + str + ";");
   return p;
};