GithubHelp home page GithubHelp logo

angular-requirejs-seed's Introduction

About

This is a fork of Angular Seed but with full RequireJS support.

  • AngularJS 1.4.x
  • RequireJS 2.1.x
  • Full support for unit tests using Karma
  • Full support for e2e tests using Protractor

Changes & Notes

  • Removed index-async.html and all the related logic & tasks. Original seed project offers a way to asynchroneusly load initial set of js files using a custom loader.
  • Bootstraping file (require-config.js) is used for both unit testing and bootstraping on the actual page. If you don't plan to build your sources using r.js, you should consider removing logic related to Karma before using this file in production.

Installation

git clone [email protected]:tnajdek/angular-requirejs-seed.git
cd angular-requirejs-seed
npm install

Running

npm start

Testing

# Run unit tests automatically whenever app changes
npm test

# Run end to end tests (requires web server to be running)
npm run protractor

Documentation from the original repo untouched

angular-seed โ€” the seed for AngularJS apps

This project is an application skeleton for a typical AngularJS web app. You can use it to quickly bootstrap your angular webapp projects and dev environment for these projects.

The seed contains a sample AngularJS application and is preconfigured to install the Angular framework and a bunch of development and testing tools for instant web development gratification.

The seed app doesn't do much, just shows how to wire two controllers and views together.

Getting Started

To get you started you can simply clone the angular-seed repository and install the dependencies:

Prerequisites

You need git to clone the angular-seed repository. You can get git from http://git-scm.com/.

We also use a number of node.js tools to initialize and test angular-seed. You must have node.js and its package manager (npm) installed. You can get them from http://nodejs.org/.

Clone angular-seed

Clone the angular-seed repository using git:

git clone https://github.com/angular/angular-seed.git
cd angular-seed

If you just want to start a new project without the angular-seed commit history then you can do:

git clone --depth=1 https://github.com/angular/angular-seed.git <your-project-name>

The depth=1 tells git to only pull down one commit worth of historical data.

Install Dependencies

We have two kinds of dependencies in this project: tools and angular framework code. The tools help us manage and test the application.

We have preconfigured npm to automatically run bower so we can simply do:

npm install

Behind the scenes this will also call bower install. You should find that you have two new folders in your project.

  • node_modules - contains the npm packages for the tools we need
  • app/bower_components - contains the angular framework files

Note that the bower_components folder would normally be installed in the root folder but angular-seed changes this location through the .bowerrc file. Putting it in the app folder makes it easier to serve the files by a webserver.

Run the Application

We have preconfigured the project with a simple development web server. The simplest way to start this server is:

npm start

Now browse to the app at http://localhost:8000/app/index.html.

Directory Layout

app/                    --> all of the source files for the application
  app.css               --> default stylesheet
  components/           --> all app specific modules
    version/              --> version related components
      version.js                 --> version module declaration and basic "version" value service
      version_test.js            --> "version" value service tests
      version-directive.js       --> custom directive that returns the current app version
      version-directive_test.js  --> version directive tests
      interpolate-filter.js      --> custom interpolation filter
      interpolate-filter_test.js --> interpolate filter tests
  view1/                --> the view1 view template and logic
    view1.html            --> the partial template
    view1.js              --> the controller logic
    view1_test.js         --> tests of the controller
  view2/                --> the view2 view template and logic
    view2.html            --> the partial template
    view2.js              --> the controller logic
    view2_test.js         --> tests of the controller
  app.js                --> main application module
  index.html            --> app layout file (the main html template file of the app)
  index-async.html      --> just like index.html, but loads js files asynchronously
karma.conf.js         --> config file for running unit tests with Karma
e2e-tests/            --> end-to-end tests
  protractor-conf.js    --> Protractor config file
  scenarios.js          --> end-to-end scenarios to be run by Protractor

Testing

There are two kinds of tests in the angular-seed application: Unit tests and End to End tests.

Running Unit Tests

The angular-seed app comes preconfigured with unit tests. These are written in Jasmine, which we run with the Karma Test Runner. We provide a Karma configuration file to run them.

  • the configuration is found at karma.conf.js
  • the unit tests are found next to the code they are testing and are named as ..._test.js.

The easiest way to run the unit tests is to use the supplied npm script:

npm test

This script will start the Karma test runner to execute the unit tests. Moreover, Karma will sit and watch the source and test files for changes and then re-run the tests whenever any of them change. This is the recommended strategy; if your unit tests are being run every time you save a file then you receive instant feedback on any changes that break the expected code functionality.

You can also ask Karma to do a single run of the tests and then exit. This is useful if you want to check that a particular version of the code is operating as expected. The project contains a predefined script to do this:

npm run test-single-run

End to end testing

The angular-seed app comes with end-to-end tests, again written in Jasmine. These tests are run with the Protractor End-to-End test runner. It uses native events and has special features for Angular applications.

  • the configuration is found at e2e-tests/protractor-conf.js
  • the end-to-end tests are found in e2e-tests/scenarios.js

Protractor simulates interaction with our web app and verifies that the application responds correctly. Therefore, our web server needs to be serving up the application, so that Protractor can interact with it.

npm start

In addition, since Protractor is built upon WebDriver we need to install this. The angular-seed project comes with a predefined script to do this:

npm run update-webdriver

This will download and install the latest version of the stand-alone WebDriver tool.

Once you have ensured that the development web server hosting our application is up and running and WebDriver is updated, you can run the end-to-end tests using the supplied npm script:

npm run protractor

This script will execute the end-to-end tests against the application being hosted on the development server.

Updating Angular

Previously we recommended that you merge in changes to angular-seed into your own fork of the project. Now that the angular framework library code and tools are acquired through package managers (npm and bower) you can use these tools instead to update the dependencies.

You can update the tool dependencies by running:

npm update

This will find the latest versions that match the version ranges specified in the package.json file.

You can update the Angular dependencies by running:

bower update

This will find the latest versions that match the version ranges specified in the bower.json file.

Loading Angular Asynchronously

The angular-seed project supports loading the framework and application scripts asynchronously. The special index-async.html is designed to support this style of loading. For it to work you must inject a piece of Angular JavaScript into the HTML page. The project has a predefined script to help do this.

npm run update-index-async

This will copy the contents of the angular-loader.js library file into the index-async.html page. You can run this every time you update the version of Angular that you are using.

Serving the Application Files

While angular is client-side-only technology and it's possible to create angular webapps that don't require a backend server at all, we recommend serving the project files using a local webserver during development to avoid issues with security restrictions (sandbox) in browsers. The sandbox implementation varies between browsers, but quite often prevents things like cookies, xhr, etc to function properly when an html page is opened via file:// scheme instead of http://.

Running the App during Development

The angular-seed project comes preconfigured with a local development webserver. It is a node.js tool called http-server. You can start this webserver with npm start but you may choose to install the tool globally:

sudo npm install -g http-server

Then you can start your own development web server to serve static files from a folder by running:

http-server -a localhost -p 8000

Alternatively, you can choose to configure your own webserver, such as apache or nginx. Just configure your server to serve the files under the app/ directory.

Running the App in Production

This really depends on how complex your app is and the overall infrastructure of your system, but the general rule is that all you need in production are all the files under the app/ directory. Everything else should be omitted.

Angular apps are really just a bunch of static html, css and js files that just need to be hosted somewhere they can be accessed by browsers.

If your Angular app is talking to the backend server via xhr or other means, you need to figure out what is the best way to host the static files to comply with the same origin policy if applicable. Usually this is done by hosting the files by the backend server or through reverse-proxying the backend server(s) and webserver(s).

Continuous Integration

Travis CI

Travis CI is a continuous integration service, which can monitor GitHub for new commits to your repository and execute scripts such as building the app or running tests. The angular-seed project contains a Travis configuration file, .travis.yml, which will cause Travis to run your tests when you push to GitHub.

You will need to enable the integration between Travis and GitHub. See the Travis website for more instruction on how to do this.

CloudBees

CloudBees have provided a CI/deployment setup:

If you run this, you will get a cloned version of this repo to start working on in a private git repo, along with a CI service (in Jenkins) hosted that will run unit and end to end tests in both Firefox and Chrome.

Contact

For more information on AngularJS please check out http://angularjs.org/

angular-requirejs-seed's People

Contributors

cesine avatar cthorne66 avatar eranation avatar jamie-pate avatar jeanbza avatar jwhitley avatar robinboehm avatar tango42 avatar thegreatsunra avatar tnajdek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-requirejs-seed's Issues

How do I make sub-services into a service folder

Hello Tom,

I'm currently using your seed as my project starter and happy with that but I have a problem- the services and directives didn't split into folders and I've tried to do it but failed.

hope you could give me some help. thanks!

Add jasmine to .jshintrc

if you add jasmine: true to .jshintrc, you won't have to do this at the top of all the specs:

/*global describe, it, expect, beforeEach, afterEach, module, inject */

probably just this

/*global module, inject */

also note the lack of the first space is important with defining globals. just offering this tip based on a cursory view.

e2e test is not working.

Hi,

I tried set this up and everything works fine but e2e test. I exactly followed instruction, go to root folder, run scripts/web-server.js. But when I opened runner.html page, this error came up, not sure what went wrong. Thanks in advance.

554ms should automatically redirect to /view1 when location hash/fragment is empty
17ms browser navigate to '/'
501ms sleep for 0.5 seconds
5ms $location.url()
http://localhost:8000/test/e2e/scenario.js:13:10

TypeError: Object [object Object] has no method 'injector'
at Object. (http://localhost:8000/test/lib/angular/angular-scenario.js:25591:30)
at null. (http://localhost:8000/test/lib/angular/angular-scenario.js:25476:18)
at angular.scenario.Application.executeAction (http://localhost:8000/test/lib/angular/angular-scenario.js:24632:19)
at Object. (http://localhost:8000/test/lib/angular/angular-scenario.js:25449:22)

was it cached?

i had been resetting my IIS and kept pressing ctrl shift f5 to force load the controller.js but kept getting the old js file.. until I renamed the js file name and re-loaded manually in the browser then i see the updated js file.. any ideas ?

How to merge views into components

Could I possibly have a different structure in which the components folder also had the views associated with each component - i.e. view2 folder would be a subfolder in the version/ folder? view1 would be in a component maybe called "core" - is a homepage of sorts.

require.js question, may be issue

Hi.
Am new to angular and stuff.
Found this thing in search for a way to compile angular app for production in to a single file.
Hoped to use require.js optimize http://requirejs.org/docs/optimization.html

From first try it did not work that well. I did install bower and dependencies trough it.
Then installed require.js globally.
Then created build.js file for optimizer

({
    baseUrl: "app/js/",
    paths: {
        angular: '../../bower_components/angular/angular',
        angularRoute: '../../bower_components/angular-route/angular-route',
        angularMocks: '../../bower_components/angular-mocks/angular-mocks',
        text: '../../bower_components/requirejs-text/text'
    },
    name: "main",
    out: "main.min.js",
    optimize: "none"
})

And changed require.js data-main to point to it.

From first look in to the file it seems to be fine, found dependencies and compiled everything together but it does not work.
For example that's how it defined angular dependency.

define("angular", function(){});

As a result in

define('services',['angular'], function (angular) {


    angular.module('myApp.services', [])
        .value('version', '0.1');
});

angular is undefined.

Can may be someone point me to what I am doing wrong or if there is better way to have nicely separated angular.js files compiled for production?

main.js: confusing $html.addClass('ng-app')

line 34: $html.addClass('ng-app');
This line is unnecessary, as I was following the code in your project, this line led me to leave out <html class="ng-app"> from the index.html file.

Just a heads up!

Use of resolve in Routing

I tried to use the resolve property with $routeProvider in routes.js:

 $routeProvider.when('/url'', {
            templateUrl: '/static/app/partials/template.html',
            controller: 'Ctrl1',
            resolve: { 
                   getPromise: /* get a promise */
             }
        });

In 'Ctrl1' definition, if I try to inject getPromise like

   ...('Ctrl1', ['$scope' ... , function($scope, ..., getPromise){...}]); 

browser says "getPromiseProvider error".

Seems like the app goes to look for it only in services.js.

Am I missing something about how the app modules/files get loaded?

ui-router not working while unit testing

Hi Thanks for the seed project. I have setup a project basedon the steps and it works great!

However I am facing issue while trying to setup unit test case with ui-router.

var module = angular.module('moduleAddonControllers', []);
module.controller('TestaddonAddCtrl',['$scope','$state', function ($scope,$parse) {

$scope.mode = "add";
$state.go();

}
]);

When controller is having reference of '$state' my unit test case if failing with following error message!
"Error: [$injector:unpr] Unknown provider: $stateProvider <- $state"

My guess is *Spec.js are included before the ui-router causing the above issue. Kindly let me know how we can get around this?

Absolute paths

This seed uses absolute paths. I fixed mine by dropping the leading forward slash (/) at:
app/js/main.js, line 7
index.html, line 6
index.html, line 7

Location of index.html

Why is index.html located in the root directory, when the angular/angular-seed has it in the app/ directory?

Thanks for the project!

Controller callback is needed?

Hi,

I am trying to implement lazy loading for my project and for doing this I used your seed project, till now it works perfectly I set up all things and worked as I intended.

the problem is when I tried to use one of my directives which needs some attributes from controller cannot get attributes because directives start working after controller injected but in your project controller should be injected in controller, and while controller doing this directives started already and catch all attributes as undefined (because real controller still working and no attributes variable is filled).

So first solution which comes to my mind is adding callback to controller to be sure all variables are filled.

do you have any other suggestion?

How to pass dependency into sub-services ?

I have learned how to declare sub services in this issue: #14

But I want to pass a dependency into the sub-service, for example:

define([], function (**DEPENDENCY_HERE**) {
    return function () {
        return {
            /**
             * @return {Object}
             */
            get: function () {
                return //code
            },
            /**
             * @param {Object} task
             */
            put: function (task) {
                //code
            }
        }
    }

});

How can I do such kind of thing ?
Many thanks in advance !

Testing asynchronously loaded controllers

Hi tnajdek,

how would you test MyCtrl2 from the boilerplate code? I cannot seem to make it work. When I use your boilerplate to test MyCtrl2, the following happens:

.controller('MyCtrl2', ['$scope', '$injector', function($scope, $injector) {
            console.log('Creating controller...');
            require(['controllers/myctrl2'], function(myctrl2) {
                console.log('Injecting scope...');
                $injector.invoke(myctrl2, this, {
                    $scope: $scope
                });
            });
        }]);

will only output

Creating controller...

It seems Karma is having issues with the asynchronous part. How would you fix that?

Cheers,
miffels

Application Loader

I've build my app using your angular-requrejs-seed, now i need add loader because my app takes time to load first time is there any suggestion to add loader while requirejs loading all required files for app ?

How to move tests into different area?

I love the angularjs-requirejs seed, and have used it several times for a number of projects. For my current project, I'm required to move the tests OUT of the main code-base. I've tried a number of ways to add tests to a separate area, but then I get errors like this:

ERROR [karma]: Uncaught Error: Mismatched anonymous define() module: function (app) {
        describe('app.version module', function() {
                beforeEach(module('app.version'));

                describe('app-version directive', function() {
                        it('should print current version', function() {
                                module(function($provide) {
                                        $provide.value('version', 'TEST_VER');
                                });
                                inject(function($compile, $rootScope) {
                                        var element = $compile('<span app-version></span>')($rootScope);
                                        expect(element.text()).toEqual('TEST_VER');
                               });
                        });
                });
        });
}
http://requirejs.org/docs/errors.html#mismatch
at http://localhost:9876/absoluteC:/Users/zzz/zzz/node_modules/requirejs/require.js?3d820c829606b74b8680ffd0416fdb96d16ec957:141

I know that the project/test environment is correct, because when I move the tests back to their native folders, everything works okay. Here's a sample karma.conf (relevant parts):

    basePath : './public',

    files : [
        {pattern: 'libs/angular/angular.js', included: false},
        {pattern: 'libs/angular-mocks/angular-mocks.js', included: false},
        {pattern: 'libs/angular-translate/angular-translate.min.js', included: false},
        {pattern: 'libs/angular-bootstrap/ui-bootstrap.min.js', included: false},
        {pattern: 'libs/angular-ui-router/release/angular-ui-router.js', included: false},
        {pattern: 'libs/requirejs-text/text.js', included: false},
        {pattern: 'app/shared/version/*.js', included: false},
        {pattern: 'app/service/*.js', included: false},
        {pattern: 'app/model/*.js', included: false},
        {pattern: 'app.js', included: false},
        {pattern: 'test/*.js', included: true},
        'require-config.js'
    ],

ng-animate

Having difficulty integrating the animation features into this project. About to give a js based method a try, but it seems like it should be much easier than this? I'm looking at other examples and they are very simple, i'm wondering if there is something strange going on?

Regardless, it might be great to have this built into the project?

Scalability

Does it make sense to split the modules out into filters, services, directives, etc... how would that scale out for a large-scale JS app?

$routeProvider.route resolve

How would you go about using route.resolve in this structure? The specific issue I'm having is with how to correctly inject the resolved value into the controller instance in controllers.js. I'm using an external controller.

service

image
image
image
image
image
Why this error? How to solve

webapp load

define([
'angular',
'angularRoute',
'view1/view1',
'view2/view2'
], function(angular, angularRoute, view1, view2) {}
if there have 1000 pages ,what should you do?the single page will load js of 1000?that too slow!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.