npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mg-model

v0.1.5

Published

Simple and lightweight angular model library

Downloads

6

Readme

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

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.