Skip to content

Instantly share code, notes, and snippets.

@mahizsas
Forked from bennadel/instance.htm
Created September 11, 2013 19:01
Show Gist options
  • Save mahizsas/6528221 to your computer and use it in GitHub Desktop.
Save mahizsas/6528221 to your computer and use it in GitHub Desktop.
<!doctype html>
<html ng-app="Demo">
<head>
<meta charset="utf-8" />
<title>
Defining Instantiatable Classes In AngularJS
</title>
</head>
<body>
<!-- Load jQuery and AngularJS from the CDN. -->
<script
type="text/javascript"
src="//code.jquery.com/jquery-2.0.3.min.js">
</script>
<script
type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js">
</script>
<!-- Load the app module and its classes. -->
<script type="text/javascript">
// Define our AngularJS application module.
var demo = angular.module( "Demo", [] );
// -------------------------------------------------- //
// -------------------------------------------------- //
// I run when the AngularJS application has been bootstrapped
// and the dependency injector is ready to rock and roll.
demo.run(
function( Friend ) {
// Create Tricia using the vanilla constructor.
var tricia = new Friend( "Tricia", "Smith" );
// Create Joanna using the convenience class method.
var joanna = Friend.fromFullName( " Joanna Smith-Joe " );
// Log the various parts to make sure values were parsed
// and stored correctly.
console.log(
tricia.getFullName(),
"... or simply,",
tricia.getFirstName()
);
console.log(
joanna.getFullName(),
"... or simply,",
joanna.getFirstName()
);
}
);
// -------------------------------------------------- //
// -------------------------------------------------- //
// Define an injectable trim method so we can demonstrate the
// use of dependency injection in the next Factory.
demo.value( "trim", jQuery.trim );
// -------------------------------------------------- //
// -------------------------------------------------- //
// To define an instantiatable class / constructor, we can
// use either the Factory() of the Value() method. I prefer
// the Factory since it allows for dependency injection.
demo.factory(
"Friend",
function( trim ) {
// Define the constructor function.
function Friend( firstName, lastName ) {
this.firstName = trim( firstName || "" );
this.lastName = trim( lastName || "" );
}
// Define the "instance" methods using the prototype
// and standard prototypal inheritance.
Friend.prototype = {
getFirstName: function() {
return( this.firstName );
},
getFullName: function() {
return( this.firstName + " " + this.lastName );
}
};
// Define the "class" / "static" methods. These are
// utility methods on the class itself; they do not
// have access to the "this" reference.
Friend.fromFullName = function( fullName ) {
var parts = trim( fullName || "" ).split( /\s+/gi );
return(
new Friend(
parts[ 0 ],
parts.splice( 0, 1 ) && parts.join( " " )
)
);
};
// Return constructor - this is what defines the actual
// injectable in the DI framework.
return( Friend );
}
);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment