marcoslin / angularAMD

Facilitate use of RequireJS in AngularJS
http://marcoslin.github.io/angularAMD
MIT License
734 stars 171 forks source link

Add controllerUrl function support. #112

Closed ivan-shaban closed 9 years ago

ivan-shaban commented 9 years ago

Hi, i have next route configuration:

app.js:

    .config(["$routeProvider", function ($routeProvider) {
        $routeProvider
            .when('/something/:id', angularAMD.route({
                templateUrl  : function ($routeParams) {
                    return "views/something/" + $routeParams.id + ".html";
                },
                controllerUrl: function ($route) {
                    return "controllers/something/" + $route.current.params.id + "/main";
                }
            }))

custom-controller.js

define(["app", "./_index"], function () {
    'use strict';

    function SomeCustomController($scope) {
             ...
    }

    return ["$scope", SomeCustomController];
});

Seems like angularAMD doesn't support controllerUrl as function and as result i can't define controllers dynamical.

I found a little workaround but probably code not the best, AngularAMD.prototype.route:

// If controller needs to be loaded, append to the resolve property
        if (load_controller) {
            var resolve = config.resolve || {};
            resolve['__AAMDCtrl'] = ['$q', '$rootScope', '$injector', function ($q, $rootScope, $injector) { // jshint ignore:line
                var defer = $q.defer();
                if (typeof load_controller === 'function' || load_controller.constructor === Array) {
                    load_controller = $injector.invoke(load_controller);
                }
                require([load_controller], function (ctrl) {
                    defer.resolve(ctrl);
                    $rootScope.$apply();
                });
                return defer.promise;
            }];
            config.resolve = resolve;
        }

Can you improve it and implement into library?

marcoslin commented 9 years ago

@LordXaoca how about a pull request? Adding this to unit test and updating README.md would be great.

ivan-shaban commented 9 years ago

@marcoslin sorry, i'm newbee with js yet, so i don't think that i could write good tests for now(( mb in near future

Also i'm check value type with array, is it really neccessary? Is it possible that value could be anything else then string, function or array?

marcoslin commented 9 years ago

@LordXaoca in the example you provided, and based on your sample code, you need to pass a 'controller' parameter inside the angularAMD.route call. How would you name your controller in this case?

ivan-shaban commented 9 years ago

Really i don't need it, i use route without controller syntax and all works fine, mb i miss anything?

I add more explicit example to first message.

marcoslin commented 9 years ago

I was going to suggest inline controller but wasn't sure of your exact implementation. It seems that you have found a solution for you already. Closing the issue.

riccardoroasio commented 8 years ago

Hi,

I can you solve this probem? I have the same "issue"...

Thanks, Riccardo