mg-model
Simple and lightweight angular model library
Requirements
ES5.1 compatible browser(IE9+ and all modern browsers)
Installation
Install with Bower
$ bower install mg-model
Install with NPM
$ npm install mg-model
Examples
Simple Model
// user.model.js:angular
Inheritance
// animal.model.js:angular; // animal.model.js:angular;
Properties
angular.module('app.models').factory('UserModel', function(ngModel) { return mgModel.extend({ firstName: null, lastName: null, $properties: { name: { get: function() { return this.firstName + ' ' + this.lastName; } } } }); });
API Documentation
mgModel service returns BaseModel instance
BaseModel
Properties
Properties are just a simple fields of a model, or properties defined through $properties
Methods
Name | Description |
---|---|
constructor(data) | Constructor which applies data to itself |
on(event, handler) | Attach a handler on the event. |
emit(event, data) | Emit the event |
getIdField():string | Return a name of id field (default id) |
getId():Object | Return a value of id field |
Static
Name | Description |
---|---|
$collection | A collection class for this model |
extend(members) | Extend a current model. Returns a new model class |
BaseCollection (extends Array)
Properties
There is only length property which is inherited from Array class
Methods
Name | Description |
---|---|
constructor(data, prepare=false) | Constructor which calls append() method |
on(event, handler) | Attach a handler on the event. |
emit(event, data) | Emit the event |
each(iterator) | A shortcut for forEach() method |
append(array, prepare=false):this | Appends records, wrap each record to $model class if necessary. If prepare=true pass the data through prepare() method |
appendResource(resource):Promise | Loads data from promise and call append(data, true) when finish |
filterExp(expression, scope):BaseCollection | Like filter() but evaluate expression instead of iterator. See the 'Simple Model' example |
oneExp(expression, scope):BaseCollection | Like filterExp() but returns only first item |
byId(id):BaseModel | Find record by calling getId() on each |
toObject():Object | Return Object keyed by id |
Static
Name | Description |
---|---|
$model | A model class for this collection |
extend(members) | Extend a current class. Returns a new collection class |
load(data) | Return a new instance filled by data |
loadResource(resource):Promise | Return a new instance wrapped to a promise and filled by the resource |
License
Licensed under MIT.