範例:建立及刪除映像實體
下列範例會導致建立「映像物件」:
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;
使用 try-catch 機制則可刪除映像實體:
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);
}