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

angular-oop

v1.0.2

Published

Object Oriented Programming approach in AngularJS

Downloads

1

Readme

angular-oop

Object Oriented Programming approach in AngularJS

This library exists to help write reusable, more extensible code in AngularJS.

In my observation, AngularJS 1.x doesn't make it very straight forward to write extensible OO code. It seems to promote a more procedural approach, keeping things encapsulated in functions and nested functions, which makes it very hard to reuse things and extend individual components. This obviously depends on how you work with Angular, so the purpose of this library to help make this process easier.

AngularJS also promotes the use of controllerAs to get around nested scope issues, but it puts the entire controller object onto the scope. There is no way to hide members from the scope and only expose what you want exposed, unless you declare private, nested, methods inside your controller or viewable service (directive, etc).

Take the following example:

angular.module('app').controller('MyController', ['$http', function MyController($scope) {

	var vm = this;

	vm.method = method;

	function method() {
		return $http.get('/foo').then(function(data) {
			vm.foo = data;
			notExposed();
			return data;
		});
	}

	function notExposed() {
		console.log('foo is', vm.foo');
	}


}]);

Now see this using angular-oop:

var AbstractController = require('angular-oop').AbstractController;

function MyController() { }

angular.inherits(MyController, AbstractController, ['$http'], {

	foo: null,

	expose: ['method', 'foo'],

	method: function() {
		return this.service('$http').get('/foo').then(function(data) {
			this.foo = data;
			this.notExposed();
			return data;
		}.bind(this));
	},

	notExposed: function() {
		// this is not exposed to scope
		console.log('foo is', this.foo);
	}

});

module.exports = MyController;

Now see how you can extend this controller to take advantage of reusable code, using CommonJS:

var MyController = require('MyController.js');

function OtherController() {
	MyController.apply(this, arguments);
}

angular.inherits(OtherController, MyController, ['$http'], {

	expose: ['doSomething'],

	doSomething: function() {
		return this.method().then(function(data) {

			return this.notExposedComputeMethod(data);

		}.bind(this));
	},

	notExposedComputeMethod: function(data) {
		// compute data and modify it some how
		data.num *= 5;
		return data;
	}

});

module.exports = OtherController;

Attach the controller to angular:

angular.module('app').controller('OtherController', require('OtherController.js'));

You can install this module using NPM: npm install angular-oop

This library is intended to be used with browserify.