Exemple : création et suppression d'entités fantômes
L'exemple suivant entraîne la création d'un objet fantôme :
var params = {
name: "GhostThing" /* STRING */,
description: "Ghost" /* STRING */,
thingTemplateName: "GenericThing" /* THINGTEMPLATENAME */
};
// Successfully creates the GhostThing entity.
Resources["EntityServices"].CreateThing(params);
// Code that will cause an exception to be thrown by the service.
var makeError;
// Exception is thrown as the notThere property does not exist
on the makeError variable at this point.
makeError.notThere = 1;
Utilisez à la place le mécanisme try-catchpour supprimer des entités fantômes
try {
var params = {
name: "GhostThing" /* STRING */,
description: "Ghost" /* STRING */,
thingTemplateName: "GenericThing" /* THINGTEMPLATENAME */
};
Resources["EntityServices"].CreateThing(params);
// Any code that could potentially cause an exception should
// be included in the try-catch block.
// The following code will cause an exception to be thrown
// as makeError.notThere has not been defined.
var makeError;
makeError.notThere = 1;
} catch (err) {
// If an exception is caught, we need to delete the entity
// that was created to prevent it from becoming a Ghost Entity
var params = {
name: "GhostThing" /* THINGNAME */
};
Resources["EntityServices"].DeleteThing(params);
}
 :