Skip to content

Instantly share code, notes, and snippets.

@dshster
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dshster/af922444c0810d6f7961 to your computer and use it in GitHub Desktop.
Save dshster/af922444c0810d6f7961 to your computer and use it in GitHub Desktop.
AngularJS backend-less development
'use strict';
var application = 'application';
var dependences = ['ngMockE2E'];
angular.module(application, dependences)
.config(['$compileProvider', function($compileProvider) {
$compileProvider.debugInfoEnabled(false);
}])
.run(['$httpBackend', 'BackendDataFactory', function($httpBackend, BackendDataFactory) {
$httpBackend.whenGET(/\/data\/\d+/).respond(function(method, url) {
var dataId = url.split('/')[2];
return [200, BackendDataFactory.getBackendData(dataId), {}];
});
$httpBackend.whenGET().passThrough();
}])
.factory('BackendDataFactory', [function() {
var list = [{
name: 'foo'
}, {
name: 'bar'
}];
return {
getBackendData: function(dataId) {
return {
dataId: dataId,
list: list
};
}
};
}])
.factory('DataFactory', ['$q', '$http', function($q, $http) {
return {
getData: function(dataId) {
return $q(function(resolve) {
$http.get('/data/' + parseInt(dataId, 10)).success(resolve);
});
},
getHtmlTemplate: function() {
return $q(function(resolve) {
$http.get('.').success(resolve);
});
}
};
}])
.controller('PracticeDataController', ['$scope', 'DataFactory', function($scope, DataFactory) {
var controller = this;
var dataId = Math.random() * 250;
DataFactory.getData(dataId).then(function(data) {
controller.list = data;
});
DataFactory.getHtmlTemplate().then(function(html) {
controller.template = html;
});
}]);
{
"name": "AngularMockPractics",
"version": "1.0.0",
"authors": [
"Dmitry Shvalyov <dmirty@shvalyov.ru>"
],
"license": "MIT",
"private": true,
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"angular": "~1.3.15",
"angular-mocks": "~1.3.15"
}
}
<!doctype html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<script src="application/components/angular/angular.min.js"></script>
<script src="application/components/angular-mocks/angular-mocks.js"></script>
<script src="application/application.js"></script>
<title>Angular-mock practics</title>
</head>
<body ng-app="application">
<div ng-controller="PracticeDataController as Data">
{{ Data.list }}
{{ Data.template }}
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment