angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

[feature request] [angular 1.3] Out of the Box Support for AMD

Open NathanielAJohnson opened this issue 12 years ago • 63 comments

Angular comes with a robust module framework that allows for easily looking up angular entities. It makes adding AMD loader support look strange and creates redundant requirement lists. From https://github.com/tnajdek/angular-requirejs-seed/blob/master/app/js/controllers.js

define(['angular', 'services'], function (angular) {
    return angular.module('myApp.controllers', ['myApp.services'])
            .controller('MyCtrl1', ['$scope', 'version', function ($scope, version) {
                    $scope.scopedAppVersion = version;
            }])
...

It would be preferable to be able to wrap the require.js define line into the angular.module line since the angular.module() is more concise but provides all the information that an AMD loader would require.

I would write an additional set of AMD methods that mimic the module so that the previous piece of code might look like this:

angular.amd.module('myApp.controllers', ['myApp.services'])
            .controller('MyCtrl1', ['$scope', 'version', function ($scope, version) {
                    $scope.scopedAppVersion = version;
            }])
....

Further reductions are probably possible but that would be a nice beginning.

UPDATE I have detailed my ideas on what I see as the problem and a potential solution. Please, comment on either page and I will update the proposal to fufill as a many expectations as possible.

NathanielAJohnson avatar Dec 14 '13 20:12 NathanielAJohnson

It's worth to think about the future as well. Any implementation should be easily upgradable to ES6 modules in the future and that is sth that has to be kept in mind.

mgol avatar Jan 10 '14 05:01 mgol

I concur with @mzgol that any module solution should be a temporary middle step towards ES6 module system. IMHO AMD is a really fubar'd mess of insane configs and doesn't really worth the effort. If we're really gonna add it, please make it an opt-in, not a default.

gsklee avatar Jan 10 '14 07:01 gsklee

+1 to ES6

artem-popov avatar Jan 10 '14 07:01 artem-popov

+1

anvarKhakimov avatar Jan 10 '14 07:01 anvarKhakimov

I agree with this being midstep to ES6 in but I have seen competing specs sit on hold for years while the PTB play chicken with each other. There is no reason that angular can't offer an AMD solution that augments the existing module structure without breaking existing code. As far as the configuration nightmare that AMD is right now, I agree completely. I would favor a Convention over Configuration approach that eliminates all config options. I think this a healthy discussion and I have written a very short proposal: https://github.com/NathanielAJohnson/angularAMD/wiki/Proposed-Solution

On Fri, Jan 10, 2014 at 2:01 AM, Anvar Khakimov [email protected]:

+1

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/5410#issuecomment-32009320 .

NathanielAJohnson avatar Jan 10 '14 20:01 NathanielAJohnson

+1

josebalius avatar Jan 11 '14 02:01 josebalius

+1

nosideeffects avatar Jan 14 '14 05:01 nosideeffects

@NathanielAJohnson regarding your proposal - is the proposal that angular implement its own version of amd with an API like that you describe? or that angular has a consistent api that sits on top of an existing AMD solution (like requirejs say)? If it is yet another amd solution, I fear how long this will take, and how feature rich it will be (for example, minification, optimisation, plugin frameworks, etc, etc). It would be reinventing the wheel. I hope/suggest that the approach taken is to either - select an existing, working, feature rich amd solution and make it work seamlessly with angular, OR, provide hooks into angular so that the various AMD camps and their communities can create angular modules/plugins that hook in their solutions seamlessly into angular - but the programmers view is consistent no matter what solution they choose. Ideally angular could do both (expose hooks, but also make a concrete supported implementation using a particular AMD solution). Later on, this approach may make es6 easier too.

Take a look at #4694 in particular the google groups post linked from there. There I describe my use case and how i solved it with a very small change. I really hope this approach gets extended and generalised to make angular a truly scalable framework that can handle large complex applications just as well and efficiently as it handles smaller scale applications.

cleyshan avatar Jan 15 '14 21:01 cleyshan

@cleyshan I think what you have done is good but doesn't deal directly with the problem I have. My problem is not that I can't achieve dynamic routing, it is that it becomes difficult to track the relationship between Angular's inject-able Modules and AMD's singleton style modules.

The extra layer of AMD on top of Injection is prone to error and is largely unneeded since Angular provides sufficient information to determine where the Module's source would reside (at least in a convention over configuration setting). The place the current mix becomes nightmarish is in maintaining (unit) tests where the dependency chains become unmanageable and each refactor can introduce hard to recognize bugs.

I would be fine with wrapping an existing AMD solution but I think that there is a philosophical difference between AMD and Angular inject-ables in much the same way that there is a philosophical difference between traditional javascript and Angular. AMD solved many problems in the traditional environment and I believe that an equally novel approach is warranted for Angular.

As I stated in earlier posts I am perfectly willing to do the work but I think that the Angular team has done a great job of managing PRs and I do not relish the idea of maintaining a separate public module on my own.

NathanielAJohnson avatar Jan 16 '14 00:01 NathanielAJohnson

Actually, I retract my previous vote. It's much more important to use ES6 modules than to have AMD support (and that will be coming via transpiling as well AFAIK). AMD is a temporary solution until ES6 is implemented in most browsers.

mgol avatar Jan 18 '14 16:01 mgol

@mzgol Right. If we're gonna have ES6 modules in Angular 2.0, it's not worth spending extra time to get AMD support in 1.3 just to be replaced really soon afterwards.

gsklee avatar Jan 18 '14 18:01 gsklee

+1 For this Guys!

gabrieledarrigo avatar Jan 22 '14 10:01 gabrieledarrigo

I agree with @mzgol Angular should use ES6 modules.

lukemurray avatar Jan 23 '14 02:01 lukemurray

+1 for ES6!

willhoag avatar Jan 24 '14 17:01 willhoag

+1

Here's a kinda hacky, yet simple implementation of using a similar api as the current `anuglar.module``

https://github.com/EFEdcuationFirstMobile/angular-amd

fredrikbonander avatar Feb 17 '14 17:02 fredrikbonander

+1

Hacklone avatar Mar 04 '14 10:03 Hacklone

+1

fcubria avatar Mar 15 '14 08:03 fcubria

+1

Krishna-teja avatar Mar 16 '14 01:03 Krishna-teja

+1

OverclockedTim avatar Apr 26 '14 15:04 OverclockedTim

+1

danijar avatar Jun 26 '14 00:06 danijar