Hi,
Im looking at the new module system that was talked about at the angular
conf. and the style guide,
https://google-styleguide.googlecode.com/svn/trunk/angularjs-google-style.html#controllersand
it seems i use factories a little differently. Im just wondering what
the pro's cons are.
I use the angular.factory to return my services like this:
angular.factory('myService', function($rootScope, $http, myService,
anotherService ... ) {
function MyService() {
this.prop = '123';
}
MyService,prototype.doSomething = function() {
$http.get(...);
}
return new MyService();
});
controllers like this:
angular.controller('myController', function($rootScope, $http, myService,
anotherService ... ) {
function MyController() {
this.prop = '123';
}
MyController,prototype.doSomething = function() {
$http.get(...);
}
return new MyController();
});
and models like this
angular.factory('myModel', function($rootScope, $http, myService,
anotherService ... ) {
function MyModel(id, people, otherVar) {
this.prop = id;
}
MyModel,prototype.doSomething = function() {
$http.get(...);
}
return MyModel;
});
I find that my model types might use MANY injected services (like 10+), as
do some services. So, it is useful that they are injected into the
'namespace' here, and used by the type, which is returned by the factory.
The difference i am seeing in the examples of the new system, and the
styleguide, is that the injected services are attributes of the
constructor, and are stored as object parameters eg:
hello.request.Request = function($http) {
/** @type {!angular.$http} */
this.http_ = $http;
};
hello.request.Request.prototype.get = function() {/*...*/};
This seems that you are going to have to store a LOT of variables. which
seems like unneeded repetition. Is this preferable?
I understand that the example here is a 'service' not a 'factory', but i
guess that's why i use factory, not service. The examples (in the style
guide) are the same for controllers. I define my controllers 'inside' the
constructor (and return a new instance) to allow the same comfort.
Is there any benefit to stroing injected services as object properties?
Will the pattern that im using still be available with the new module
system as proposed in es6?
cheers
--
Tony Polinelli
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.