SlightlyLoony
Tera Contributor
Options
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
04-15-2011
05:58 AM
This is the day our geekette at right has been waiting for: the day she finds out how to extend a JavaScript class on the Service-now platform. Woo hoo!
She want the following code (using our new method from yesterday) to work anywhere, anytime in her Service-now instance:
var gru = new GRUtil();
gs.log(gru.getChildTables('cmdb_ci_server'));
Here's how she (and you!) can do it:
Posts in this series:
A JavaScript Class to be Extended
Extending a Class, Part I...
Just add the getChildTables() method to the GRUtil script includes, as follows:
var GRUtil = Class.create();
GRUtil.prototype = {
initialize : function() {
},
/*
* Returns an array containing the component tables for the given table. For instance, given
* 'cmdb_ci_win_server', returns an array with 'cmdb_ci_win_server', 'cmdb_ci_server',
* 'cmdb_ci_computer', 'cmdb_ci_hardware', and 'cmdb_ci'.
*/
getTables: function(table) {
var om = Packages.com.glide.db.DBObjectManager.get();
var tables = om.getTables0(table);
return j2js(tables);
},
/*
* Returns an array containing any tables that extend the given table. For example, given
* 'cmdb_ci_netgear', returns an array with 'cmdb_ci_ip_switch' and 'cmdb_ci_ip_router'.
*/
getChildTables: function(table) {
var om = Packages.com.glide.db.DBObjectManager.get();
var tables = om.getTableExtensions(table);
return j2js(tables);
}
}
One thing to note carefully in the syntax of adding a method: there must be a comma (',') between method definitions. In the example above, I've followed the usual convention of putting the comma after the closing brace ('}') of the preceding method. Something that frequently trips people up is to habitually put a comma after every method's closing brace — but if you do this after the last method's closing brace, you'll get an error. Just remember that they go between method definitions (actually, every property definition of any type).
Also note the use of comments to describe what the method actually does. This is very polite and useful for others who might want to use your GRUtil class. If you're anything like me, it will also be useful for you at some distant future remove (like, say, tomorrow morning) when you've forgotten everything you did here.
Adding methods to class definitions in script includes is probably the single most common way for people to extend classes on the Service-now platform. For many situations, it's exactly the right solution. There are at least two situations, however, where it's exactly the wrong solution:
- Extending classes originally provided by Service-now: If you modify a script include that is provided by Service-now, then (just like any modification you might make in the product) that script include will effectively be "owned" by you, and you will not get future upgrades to that script include. This isn't necessarily the end of the world, but it does entail some risk — we might, in the future, release some code that depends on a new method in that script include, and if you don't get the update, you won't have the new method. Fortunately there's an easy way to avoid this, which I'll show you on Monday.
- Extending classes in a non-generic way: To make that concrete with an example, suppose we wanted to add a method to GRUtil that did something specific for computer CIs. Currently GRUtil is a completely generic class; it's two methods work with any table in the system. It would be strange to extend it to do something specific for computers. What you really want, if you're going to have a good object-oriented design, is a class that does everything that GRUtil does, plus some extra, special stuff just for computers. In object-oriented design speak, you want to extend the GRUtil class to make a specialized version of it just for computers. That's easy to do, and it happens to be the same thing you do to avoid the first problem above. I'll show you this secret code jujitsu on Monday.
1 Comment
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.