0.1.5 • Published 8 years ago

mg-model v0.1.5

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

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.module('app.models').factory('UserModel', function(ngModel) {
    return mgModel.extend({
        // These fields don't need to be declared explicitly
        firstName: null,
        lastName: null,
        getName: function() {
            return this.firstName + ' ' + this.lastName;
        },
        $collection: ngModel.$collection.extend({
            getByName: function(name) {
                return this.oneExp('$model.name == value', name)
            },
            mapNames: function() {
                return this.map(function(model) { return model.getName() };
            }
        });
    });    
});

// user.controller.js
function UserController(UserModel, $http) {
    var vm = this;
    UserModel.$collection.load($http('/users').then(function(collection) {
        vm.john = collection.getByName('John');
        vm.usersCount = collection.length;
        vm.names = collection.mapNames();
    });
}

Inheritance

// animal.model.js:
angular.module('app.models').factory('AnimalModel', function(ngModel) {
    return mgModel.extend({
        say: function() {
            throw 'Not implemented';
        }
    });
});    
    
// animal.model.js:
angular.module('app.models').factory('DogModel', function(AnimalModel) {
    return AnimalModel.extend({
        say: function() {
            return 'woof';
        }
    });
});    

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

NameDescription
constructor(data)Constructor which applies data to itself
on(event, handler)Attach a handler on the event.
emit(event, data)Emit the event
getIdField():stringReturn a name of id field (default id)
getId():ObjectReturn a value of id field

Static

NameDescription
$collectionA 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

NameDescription
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):thisAppends records, wrap each record to $model class if necessary. If prepare=true pass the data through prepare() method
appendResource(resource):PromiseLoads data from promise and call append(data, true) when finish
filterExp(expression, scope):BaseCollectionLike filter() but evaluate expression instead of iterator. See the 'Simple Model' example
oneExp(expression, scope):BaseCollectionLike filterExp() but returns only first item
byId(id):BaseModelFind record by calling getId() on each
toObject():ObjectReturn Object keyed by id

Static

NameDescription
$modelA 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):PromiseReturn a new instance wrapped to a promise and filled by the resource

License

Licensed under MIT.