Skip to content

Commit

Permalink
Merge pull request #15 from janhommes/v0.2.0
Browse files Browse the repository at this point in the history
V0.2.0
  • Loading branch information
janhommes committed Feb 3, 2016
2 parents bd6dd94 + 66bae5d commit 2e875ee
Show file tree
Hide file tree
Showing 12 changed files with 513 additions and 159 deletions.
52 changes: 28 additions & 24 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# =========================
# Operating System Files
# =========================

# OSX
# =========================

# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# =========================
# Operating System Files
# =========================

# OSX
# =========================

.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon
Icon


# Thumbnails
._*
Expand All @@ -41,3 +42,6 @@ Icon
Network Trash Folder
Temporary Items
.apdisk

#nodemodules
node_modules
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
node_js:
- "4.1"
- "4.0"
- "0.12"
- "0.11"
- "0.10"
- "iojs"
83 changes: 45 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# o.js
*o.js beta v0.1.1*
*o.js beta v0.2.0*

o.js is a client side Odata Javascript library to simplify the request of data. The main goal is to build a **standalone, lightweight and easy** to understand Odata lib.
o.js is a client side Odata Javascript library to simplify the request of data. The main goal is to build a **standalone, lightweight and easy** to understand Odata lib.

## Install ##
------------
Expand All @@ -11,17 +11,24 @@ Download the *o.js* or *o.min.js* file or install it via *bower* and add the scr
bower install o.js
```

You can use o.js in node.js as well:
```
npm install o.js
//var o = require('o.js');
```

## Samples ##
------------
For alle samples we are using the test odata service from <a href="http://www.odata.org">Odata.org</a>. You can find the metadata of this service <a href="http://services.odata.org/V4/OData/OData.svc">here</a>.
For alle samples we are using the test odata service from <a href="http://www.odata.org">Odata.org</a>. You can find the metadata of this service <a href="http://services.odata.org/V4/OData/OData.svc">here</a>.

### Simple Odata query with o.js ###
----------------------
```js
o('http://services.odata.org/V4/OData/OData.svc/Products').get(function(data) {
console.log(data); //returns an array of Product data
});
```
```
o.js uses a jQuery like syntax to determine which resource you want to access. You can define any Odata service url (or any json web service) in the o-function: `o('<your odata service resource>')`. This only holds a handler to this resource and dosn't start the ajax call. If you want to get the resource, you need to call `.get()`. Get accepts a function callback which contains the data as the first parameter.

### Methods ###
Expand All @@ -31,7 +38,7 @@ By adding some chained functions to the o-handler you can add query options:
o('http://services.odata.org/V4/OData/OData.svc/Products').take(5).skip(2).get(function(data) {
console.log(data); //An array of 5 products skiped by 2
});
```
```

### Routing ###
--------
Expand All @@ -42,11 +49,11 @@ You can use hash routes to map your Odata service endpoint to your website:
console.log(data);
});
```
Instead of manual getting your data with the `get()` function, this routing function always returns the data when somebody navigates to an URL with the hash value `index.html#Product/Detail/1/Some more parameter`. The `find()` method automatically maps the right parameter (in this example *1*). <a href="https://github.com/janhommes/o.js/tree/master/example">See this</a> demonstration for more examples.
Instead of manual getting your data with the `get()` function, this routing function always returns the data when somebody navigates to an URL with the hash value `index.html#Product/Detail/1/Some more parameter`. The `find()` method automatically maps the right parameter (in this example *1*). <a href="https://github.com/janhommes/o.js/tree/master/example">See this</a> demonstration for more examples.

### Get data (details) ###
--------
If you want to get data you need to call the `get()` function. This functions returns an async callback function which holds an array as it's parameter. If you use `first()` or the `find()` method it only returns the data because an array is not needed.
If you want to get data you need to call the `get()` function. This functions returns an async callback function which holds an array as it's parameter. If you use `first()` or the `find()` method it only returns the data because an array is not needed.
You can also save your o-function to call it later:
```js
var oHandler=o('http://services.odata.org/V4/OData/OData.svc/Products');
Expand All @@ -60,7 +67,7 @@ oHandler.get(function(data) {
//or the saved var also contains the data:
console.log(oHandler.data);
});
```
```

If you need to get several data you can use promise. Currently o.js only suports [q.js](https://github.com/kriskowal/q). The following example show how you can get the data of two differend resources:
```js
Expand All @@ -81,7 +88,7 @@ o('http://services.odata.org/V4/OData/OData.svc/Products(2)').get().then(functio
}).fail(function(ex) {
console.log(ex);
}
```
```
## Add and change data ##
---------
Expand All @@ -94,34 +101,34 @@ You can use the `post()` function in combination with the `save()` method to add
o('http://services.odata.org/V4/OData/OData.svc/Products').post({Name:'Example 1',Description:'a'}).post({Name:'Example 2',Description:'b'}).save(function(data) {
console.log("Two Products added");
}
````
````

### Patch/Put: ###
---------
Changing (PATCH or PUT) data is nearly the same:
```js
o('http://services.odata.org/V4/OData/OData.svc/Products(1)').patch({Name:'NewName'}).save(function(data) {
console.log("Product Name changed");
console.log("Product Name changed");
});
````
````
### Delete: ###
---------
To remove (DELETE) data you need to call `remove()`:
```js
o('http://services.odata.org/V4/OData/OData.svc/Products(1)').remove().save(function(data) {
console.log("Product deleted");
console.log("Product deleted");
});
````
````

### Reference: ###
---------
To add an reference to an other resource use `ref` (to remove it simply use `removeRef` the same way):
```js
o('http://services.odata.org/V4/OData/OData.svc/Products(1)').ref('Categories', 2).save(function(data) {
console.log("Product(1) associated with Categories(2)");
console.log("Product(1) associated with Categories(2)");
});
````
````
You can also combine a single data request (`first()` or `find()`) with the save method and chain it:
```js
Expand All @@ -133,7 +140,7 @@ o('http://services.odata.org/V4/OData/OData.svc/Products').find(2).get().then(fu
}).fail(function(ex) {
console.log("error");
});
```
```
### Endpoint configuration ###
---------
Expand All @@ -143,7 +150,7 @@ You can configure a endpoint with the `o().config()` function. This config is pe
o().config({
endpoint:'http://services.odata.org/V4/OData/OData.svc'
});

// after you have set an endpoint, you can shorten your queries:
o('Products').get(function(data) {
//same result like the first exmple on this page
Expand All @@ -165,47 +172,47 @@ However, if you have set an endpoint you can still do a full endpoint request fo
password:null, // a basic auth password
isAsync:true //set this to false to make synced (a)jax calls. (dosn't work with basic auth!)
});
```
```
### Full list of supported functions ###
---------
Currently the following queries are supported:
`.find(int)` - returns the object with the given id. (Odata: Products*(1)*)
`.top(int)` - returns the top x objects (Odata: Products/?*$top=2*) - Synonym: `.take`
`.skip(int)` - returns the objects skipped by the given value (Odata: Products/?*$skip=2*)
`.first()` - returns the first object which is found (Odata: Products/?*$top=1*)
`.filter(string)` - adds a filter string (o.js can convered simple JS-Syntax. If you need something complex use the plain Odata $filter syntax: [see the Odata doc](http://www.odata.org/documentation/odata-version-3-0/url-conventions/) for more information) (Odata: Products/?*$filter=Name eq 'Example'*) - Synonym: `.where`
`.any(string, string)` - applies an any filter to an resource (Odata: Products/?*$filter=Categories/any(x:x/Name eq 'Test')*)
`.search(array, string)` - builds up a search $filter. The first parameter defines the columns to search in the second the searchword (e.g.: `.search(['Name', 'Description'], 'Test')`)
`.orderBy(string)` - orders the data (Odata: Products/?*$orderBy=Name*)
`.orderByDesc(string)` - orders the data descading (Odata: Products/?*$orderBy=Name*)
`.count()` - only counts the result (Odata: Products/*$count*)
`.inlineCount(string)` - adds a inlinecount to the result. (Odata: Products/?*$count=true*)
`.batch(string)` - adds a second resource to the request (Odata: $batch)
`.expand(string)` - expands a related resource (Odata: Products/?*$expand=ProductUnit*)
`.ref(string, string)` - expands a related resource (Odata: Products/*$ref=Categories(1)*)
`.deleteRef(string, string)` - expands a related resource (Odata: Products/*$ref=Categories(1)*)
`.post(object)` - Post data to an endpoint
`.patch(object)` - PATCH data on an endpoint
`.put(object)` - PUT data on an endpoint
`.remove(object)` - DELETE data on an endpoint (You must define only one resource: e.g: Products(1) )
26 changes: 12 additions & 14 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
//
// An example for o.js.
//
// By Jan Hommes
// By Jan Hommes
// Date: 15.06.2015
// +++

//knockout view model
function ViewModel() {
var self=this;

//ko observables
self.People=ko.observableArray([]);
//self.currentPeople=ko.observable(null);
Expand All @@ -19,15 +19,15 @@ function ViewModel() {
self.total=ko.observable(0);
self.detailPeople=ko.observable();
self.isLoading=ko.observable(false);

self.remove = function(d) {
o('People(\'' + self.detailPeople().UserName + '\')/Trips(' + d.TripId + ')').remove().save(function() {
o('People(\'' + self.detailPeople().UserName + '\')').expand('Trips').get(function(d) {
self.detailPeople(d);
});
});
}

//o.js init
o().config({
endpoint:'http://services.odata.org/V4/%28S%28wptr35qf3bz4kb5oatn432ul%29%29/TripPinServiceRW/',
Expand All @@ -40,37 +40,35 @@ function ViewModel() {
self.isLoading(false);
}
});


//+++ initialize the routes +++

//get top 3 People on start TODO: At filter for best selling!
o('People').take(3).route('Home', function(data) {
self.route('Home');
self.People(data);
}).triggerRoute(window.location.hash === '' ? '#Home' : window.location.hash);

//get a People list on People click
o('People').take(9).inlineCount().route('People',function(data) {
o('People').take(9).inlineCount().route('People', function(data) {
self.route('People');
self.People(data);
self.skip(0);
self.total(this.inlinecount);
});

//People pagination
o('People').skip(':0').take(9).inlineCount().route('People/Page/:0',function(data) {
console.log(this.param);
o('People').skip(':0').take(9).inlineCount().route('People/Page/:0', function(data) {
self.skip(parseInt(this.param[0]));
self.route('People');
self.People(data);
self.total(this.inlinecount);
});

//People detail
o('People').filter('UserName == \':0\'').expand('Trips').first().route('People/Detail/:0',function(data) {
o('People').filter('UserName == \':0\'').expand('Trips').first().route('People/Detail/:0', function(data) {
self.route('Detail');
console.log(data);
self.detailPeople(data);
});
}
Expand Down
Loading

0 comments on commit 2e875ee

Please sign in to comment.