Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/swingdev/angular-smart-models
Smart Models for Angular JS
https://github.com/swingdev/angular-smart-models
Last synced: 12 days ago
JSON representation
Smart Models for Angular JS
- Host: GitHub
- URL: https://github.com/swingdev/angular-smart-models
- Owner: SwingDev
- License: mit
- Created: 2015-11-18T14:17:10.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-06T09:42:01.000Z (about 9 years ago)
- Last Synced: 2025-01-02T22:04:39.230Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 13.7 KB
- Stars: 2
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Angular Smart Models
## Overview
Angular Smart Models is a angular wrapper for Smart Model library. It also provides interceptors for Angular $http so you can plug it to your project with almost no configuration.## Installation
You can install Angular Smart Model from bower and npm.
### NPM
```npm install angular-smart-models```
### Bower
```bower install angular-smart-models```Then include Smart Model and Angular Smart Model scripts after the angular
```html```
### Usage
### Basic Example
```javascriptclass User extends SmartModel { }
angular.module('MyApp', [])
.config(function(ModelInterceptorProvider) {
ModelInterceptorProvider.registerInterceptor({
model: User,
url: '/user/',
method: ModelInterceptorProvider.ALL
});
});angular.module('MyApp')
.controller('MainController', function($http) {
$http.get('/user/1').success(function(user) {
// user is of type User
});
});
```### Usage with Collections
You can also provide the collection name and key in interceptor configuration. When the backend response with array of objects, the library will simply pack everything into the Collection of that type.
```javascript
class Post extends SmartModel { }
class PostsCollection extends Collection { }
angular.module('MyApp', [])
.config(function(ModelInterceptorProvider) {
ModelInterceptorProvider.registerInterceptor({
model: Post,
url: '/my-post/',
method: ModelInterceptorProvider.ALL,
collection: PostsCollection,
collectionKey: 'MyPosts'
});
})
.controller('MyPostsController', function($http) {
$scope.posts = PostsCollection.create('MyPosts'); // notice that we use the same collection class and collection key here$http.get('my-posts'); // and that's all we have to do here! We no longer need to call .success, the Collection will be filled with data in the interceptor and attached to $scope.posts !
});
```## Tests
The code is covered with tests, you can run them using `npm test`.