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

Extending Lazy Services for value return #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/admin/controllers/fourth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Created with IntelliJ IDEA.
* User: Mateusz
* Date: 15.11.12
* Time: 22:38
*/

'use strict';

define([], function () {

return ['FourthController', function ($scope) {
$scope.message = "I'm the 4th controller!";
}]
})
7 changes: 7 additions & 0 deletions app/admin/partials/admin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>
<h1>{{message}}</h1>
<h2 ng-controller="ThirdController">{{message}}</h2>
<h2 ng-controller="FourthController">{{message}}</h2>
<h1>Still - {{message}}</h1>
</div>

Empty file modified app/css/app.css
100644 → 100755
Empty file.
Empty file modified app/css/bootstrap.css
100644 → 100755
Empty file.
3 changes: 3 additions & 0 deletions app/index.html
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
<div>
<a href="#/view1" class="btn">view1</a>
<a href="#/view2" class="btn">view2</a>
<a href="#/admin" class="btn" ng-show="isAdmin">admin</a>
</div>

<div ng-view class="well well-small"></div>

<button ng-hide="isAdmin" ng-click="isAdmin=true;">Become admin</button>

<script data-main="js/main" src="lib/require/require.js"></script>
</body>
</html>
6 changes: 4 additions & 2 deletions app/js/app.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
*/
'use strict';

define(['angular', 'utils/route-config'], function (angular, routeConfig, lazyDirectives) {
define(['angular', 'utils/route-config'], function (angular, routeConfig) {

return angular.module('myApp', [], function ($compileProvider, $controllerProvider) {
return angular.module('myApp', [], function ($provide, $compileProvider, $controllerProvider, $filterProvider) {
routeConfig.setProvide($provide);
routeConfig.setCompileProvider($compileProvider);
routeConfig.setControllerProvider($controllerProvider);
routeConfig.setFilterProvider($filterProvider);
})
});
2 changes: 2 additions & 0 deletions app/js/controllers/first.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ define([], function () {

function FirstController($scope) {
$scope.message = "I'm the 1st controller!";
$scope.greeting = "Hello world!";

}

return FirstController;
Expand Down
4 changes: 2 additions & 2 deletions app/js/controllers/second.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

define([], function () {

function SecondController($scope) {
$scope.message = "I'm the 2nd controller!";
function SecondController($scope, tester) {
$scope.message = "I'm the 2nd controller! " + tester;
}

return SecondController;
Expand Down
15 changes: 15 additions & 0 deletions app/js/controllers/third.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Created with IntelliJ IDEA.
* User: Mateusz
* Date: 15.11.12
* Time: 22:38
*/

'use strict';

define([], function () {

return ['ThirdController', function ($scope) {
$scope.message = "I'm the 3rd controller!";
}]
})
Empty file modified app/js/directives/version.js
100644 → 100755
Empty file.
28 changes: 28 additions & 0 deletions app/js/filters/reverse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Created with JetBrains WebStorm.
* User: avivcallander
* Date: 3/31/13
* Time: 4:40 PM
* To change this template use File | Settings | File Templates.
*/


'use strict';

define([], function () {

return ['reverse', function() {
return function(input, uppercase) {
var out = "";
for (var i = 0; i < input.length; i++) {
out = input.charAt(i) + out;
}
// conditional based on optional argument
if (uppercase) {
out = out.toUpperCase();
}
return out;
}}];
})


Empty file modified app/js/main.js
100644 → 100755
Empty file.
5 changes: 3 additions & 2 deletions app/js/routes.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
define(['app', 'utils/route-config'], function (app, routeConfig) {

return app.config(function ($routeProvider) {
$routeProvider.when('/view1', routeConfig.config('../partials/view1.html', 'controllers/first'));
$routeProvider.when('/view2', routeConfig.config('../partials/view2.html', 'controllers/second', ['directives/version']));
$routeProvider.when('/view1', routeConfig.config('../partials/view1.html', 'controllers/first', null, {directives: ['directives/version'], services: [], filters: ['filters/reverse']}));
$routeProvider.when('/view2', routeConfig.config('../partials/view2.html', 'controllers/second', null, {directives: ['directives/version'], services: ['services/tester'], filters: []}));
$routeProvider.when('/admin', routeConfig.config('../admin/partials/admin.html', 'controllers/second', ['controllers/third', '../admin/controllers/fourth']));

$routeProvider.otherwise({redirectTo:'/view1'});
});
Expand Down
13 changes: 13 additions & 0 deletions app/js/services/tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Created with IntelliJ IDEA.
* User: Mateusz
* Date: 22.11.12
* Time: 22:04
*/

'use strict';

define([], function () {

return ['tester', 'test service via value return']
})
15 changes: 10 additions & 5 deletions app/js/utils/lazy-directives.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ define([], function () {
}

function register(directive) {
if (!$compileProvider) {
throw new Error("$compileProvider is not set!");
if(directive){
if (!$compileProvider) {
throw new Error("$compileProvider is not set!");
}
$compileProvider.directive.apply(null, directive);
}else{
$compileProvider.directive.apply = null;
}
$compileProvider.directive.apply(null, directive);

}

return {
setCompileProvider:setCompileProvider,
register:register
setCompileProvider: setCompileProvider,
register: register
}
})
25 changes: 25 additions & 0 deletions app/js/utils/lazy-filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
define([], function () {

var $filterProvider;

function setFilterProvider(value) {
$filterProvider = value;
}

function register(filter) {
if(filter){
if (!$filterProvider) {
throw new Error("$setProvide is not set!");
}
$filterProvider.register(filter[0], filter[1]);
}else{
$filterProvider.register = null;
}

}

return {
setFilterProvider: setFilterProvider,
register: register
}
})
26 changes: 26 additions & 0 deletions app/js/utils/lazy-services.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
define([], function () {

var $provide;

function setProvide(value) {
$provide = value;
}

function register(service) {
if (service) {
if (!$provide) {
throw new Error("$setProvide is not set!");
}
$provide.value(service[0], service[1]);
} else {
$provide.value = null;
}

}


return {
setProvide: setProvide,
register: register
}
})
92 changes: 71 additions & 21 deletions app/js/utils/route-config.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
* Time: 22:38
*/

define(['utils/lazy-directives'], function (lazyDirectives) {
define(['utils/lazy-directives', 'utils/lazy-services', 'utils/lazy-filters'], function (lazyDirectives, lazyServices, lazyFilters) {

var $controllerProvider,
$compileProvider;
$compileProvider,
$provide,
$filterProvider;

function setControllerProvider(value) {
$controllerProvider = value;
Expand All @@ -19,7 +21,25 @@ define(['utils/lazy-directives'], function (lazyDirectives) {
lazyDirectives.setCompileProvider(value);
}

function config(templateUrl, controllerName, directives) {
function setProvide(value) {
$provide = value;
lazyServices.setProvide(value);
}

function setFilterProvider(value) {
$filterProvider = value;
lazyFilters.setFilterProvider(value);
}

// Use this as a guide to extend... current "services" is only value provide
// Services should use factory
//$provide.value('a', 123);
//$provide.factory('a', function() { return 123; });
//$compileProvider.directive('directiveName', ...);
//$filterProvider.register('filterName', ...);


function config(templateUrl, controllerName, controllers, lazyResources) {
if (!$controllerProvider) {
throw new Error("$controllerProvider is not set!");
}
Expand All @@ -33,28 +53,56 @@ define(['utils/lazy-directives'], function (lazyDirectives) {
};
routeDefinition.controller = controllerName;
routeDefinition.resolve = {
delay:function ($q, $rootScope) {
delay: function ($q, $rootScope) {
defer = $q.defer();
if (!html) {
var dependencies = [controllerName, "text!" + templateUrl];
if (directives) {
dependencies = dependencies.concat(directives);
var dependencies = ["text!" + templateUrl, controllerName];
if (controllers) {
dependencies = dependencies.concat(controllers);
}
if (lazyResources) {
dependencies = dependencies.concat(lazyResources.directives);
dependencies = dependencies.concat(lazyResources.services);
dependencies = dependencies.concat(lazyResources.filters);
}
require(dependencies, function () {
var controller = arguments[0],
template = arguments[1];

for (var i = 2; i < arguments.length; i++) {
lazyDirectives.register(arguments[i]);
}
// TODO fix this mess
var template = arguments[0];
$controllerProvider.register(controllerName, arguments[1]);
var baseNumber = 2 + (controllers == null ? 0 : controllers.length);
var directiveNumber = (lazyResources.directives == null ? 0 : lazyResources.directives.length);
var serviceNumber = (lazyResources.services == null ? 0 : lazyResources.services.length);
var filterNumber = (lazyResources.filters == null ? 0 : lazyResources.filters.length);

$controllerProvider.register(controllerName, controller);
html = template;
defer.resolve();
$rootScope.$apply()
})
if (controllers) {
for (var i = 2; i < baseNumber; i++) {
$controllerProvider.register(arguments[i][0], arguments[i][1]);
}
}
if (lazyResources.directives) {
for (var i = (baseNumber); i < baseNumber + directiveNumber; i++) {
lazyDirectives.register(arguments[i]);
}
}
if (lazyResources.services) {
for (var i = (baseNumber) + directiveNumber; i < baseNumber + directiveNumber + serviceNumber; i++) {
lazyServices.register(arguments[i]);
}
}
if (lazyResources.filters) {
for (var i = (baseNumber) + directiveNumber + serviceNumber; i < baseNumber + directiveNumber + serviceNumber + filterNumber; i++) {
lazyFilters.register(arguments[i]);
}
}
html = template;
defer.resolve();
$rootScope.$apply()
}
)

} else {
}
else {
defer.resolve();
}
return defer.promise;
Expand All @@ -65,9 +113,11 @@ define(['utils/lazy-directives'], function (lazyDirectives) {
}

return {
setControllerProvider:setControllerProvider,
setCompileProvider:setCompileProvider,
config:config
setControllerProvider: setControllerProvider,
setCompileProvider: setCompileProvider,
setProvide: setProvide,
setFilterProvider: setFilterProvider,
config: config
}
})

Empty file modified app/lib/angular/angular-bootstrap-prettify.js
100644 → 100755
Empty file.
Empty file modified app/lib/angular/angular-bootstrap.js
100644 → 100755
Empty file.
Empty file modified app/lib/angular/angular-cookies.js
100644 → 100755
Empty file.
Empty file modified app/lib/angular/angular-cookies.min.js
100644 → 100755
Empty file.
Empty file modified app/lib/angular/angular-loader.js
100644 → 100755
Empty file.
Empty file modified app/lib/angular/angular-loader.min.js
100644 → 100755
Empty file.
Empty file modified app/lib/angular/angular-resource.js
100644 → 100755
Empty file.
Empty file modified app/lib/angular/angular-resource.min.js
100644 → 100755
Empty file.
Empty file modified app/lib/angular/angular-sanitize.js
100644 → 100755
Empty file.
Empty file modified app/lib/angular/angular-sanitize.min.js
100644 → 100755
Empty file.
Empty file modified app/lib/angular/angular.js
100644 → 100755
Empty file.
Empty file modified app/lib/angular/angular.min.js
100644 → 100755
Empty file.
Empty file modified app/lib/angular/version.txt
100644 → 100755
Empty file.
Empty file modified app/lib/jquery/jquery.js
100644 → 100755
Empty file.
Empty file modified app/lib/require/require.js
100644 → 100755
Empty file.
Empty file modified app/lib/require/text.js
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions app/partials/view1.html
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<div>
<h1>{{message}}</h1>
No filter: {{greeting}}<br>
Reverse: {{greeting|reverse}}
</div>
Empty file modified app/partials/view2.html
100644 → 100755
Empty file.