Всем привет. Клиентская часть приложения написана на Angularjs, сейчас я переделываю проект, что бы он использовал Requirejs.
Мне надо решить проблему с подключением зависимостей.
'application/services/P48Wallet'
В P48Wallet.js мне надо подключить Http.js
define(['application/app', 'application/services/Http'], function(app) {
В Http.js, Когда я пытаюсь вернуть app.factory
return app.factory('Http', function($http, $q, $location, $rootScope) {
Я получаю ошибку:
Uncaught TypeError: Cannot call method 'factory' of undefined Http.js
console.log(app); // undefined
Что не так, почему app не определен в Http.js?
================================================== ================================
Структура проекта
static\
js\
application\
controllers\
Main.js
directives\
...
filters\
...
services\
Http.js
P48Wallet.js
views\
main.html
app.js
main.js
libs\
angular.min.js
....
================================================== ================================
index.html
<!DOCTYPE html>
<html>
<head>
<script data-main="/static/js/application/main" src="/static/js/libs/require.min.js"></script>
<script type="text/javascript">
var server_url = '{$HTTP_STATIC_PATH}';
</script>
</head>
<body>
<div class="loader_cover" loader="{literal}{{showLoader}}{/literal}"><div class="loader_img"></div></div>
<div class="page" ng-view></div>
</body>
</html>
================================================== ================================
app.js
define(
[
'angularAMD',
'angular-route',
'angular-animate',
'application/services/P48Wallet'
],
function (angularAMD) {
var app = angular.module('Order', ['ngRoute', 'ngAnimate']);
app.config(function($routeProvider){
$routeProvider
.when('/',
angularAMD.route({
templateUrl: 'static/js/application/views/main.html',
controller: 'application/controllers/Main',
resolve: {
deposits:
function(P48Wallet) {
return P48Wallet.getUserData();
}
}
})
)
.otherwise({redirectTo: '/'});
});
angularAMD.bootstrap(app);
return app;
});
================================================== ================================
main.js
require.config({
baseUrl: "static/js",
paths: {
'angular': 'libs/angular.min',
'angular-route': 'libs/angular-route.min',
'angular-animate': 'libs/angular-animate.min',
'angularAMD': 'libs/angularAMD.min'
},
shim: {
'angularAMD': ['angular'],
'angular-route': ['angular'],
'angular-animate': ['angular']
},
deps: ['application/app']
});
================================================== ================================
Controller Main.js
define(['application/app'], function (app) {
'use strict';
app.controller('Main', function ($rootScope, $location) {
var Main = {};
return $rootScope.Main = Main;
});
});
================================================== ================================
Services
1) Http.js
'use strict';
define(['application/app'], function(app) {
return app.factory('Http', function($http, $q, $location, $rootScope) {
return HTTP = {
xxx: '/',
xxx: false
};
});
});
2) P48Wallet.js
'use strict';
define(['application/app', 'application/services/Http'], function(app) {
console.log(app); //undefined
return app.factory('P48Wallet', function(Http) {
// ...
return this;
});
});