Last active
August 29, 2015 14:17
-
-
Save dshster/af922444c0810d6f7961 to your computer and use it in GitHub Desktop.
AngularJS backend-less development
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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; | |
}); | |
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "AngularMockPractics", | |
"version": "1.0.0", | |
"authors": [ | |
"Dmitry Shvalyov <[email protected]>" | |
], | |
"license": "MIT", | |
"private": true, | |
"ignore": [ | |
"**/.*", | |
"node_modules", | |
"bower_components", | |
"test", | |
"tests" | |
], | |
"dependencies": { | |
"angular": "~1.3.15", | |
"angular-mocks": "~1.3.15" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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