Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting angular to manually bootstrap using grunt and r.js/requirejs

I am trying to integrate requirejs into an existing project, which runs on a node/grunt stack. I want to use the r.js optimizer to concat everything together and noodle through dependencies to do its magic as well.

I am able to get r.js to build a single .js file, and there are no errors... but nothing happens. I set breakpoints in my code, but nothing is getting kicked off - the app never actually runs the bootstrap.js file. I have tried putting the bootstrap.js in an immediate function, and of course it does run then, but the dependencies are not loaded yet (it seems). What am I missing here?

File structure:

 app
 -> modules
 - -> common
 - - -> auth.js // contains an auth call that needs to return before I bootstrap angular
 -> app.js
 -> index.html
 config
 -> main.js
 node_modules
 vendor
 -> angular/jquery/require/domready/etc
 gruntfile.js

gruntfile requirejs task:

requirejs: {
  compile: {
    options: {
      name: 'app',
      out: 'build/js/app.js',
      baseUrl: 'app',
      mainConfigFile: 'config/main.js',
      optimize: "none"
    }
  }
},

main.js config:

require.config({
  paths: {
    'bootstrap':      '../app/bootstrap',
    'domReady':      '../vendor/requirejs-domready/domReady',
    'angular':       '../vendor/angular/angular',
    'jquery':        '../vendor/jquery/jquery.min',
    'app':           'app',
    'auth':          '../app/modules/common/auth',
    requireLib:      '../vendor/requirejs/require'
  },
  include: ['domReady', 'requireLib'],

  shim: {
    'angular': {
      exports: 'angular'
    }
  },

  // kick start application
  deps: ['bootstrap']
});

app.js:

define([
  'angular',
  'jquery',
], function (angular, $) {
  'use strict';

  $( "#container" ).css("visibility","visible");

  return angular.module('app');
});

bootstrap.js:

define([
    'require',
    'angular',
    'app',
    'jquery',
    'auth'
], function (require, angular, app, $, auth) {

    var Authentication = auth.getInstance();
    .. do auth stuff...

      if (Authentication.isAuthorized) {
        require(['domReady!'], function (document) {
          angular.bootstrap(document, ['app']);
       });
  }
);
like image 251
tengen Avatar asked Oct 20 '22 21:10

tengen


1 Answers

You need to set up a main point of entry into your application; here it would be app.js, but all you are doing in that file is defining that files dependencies and not actually loading any code. You need to change your code to this:

require([
  'angular',
  'jquery',
], function (angular, $) {
  'use strict';

  $( "#container" ).css("visibility","visible");

  return angular.module('app');
});

See this thread for more details on the difference between define and require.

like image 110
Ben Avatar answered Oct 27 '22 11:10

Ben