-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api] First pass at adding ability to specify custom resource methods. …
…#116 Examples working. Needs tests.
- Loading branch information
Showing
4 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,60 @@ | |||
// | |||
// custom-methods.js - example of extending resources to have methods | |||
// | |||
|
|||
var resourceful = require('../lib/resourceful'); | |||
resourceful.use('memory'); | |||
|
|||
var Creature = resourceful.define('creature'); | |||
|
|||
var lazer = function(options){ | |||
console.log('firing mah lazar @ ' + options.powa); | |||
}; | |||
|
|||
// | |||
// Resource.method takes three arguments: | |||
// | |||
// name, Function, schema | |||
// | |||
// The schema represents an optional JSON-Schema, | |||
// which if present, is enforced on the Function's arguments | |||
// | |||
// | |||
Creature.method('fire', lazer, { properties: { | |||
"powa": { | |||
"type": "string", | |||
"enum": ["high", "low", "med", "SUPA POWA"], | |||
"required": true | |||
} | |||
}}); | |||
|
|||
Creature.fire({ powa: "med"}); | |||
Creature.fire({ powa: "TOO HIGH"}, function(err, result){ | |||
// errors since TOO HIGH is not in enum | |||
console.log(err, result); | |||
}); | |||
|
|||
// throws validation error | |||
// Creature.fire({ powa: "TOO HIGH"}); | |||
|
|||
Creature.create({ | |||
id: 'Marak' | |||
}, function(err, marak){ | |||
if(err){ | |||
console.log(err.validate) | |||
|
|||
} | |||
marak.fire({ powa: "med"}); | |||
|
|||
// throws validation error | |||
// marak.fire({ powa: "TOO HIGH"}); | |||
|
|||
marak.fire({ powa: "low"}, function(err, result){ | |||
console.log(err, result); | |||
}); | |||
|
|||
marak.fire({ powa: "TOO HIGH"}, function(err, result){ | |||
// errors since TOO HIGH is not in enum | |||
console.log(err, result); | |||
}); | |||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters