Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Note

Desk Phone API features described in this section are deprecated and supported only on the following models: D40, d45, d50, d60, d62, d65, d70

require

Used to include and evaluate other scripts. Anything exported by a required script is then made available using require.

...

The following example code includes the util javascript module, which makes all exported util methods then available, including util.isDef, util.forceArray, etc.

Code Block
var app = require('util');
  
var foo = util.isDef(bar);  //foo = false
exampleArray = util.forceArray('test');  //exampleArray = ['test']

Note: Required JavaScript files are referenced by their filename without the .js extension.

...

As an example, see the following app, which is split into three scripts: example1.js, example2.js, and example3.js. The following is its app.json file:

Code Block
{
    "name": "test",
    "jsFiles": ["example1.js"],
    "type": "foreground",
} 
  1. When the app is started, example1.js requires both example2.js and example3.js (and example3.js, in turn, also references example2.js). When example2.js is evaluated, its init() function assigns a value to the 'status' property of 'Status set by example2.init()'

  2. This value is saved to the variable 'foo' in example1.js

  3. example2.setStatus() is used to change the value of example2.status to 'Status reset by example1.js'

  4. When the 'bar' variable is set to the result of the example3.getStatus() function, you can see that the 'example2' object referenced in both example1.js and example3.js is actually the same object.

Code Block
var example2 = require('example2');
var example3 = require('example3');
  
var foo = example2.getStatus();  //foo = 'Status set by example2.init()'
example2.setStatus('Status reset by example1.js');
var bar = example3.getStatus();  //bar = 'Status reset by example1.js'

Code Block
var example2 = {};
example2.init = function () {
    this.status = 'Status set by example2.init()';
};
example2.getStatus = function () {
    return this.status;
};
example2.setStatus = function (param) {
    this.status = param;
};
  
example2.init();
exports.getStatus = example2.getStatus.bind(example2);
exports.setStatus = example2.setStatus.bind(example2);

Code Block
var example2 = require('example2');
var example3 = {};
  
example3.getStatus = function () {
    return example2.getStatus();
};
exports.getStatus = example3.getStatus.bind(example3);

Parameters

Name

Required

Type

Default

Description

JavaScript module

Yes

JavaScript module to include.