Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Predefined modules dosn't work when injected. #214

Closed
MtDalPizzol opened this issue Jul 20, 2015 · 22 comments
Closed

Predefined modules dosn't work when injected. #214

MtDalPizzol opened this issue Jul 20, 2015 · 22 comments
Labels

Comments

@MtDalPizzol
Copy link

I have this config on my main application:

var mainApp = angular.module('mainApp', ['oc.lazyLoad']);

mainApp.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {

  $ocLazyLoadProvider.config({
    modules: [{
      name: 'dependency2',
      files: ['/full/path/to/dependency2.js']
    }]
  });

}]);

Now, when injecting dependencies into other modules, this happens:

This works:

var subApp = angular.module('subApp', [
    'dependency1',
    ['/full/path/to/dependency2.js'] // <-- This works 
]);

This dosn't work

var subApp = angular.module('subApp', [
    'dependency1',
    ['dependency2'] // <-- This dosn't work
]);

Am I missing something?

@ocombe
Copy link
Owner

ocombe commented Jul 21, 2015

Hmm no you're not, it should work !

@MtDalPizzol
Copy link
Author

Hi, Oliver! Here's the deal: I spent two whole days trying to get this working and here are my conclusions and observations:

There's really no problem with the code I wrote: it simply fails the moment I switch the dependency's file full path by the dependency's name. I have tryed this either with the default and the RequireJS versions of the lib.

Given this, I was able to think in the following workarounds, since I'm working on a really big project and I can't have the full path of the dependencies hardcoded over and over again:

1. Create a global object holding the full path of the files, so I can use this object to inject dependencies, like this:

var globlaDependenciesObj = {
    dependency2: '/full/path/to/dependency2.js'    
};

var subApp = angular.module('subApp', [
    'dependency1',
    [globlaDependenciesObj.dependency2] // <-- I have no idea if this can work
]);

2. Use the RequireJS version of the lib to load and register controllers when routing and use common AMD define() to load everything else, like this:

define(['dependency2'], function(){
    var subApp = angular.module('subApp', [
        'dependency1',
        'module.dependency2' // <-- This works
    ]);
});

This worked and, until now, seems to be the way I will keep going since I like the async load pattern of RequireJS.

3. Wait for you to inspect the issue and save my life. =P

Do you have a word on this? Which of this is the most recomended one?

Best regards!

@ocombe
Copy link
Owner

ocombe commented Jul 23, 2015

I will take a look at this tonight :)

In the mean time, could you try this please:

var subApp = angular.module('subApp', [
    'dependency1',
    {name: 'dependency2'}
]);

@ocombe ocombe added the bug label Jul 23, 2015
@ocombe ocombe closed this as completed in 023e4bb Jul 23, 2015
@ocombe
Copy link
Owner

ocombe commented Jul 23, 2015

This should be fixed as of 1.0.3, it was a bug hard to track, but I think that I got it :)

@MtDalPizzol
Copy link
Author

Awesome! I'm sorry I didn't had the time to test your suggestion until now, since I ended up setting my project with requirejs. I'll give it a try this weekend and I'll let you know the results asap! Thanks for your attention, Oliver!

@ocombe
Copy link
Owner

ocombe commented Jul 24, 2015

No problem :)
I fixed another bug with this anyway

@MtDalPizzol
Copy link
Author

Hi again, Oliver! I think you ended up only partially solving the problem... Here's what's happening now: The files are loaded as expected. Awesome! But the code isn't running... here's an example:

app-router.js

app.config(['$routeProvider', '$ocLazyLoadProvider', function($routeProvider, $ocLazyLoadProvider) {

  $ocLazyLoadProvider.config({
    jsLoader: requirejs,
    modules: [{
      name: 'moduleController',
      files: ['/path/to/module-controller.js'] // This is lazy loaded! Ok.
    }, {
      name: 'moduleResource',
      files: ['/path/to/module-resource.js'] // This is also lazy loaded! Ok.
    }, {
      name: 'crudDependencies',
      files: [
        '/path/to/dependency1.js',  // This is lazy loaded! Ok.
        '/path/to/dependency2.js' // This is also lazy loaded! Ok.
      ]
    }]
  });

  $routeProvider.when('mdoule', {
    templateUrl: '/path/to/module-list.html',
    controller: 'ModuleIndexController',
    resolve: {
      load: ['$ocLazyLoad', function($ocLazyLoad) {
        return $ocLazyLoad.load(['moduleController']);
      }]
    }
  });

}]);

module-controller.js

var app = angular.module('module.controller', [['moduleResource', 'crudDependencies']]);

app.controller('ModuleIndexController', function($scope, crud) {
   // Nothing here runs
}]);

@MtDalPizzol
Copy link
Author

Now, this have worked:

main.js

requirejs.config({
  baseUrl: '/my/base/url/',
  paths: {
    dep1: '/path/to/dependency1.js',
    dep2: '/path/to/dependency2.js',
    modRsc: '/path/to/module-resource.js'
  }
});

module-controller.js

var app = angular.module('module.controller', [['modRsc', 'dep1', 'dep2']]);

But I still think the first one is the way to go...

@ocombe
Copy link
Owner

ocombe commented Jul 30, 2015

How about:

var app = angular.module('module.controller', ['moduleResource', 'crudDependencies']);

It should work (no need for an array of arrays).

@MtDalPizzol
Copy link
Author

Nope... same problem: files are loaded but the code dosn't run... =/

@ocombe
Copy link
Owner

ocombe commented Jul 30, 2015

Well there's a bug that I'll fix tonight where .run don't... run, maybe that's the problem :)

@MtDalPizzol
Copy link
Author

Awesome! Thanks again!

@sleeper01
Copy link

ooh,I got the problem,too. Have you fixed the bug?My problem is the same as files are loaded but the code dosn't run

@ocombe
Copy link
Owner

ocombe commented Dec 10, 2015

Hmm that's old, so I'm not sure :)
There's nothing in the changelog for july 30th that talks about a run bug

@sleeper01
Copy link

Is there any way to just bypass this bug?

@ocombe
Copy link
Owner

ocombe commented Dec 10, 2015

Your problem is that the run blocks don't work ? Could you make me a plunkr or something so that I can try to narrow it down please?

@sleeper01
Copy link

$ocLazyLoadProvider.config({
          modules: [{
            name: 'HomeModule',
            files: ['apps/home/HomeModule.js',
            'apps/home/controller/HomeCtrl.js',
            'apps/home/service/HomeService.js']
          },{
            name: 'SdkModule',
            files: ['apps/sdk/SdkModule.js']
          }]
        });

$stateProvider.state('index', {
          url: "/", 
          templateUrl: 'apps/home/view/home.html',
          controller : 'HomeCtrl',
          resolve: {
            loadModule: ['$ocLazyLoad', function($ocLazyLoad) {
                $ocLazyLoad.load('HomeModule').then(function() {

                }, function() {

                });
            }]
          }
        });

In the HomeModule.js
angular.module("HomeModule",['SdkModule']);

Here is SdkModule.js

angular.module('SdkModule',[]).factory("DateUtils",['$filter',function($filter){
    return {
        getCurrentDate : function(){
            return $filter('date')(new Date(),'yyyy-MM-dd HH:mm:ss');
        }
    };
}]);

where the page route to "/",The SdkModule.js was loaded as deps,but the code don't run

@ocombe
Copy link
Owner

ocombe commented Dec 10, 2015

In your resolve function, you forgot to return the load promise:

$stateProvider.state('index', {
          url: "/", 
          templateUrl: 'apps/home/view/home.html',
          controller : 'HomeCtrl',
          resolve: {
            loadModule: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load('HomeModule').then(function() {

                }, function() {

                });
            }]
          }
        });

@sleeper01
Copy link

A ha,Yes u r right! Thanks very much!

@ocombe
Copy link
Owner

ocombe commented Dec 10, 2015

no problem :)

@sleeper01
Copy link

Hey,How can I use ocLazyLoad to load an AMD module?
In my HomeModule.js
this is work
angular.module("HomeModule",['DateUtilsModule','Home.HomeCtrl']);

But this can't

define(function(){
angular.module("HomeModule",['DateUtilsModule','Home.HomeCtrl']);
});

am I wrong with anything?

@ocombe
Copy link
Owner

ocombe commented Dec 11, 2015

ocLazyLoad only loads regular angular modules.
If you want to load something else you can either use requirejs and ocLazyLoad (the requirejs version in the dist folder), or write your own jsloader and plug it into ocLazyLoad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants