-
-
Save joelhooks/6251511 to your computer and use it in GitHub Desktop.
//from most to least verbose, but it is all the same in the end. | |
(function() { | |
var module = angular.module("myApp.myModel", []); | |
var MyModel = function MyModel() { | |
this.asyncService = null; | |
this.someApi = function() { | |
return this.asyncService.getStuff(); //promise? | |
} | |
} | |
var myModelProvider = Class.extend({ | |
model: new MyModel(); | |
$get: ['asyncService', function (asyncService) { | |
model.asyncService = asyncService; //dependency injection | |
return model; //resolved for the lifetime of app | |
}] | |
}); | |
modules.provider('myModel', myModelProvider); | |
}()) | |
// or | |
(function() { | |
var module = angular.module("myApp.myModel", []); | |
var MyModel = function MyModel(asyncService) { | |
this.asyncService = null; | |
this.someApi = function() { | |
return this.asyncService.getStuff(); //promise? | |
} | |
} | |
module.factory('myModel', ['asyncService', function (asyncService) { | |
//could do some stuff here | |
return new MyModel(asyncService); | |
}]); | |
}()) | |
// or | |
(function() { | |
var module = angular.module("myApp.myModel", []); | |
var MyModel = function MyModel(asyncService) { | |
this.asyncService = null; | |
this.someApi = function() { | |
return this.asyncService.getStuff(); //promise? | |
} | |
} | |
module.service('myModel', ['$asyncService', MyModel]); //most simple option | |
}()) |
angular.run()
tells the NG engine to start
- Processing the html to determine which Angular construction is needed
- Instantiating all the required, registered services, values, and factories.
It uses ng-app
, ng-controller
etc in the HTML to determine what to load/instantiate at what time.
Thanks @ThomasBurleson. These were thrown together as a quick example for somebody and I never actually ran them (or even studied them more than a moment). Munged together from three code bases on my hard drive with names change to protect the innocent :>
@netpoetica Class is John Resig's simple class inheritance. http://ejohn.org/blog/simple-javascript-inheritance/
@joelhooks
Just working for you as a second pair of eyes
;-).
BTW, rumour/documentation implies that a factory
creates a singleton
. Since a service (which uses new
) does not necessarily create a singleton, here is a way to easy force/manage singletons regardless of .factory()
or .service()
instantiations.
(function(angular) {
"use strict";
var _service = null,
_singleton = {
asyncService : function() { return _service },
someApi : function() { return _service.getStuff(); }
};
/**
* MyModel constructor function
*/
MyModel = function ($asyncService) {
_service = $asyncService || _service;
return _singleton;
};
},
// Redefine for minification...
MyModel = [ '$asyncService', MyModel ];
// Register `myModel` as an instance of the MyModel service or factory.
angular.module( "myApp.myModel", [])
.service('myModel', MyModel);
}(angular));
@netpoetica -
$asyncService is another instance (of a Class, object, function) that was
.service()
or.factory()
, and then later...This instance
$asyncService
is then injected as an parameter to the functionMyModel( )
invocation; which creates an instance ofmyModel
.See how Angular allows you to create beans/services, define dependencies, and auto-injects those as needed?