<commit_before><!-- load: plRestmod -->
<!-- provide: $provide -->
<!-- inject: $httpBackend -->
<!-- inject: $injector -->
<!-- inject: $injector -->
<!-- inject: $restmod -->

<!-- before:
	$httpBackend.when('GET', '/bikes/1').respond({ model: 'Slash', brand: 'Trek' })
	$httpBackend.when('GET', '/bikes?brand=trek').respond([ { model: 'Slash' }, { model: 'Remedy' } ])
	$httpBackend.when('GET', '/bikes?category=enduro').respond([ { model: 'Slash' }, { model: 'Remedy' } ])

	module = $provide;
-->

Angular Restmod  [![Build Status](https://api.travis-ci.org/platanus/angular-restmod.png)](https://travis-ci.org/angular-platanus/restmod) [![Stories in Ready](https://badge.waffle.io/platanus/angular-restmod.png?label=ready&title=Ready)](https://waffle.io/platanus/angular-restmod)
===============
Restmod creates objects that you can use from within Angular to interact with your RESTful API.

Saving Bikes on your serverside database would be as easy as:

<!-- section: main example -->

```javascript
var Bike = $restmod.model('/bikes');
var newBike = Bike.$build({ brand: 'Trek' });
newBike.model = '5200';
newBike.$save(); // bike is persisted sending a POST to /bikes
```

<!-- end -->

It also supports collections, relations, lifecycle hooks, attribute renaming and much more.
Continue reading for a quick start or check the API Reference for more: http://platanus.github.io/angular-restmod

## Why Restmod?
Restmod brings Rails ActiveRecord's ease of use to the Angular Framework. It succesfuly combines Angular's encapsulated design with Active Record's opinionated style. There are other alternatives available though:

**$resource:** Might be enough for small projects, included as an Angular opt-in. It only provides a basic model type layer, with limited features.

**Restangular:** very complete library, but does not propose a model layer.

**angular-activerecord:** Nice alternative to $resource, still very limited in its functionality.

**ModelCore:** Inspired in angular-activerecord, provides a more complete set of features but lacks testing.

## Getting Started

#### 1. Get the code

You can get it straight from the repository

```
git clone <EMAIL>:platanus/angular-restmod.git
```

but we recommend you to use bower to retrieve the Restmod package

```
bower install angular-restmod --save
```

#### 2. Include it on your project

Make sure the restmod source is required in your code.

```html
<script type="text/javascript" src="js/angular-restmod-bundle.min.js"></script>
```

Next, include angular module as one of your app's dependencies

<!-- before: module = $provide; -->
<!-- ignore -->

```javascript
module = angular.module('MyApp', ['plRestmod'])
```

<!-- end -->

# Basic usage

You begin by creating a new model using the `$restmod.model` method. We recommend you to put each model on a separate factory. The first argument for `model` is the resource URL.

```javascript
module.factory('Bike', function($restmod) {
	return $restmod.model('/bikes');
});
```

<!-- before: Bike = $injector.get('Bike') -->
<!-- it: expect(Bike).not.toBeNull() -->

The generated model type provides basic CRUD operations to interact with the API:

<!-- section: $find -->

To retrieve an object by ID use `$find`, the returned object will be filled with the response data when the server response is received.

Let's say you have a REST API that responds JSON to a GET REQUEST on /bikes/1

```json
{
	"id": 1,
	"brand": "Trek",
	"created_at": "2014-05-23"
}
```

Then, on your code you would call

```javascript
bike = Bike.$find(1);
```

The bike object will be populated as soon as the API returns some data. Yo can use `$then` to do something when data becomes available.

```javascript
bike.$then(function() {
	console.log(bike.brand); // will output 'Trek'
});
```

**IMPORTANT**: RestMod will rename attributes from under_score to camelCase by default, refer to the building docs if you need to disable this feature. In the example above you should use `bike.createdAt` to refer to the value of the `created_at` returned by the API.

<!-- it: $httpBackend.flush(); expect(bike.model).toEqual('Slash') -->
<!-- end -->

<!-- section: $fetch -->
<commit_msg>Update README - Fix typo.<commit_after><!-- load: plRestmod -->
<!-- provide: $provide -->
<!-- inject: $httpBackend -->
<!-- inject: $injector -->
<!-- inject: $injector -->
<!-- inject: $restmod -->

<!-- before:
	$httpBackend.when('GET', '/bikes/1').respond({ model: 'Slash', brand: 'Trek' })
	$httpBackend.when('GET', '/bikes?brand=trek').respond([ { model: 'Slash' }, { model: 'Remedy' } ])
	$httpBackend.when('GET', '/bikes?category=enduro').respond([ { model: 'Slash' }, { model: 'Remedy' } ])

	module = $provide;
-->

Angular Restmod  [![Build Status](https://api.travis-ci.org/platanus/angular-restmod.png)](https://travis-ci.org/angular-platanus/restmod) [![Stories in Ready](https://badge.waffle.io/platanus/angular-restmod.png?label=ready&title=Ready)](https://waffle.io/platanus/angular-restmod)
===============
Restmod creates objects that you can use from within Angular to interact with your RESTful API.

Saving Bikes on your serverside database would be as easy as:

<!-- section: main example -->

```javascript
var Bike = $restmod.model('/bikes');
var newBike = Bike.$build({ brand: 'Trek' });
newBike.model = '5200';
newBike.$save(); // bike is persisted sending a POST to /bikes
```

<!-- end -->

It also supports collections, relations, lifecycle hooks, attribute renaming and much more.
Continue reading for a quick start or check the API Reference for more: http://platanus.github.io/angular-restmod

## Why Restmod?
Restmod brings Rails ActiveRecord's ease of use to the Angular Framework. It succesfuly combines Angular's encapsulated design with Active Record's opinionated style. There are other alternatives available though:

**$resource:** Might be enough for small projects, included as an Angular opt-in. It only provides a basic model type layer, with limited features.

**Restangular:** very complete library, but does not propose a model layer.

**angular-activerecord:** Nice alternative to $resource, still very limited in its functionality.

**ModelCore:** Inspired in angular-activerecord, provides a more complete set of features but lacks testing.

## Getting Started

#### 1. Get the code

You can get it straight from the repository

```
git clone git@github.com:platanus/angular-restmod.git
```

but we recommend you to use bower to retrieve the Restmod package

```
bower install angular-restmod --save
```

#### 2. Include it on your project

Make sure the restmod source is required in your code.

```html
<script type="text/javascript" src="js/angular-restmod-bundle.min.js"></script>
```

Next, include angular module as one of your app's dependencies

<!-- before: module = $provide; -->
<!-- ignore -->

```javascript
module = angular.module('MyApp', ['plRestmod'])
```

<!-- end -->

# Basic usage

You begin by creating a new model using the `$restmod.model` method. We recommend you to put each model on a separate factory. The first argument for `model` is the resource URL.

```javascript
module.factory('Bike', function($restmod) {
	return $restmod.model('/bikes');
});
```

<!-- before: Bike = $injector.get('Bike') -->
<!-- it: expect(Bike).not.toBeNull() -->

The generated model type provides basic CRUD operations to interact with the API:

<!-- section: $find -->

To retrieve an object by ID use `$find`, the returned object will be filled with the response data when the server response is received.

Let's say you have a REST API that responds JSON to a GET REQUEST on /bikes/1

```json
{
	"id": 1,
	"brand": "Trek",
	"created_at": "2014-05-23"
}
```

Then, on your code you would call

```javascript
bike = Bike.$find(1);
```

The bike object will be populated as soon as the API returns some data. You can use `$then` to do something when data becomes available.

```javascript
bike.$then(function() {
	console.log(bike.brand); // will output 'Trek'
});
```

**IMPORTANT**: RestMod will rename attributes from under_score to camelCase by default, refer to the building docs if you need to disable this feature. In the example above you should use `bike.createdAt` to refer to the value of the `created_at` returned by the API.

<!-- it: $httpBackend.flush(); expect(bike.model).toEqual('Slash') -->
<!-- end -->

<!-- section: $fetch -->

{'fraction_non_alphanumeric': 0.15023809523809523, 'fraction_numerical': 0.004285714285714286, 'mean_word_length': 4.290302267002519, 'pattern_counts': {'":': 6, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '694934', 'n_tokens_mistral': 2576, 'n_tokens_neox': 2458, 'n_words': 951}