0
votes

In XPages we write Javascript in server side. We use "the same" client side language. However in client side we can use a lot of cool libraries like jQuery, Dojo and Underscore.

I've used server side custom functions like, the each method:

Object.prototype.each = function (callback) {
    for (var x in this) {
        if (this.hasOwnProperty(x)) {
            callback.call(this, this[x]);
        }
    }
}

But these client side libraries already provide helpers like this and a lot more. If I tried to use these libraries in server side XPages they fail for several reasons like:

  • Dom manipulation
  • Global variables
  • Window object
  • The SSJS XPage wierd error: Error calling method 'call(Object)' on an object of type 'function [JavaScript Object]'

Anybody has a server side library like that? Maybe frontend Node.js developers has already solved this.

Anybody uses methods like:

  • each
  • map
  • reduce
  • reduceRight
  • find
  • filter
  • where
  • findWhere
  • reject
  • every
  • some
  • contains
  • invoke
  • pluck
  • max
  • min
  • sortBy
  • groupBy
  • indexBy
  • countBy
  • shuffle
  • sample
  • toArray
  • size
  • first
  • initial
  • last
  • rest
  • compact
  • flatten
  • without
  • union
  • intersection
  • difference
  • uniq
  • zip
  • object
  • indexOf
  • lastIndexOf
  • sortedIndex
  • range
  • bind
  • bindAll
  • partial
  • memoize
  • delay
  • defer
  • throttle
  • debounce
  • once
  • after
  • wrap
  • compose

These are some Underscore features. I found this link about Underscore in server side js but I failed with the mentioned SSJS XPage wierd error: Error calling method 'call(Object)' on an object of type 'function [JavaScript Object]'

2
Mootools would be cool also: David Walsh Mootools in Node.js. But I'm afraid it would be different in XPages.Johann Echavarria
JavaScript libraries tend to serve two purposes: fixing the deficiencies of, and discrepancies between, browsers; and streamlining implementation of complex behavior via widgets and convenience functions like those you mentioned. The first half is therefore useless server-side. In XPages, widgets are generally provided via components to minimize the need for client-side JavaScript. If you're writing complex behavioral code, it should be written in Java -- for the sake of runtime performance as well as long-term maintainability.Tim Tripcony
Interesting. Your point about better performance in Java is very good. Still I think it would be useful to have a ssjs lib because a lot of features are simpler in js than Java and a lot of times a few ms of performance isn't an issue.Johann Echavarria

2 Answers

5
votes

SSJS can use Java objects, which opens a big world of server-side libraries. See SSJS to call a method in java class (in java library) for an example.

One such library is Google's Guava https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained.

0
votes

My answer after 5 months: this is a SSJS library I'm using right now: polyfill.js

It polyfills the following JavaScript methods for the old Domino 8.5.3's JavaScript: (I don't know the javascript XSP version for IBM Notes 9)

  • Array.isArray
  • Array.prototype.indexOf
  • Array.prototype.lastIndexOf
  • Array.prototype.forEach
  • Array.prototype.some
  • Array.prototype.every
  • Array.prototype.map
  • Array.prototype.reduce
  • Array.prototype.reduceRight
  • Array.prototype.filter
  • Function.prototype.bind
  • String.prototype.trim
  • Object.keys

Collection of JavaScript polyfills of MDN (Mozilla Developer Network) https://developer.mozilla.org. Polyfill definition in MDN (https://developer.mozilla.org/en-US/docs/Web/Guide/Terminology) "A polyfill is a piece of code that implements a common API that isn't natively supported by a browser". This file can be used in XPages SSJS (Server Side JavaScript) in order to fill holes in Domino 8.5.3 JavaScript. This file can be used also in client side in order to fill js methods for older browsers: IE9, IE8, IE7. If you use this file in client side remove the code from line 400. I use the $.each function in SSJS XPages in a very similar way than jQuery.each() http://api.jquery.com/jquery.each/. I use it as an iterator even with Java objects like java.util.Vector.

Edit 1: Warning! right now (2014/11/09) there is a huge bug with prototype in XPages SSJS (at least in Domino 8.5.3 and 9): How to clean SSJS in Domino server after someone used javascript prototype in a nsf?