https://github.com/zweifisch/invoker
A CoffeeScript library for Client/Server interaction
https://github.com/zweifisch/invoker
Last synced: 4 months ago
JSON representation
A CoffeeScript library for Client/Server interaction
- Host: GitHub
- URL: https://github.com/zweifisch/invoker
- Owner: zweifisch
- Created: 2012-11-16T07:46:21.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2012-11-24T11:58:08.000Z (over 12 years ago)
- Last Synced: 2023-04-09T21:22:23.701Z (about 2 years ago)
- Language: JavaScript
- Size: 188 KB
- Stars: 4
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Invoker
A CoffeeScript library for Client/Server interaction, inspired by [fetch](https://github.com/ibdknox/fetch)
## Usage
### client side
```html
```
```coffeescript
{batch} = invoker
{Utils,User} = invoker.classes
# Utils and User are classes generated in the included 'scripts'
# mapped to server side classes# calling server side static method
Utils.add(1,2) (result)-> # result.should.equal 3# calling multiple methods in one request
batch (done)->
User.listUsers() (users)->
# users.should.have.length 0
user = new User 'foo'
user.save()
User.listUsers() (users)->
# users.should.have.length 1
```javascript version:
```javascript
var batch = invoker.batch;
Utils = invoker.classes.Utils,
User = invoker.classes.User,
Utils.add(1,2)(function(result){
// result.should.equla(3)
});
batch(function(done){
User.listUser()(function(users){
// user.should.have.length(0)
});
user = new User('foo');
user.save();
User.listUsers()(function(users){
// user.should.have.length(1)
});
});
```### server side
more details see [test/index.php](https://github.com/zweifisch/invoker/blob/master/test/index.php) and [the php backend](https://github.com/zweifisch/Invoker-php)
the php classes can be found [here](https://github.com/zweifisch/invoker/tree/master/test/php)```php
require 'vendor/autoload.php';allowedMethods = array(
'Utils'=>'*',
'User'=>'*',
'Path'=>array('pwd'),
);
# Utils,User,Path must be available$server = new Invoker\Server(allowedMethods);
$server->listen();
```## more on client side
one line version
```coffeescript
invoker.invoke ['Utils','add',[1,2]], (result)-> console.log result is 3
```call multiple methods in one ajax request
```coffeescript
invoker.batch (done)->
utils.add(1,2) (sum)->
console.log sum is 3
utils.add(2,3) (sum)->
console.log sum is 5
path.pwd() (pwd)->
console.log pwd
done ->
console.log 'done'
```abort an invocation
```coffeescript
invocation = batch ->
utils.add (1,2) (sum)->
invocation.abort()
```handle errors
```coffeescript
Utils.add(1,2)
success:(result)->
error:(result,code)->
batch (done,onError)->
Path.scanDir() ->
# won't be reached
Utils.add(1,1) ->
# won't be reached
done ->
# won't be reached
onError (response,code)->
console.log code > 200
```
### intergration with other frameworkTBD
## test
```sh
cd test
composer install
php -S localhost:1212 index.php
```visit [http://localhost:1212](http://localhost:1212)