AngularJS + OOP is a kind of sexual function to use
Hi, I have been successfully using OOP with AngularJs for some time (first I started with angularjs with oop inheritance in action ), the approach provided allows you to define your classes as angular services, which you can later extend or inherit from this:
Application.factory('AbstractObject', [function () { var AbstractObject = Class.extend({ virtualMethod: function() { alert("Hello world"); }, abstractMethod: function() {
Plunker: http://plnkr.co/edit/rAtVGAsNYggBhNADMeoT
using the described approach gives you the opportunity to define classes that integrate perfectly with the angular infrastructure. You get all sorts of great features from two worlds - OOP and AngularJs. Injection injection is free for your classes, and this makes your classes simple, allows you to put a lot of the code of the template controller into some base class, which can be reused later.
However
AngularJs infrastructure blocks the previously described approach from spreading its wings at 100%. The problem arises when you try to define recursive class definitions (e.g. recursive aggregation), for example, you have two class definitions such as Blog and Tag
Application.factory('Blog', ['Tag', function (Tag) { var Blog = Class.extend({ tags: function() { return this.tags; } }); return Blog; }]); Application.factory('Tag', ['Blog', function (Blog) { var Tag = Class.extend({ Blogs: function() { return this.blogs; } }); return Tag; }]);
This will not work, because both Blog and Tag refer to themselves, causing a circular dependency.
PS
The last thing I found was a kind of ugly solution that solves my problem in my particular case, but does not work at all, and, as I said, this is ugly:
Application.factory('BlogNamespace', [function () { var Blog = Class.extend({ tags: function() { return this.tags; } }); var Tag = Class.extend({ Blogs: function() { return this.blogs; } }); return { Tag: Tag, Blog: Blog }; }]);
Question
The above fix will not work, as namespaces can also be subject to circular dependency. This means that this is not a solution to the described problem, but a problem at a level below the level.
Any suggestions on how to solve the described problem in the general case?