rename scope.$inject to scope.$service

see changelog diff for more info
This commit is contained in:
Igor Minar 2011-01-04 11:53:23 -08:00
parent 1430c6d6b1
commit b2631f6170
14 changed files with 146 additions and 133 deletions

View file

@ -5,6 +5,9 @@
not needed. not needed.
### Breaking changes ### Breaking changes
- API for accessing registered services — `scope.$inject` — was renamed to
[`scope.$service`](http://docs.angularjs.org/#!angular.scope.$service).
- Support for `eager-published` services was removed. This change was done to make explicit - Support for `eager-published` services was removed. This change was done to make explicit
dependency declaration always required in order to allow making relatively expensive services dependency declaration always required in order to allow making relatively expensive services
lazily initialized (e.g. $cookie, $location), as well as remove 'magic' and reduce unnecessary lazily initialized (e.g. $cookie, $location), as well as remove 'magic' and reduce unnecessary
@ -21,17 +24,17 @@
- $invalidWidgets - $invalidWidgets
To temporarily preserve the 'eager-published' status for these services, you may use `ng:init` To temporarily preserve the 'eager-published' status for these services, you may use `ng:init`
(e.g. `ng:init="$location = $inject('$location'), ...`) in the view or more correctly create (e.g. `ng:init="$location = $service('$location'), ...`) in the view or more correctly create
a service like this: a service like this:
angular.service('published-svc-shim', function() { angular.service('published-svc-shim', function() {
this.$location = this.$inject('$location'); this.$location = this.$service('$location');
this.$route = this.$inject('$route'); this.$route = this.$service('$route');
this.$cookies = this.$inject('$cookies'); this.$cookies = this.$service('$cookies');
this.$window = this.$inject('$window'); this.$window = this.$service('$window');
this.$document = this.$inject('$document'); this.$document = this.$service('$document');
this.$exceptionHandler = this.$inject('$exceptionHandler'); this.$exceptionHandler = this.$service('$exceptionHandler');
this.$invalidWidgets = this.$inject('$invalidWidgets'); this.$invalidWidgets = this.$service('$invalidWidgets');
}, {$creation: 'eager'}); }, {$creation: 'eager'});
- In the light of the `eager-published` change, to complete the cleanup we renamed `$creation` - In the light of the `eager-published` change, to complete the cleanup we renamed `$creation`

View file

@ -3,8 +3,7 @@ describe('example.personalLog.LogCtrl', function() {
function createNotesCtrl() { function createNotesCtrl() {
var scope = angular.scope(); var scope = angular.scope();
var inject = scope.$inject; scope.$cookies = scope.$service('$cookies');
scope.$cookies = inject('$cookies');
return scope.$new(example.personalLog.LogCtrl); return scope.$new(example.personalLog.LogCtrl);
} }

View file

@ -947,7 +947,7 @@ function angularInit(config){
if (config.autobind) { if (config.autobind) {
// TODO default to the source of angular.js // TODO default to the source of angular.js
var scope = compile(window.document, _null, {'$config':config}), var scope = compile(window.document, _null, {'$config':config}),
$browser = scope.$inject('$browser'); $browser = scope.$service('$browser');
if (config.css) if (config.css)
$browser.addCss(config.base_url + config.css); $browser.addCss(config.base_url + config.css);

View file

@ -35,7 +35,7 @@ Template.prototype = {
foreach(this.inits, function(fn) { foreach(this.inits, function(fn) {
queue.push(function() { queue.push(function() {
childScope.$tryEval(function(){ childScope.$tryEval(function(){
return childScope.$inject(fn, childScope, element); return childScope.$service(fn, childScope, element);
}, element); }, element);
}); });
}); });

View file

@ -578,7 +578,7 @@ function createScope(parent, providers, instanceCache) {
foreach(Class.prototype, function(fn, name){ foreach(Class.prototype, function(fn, name){
instance[name] = bind(instance, fn); instance[name] = bind(instance, fn);
}); });
instance.$inject.apply(instance, concat([Class, instance], arguments, 1)); instance.$service.apply(instance, concat([Class, instance], arguments, 1));
//TODO: backwards compatibility hack, remove when we don't depend on init methods //TODO: backwards compatibility hack, remove when we don't depend on init methods
if (isFunction(Class.prototype.init)) { if (isFunction(Class.prototype.init)) {
@ -615,7 +615,23 @@ function createScope(parent, providers, instanceCache) {
if (!parent.$root) { if (!parent.$root) {
instance.$root = instance; instance.$root = instance;
instance.$parent = instance; instance.$parent = instance;
(instance.$inject = createInjector(instance, providers, instanceCache))();
/**
* @workInProgress
* @ngdoc function
* @name angular.scope.$service
* @function
*
* @description
* Provides access to angular's dependency injector and
* {@link angular.service registered services}. In general the use of this api is discouraged,
* except for tests and components that currently don't support dependency injection (widgets,
* filters, etc).
*
* @param {string} serviceId String ID of the service to return.
* @returns {*} Value, object or function returned by the service factory function if any.
*/
(instance.$service = createInjector(instance, providers, instanceCache))();
} }
return instance; return instance;

View file

@ -383,7 +383,7 @@ extend(angularValidator, {
cache.current = input; cache.current = input;
var inputState = cache.inputs[input], var inputState = cache.inputs[input],
$invalidWidgets = scope.$inject('$invalidWidgets'); $invalidWidgets = scope.$service('$invalidWidgets');
if (!inputState) { if (!inputState) {
cache.inputs[input] = inputState = { inFlight: true }; cache.inputs[input] = inputState = { inFlight: true };

View file

@ -271,7 +271,7 @@ function valueAccessor(scope, element) {
formatterName = element.attr('ng:format') || NOOP, formatterName = element.attr('ng:format') || NOOP,
formatter = angularFormatter(formatterName), formatter = angularFormatter(formatterName),
format, parse, lastError, required, format, parse, lastError, required,
invalidWidgets = scope.$inject('$invalidWidgets') || {markValid:noop, markInvalid:noop}; invalidWidgets = scope.$service('$invalidWidgets') || {markValid:noop, markInvalid:noop};
if (!validator) throw "Validator named '" + validatorName + "' not found."; if (!validator) throw "Validator named '" + validatorName + "' not found.";
if (!formatter) throw "Formatter named '" + formatterName + "' not found."; if (!formatter) throw "Formatter named '" + formatterName + "' not found.";
format = formatter.format; format = formatter.format;

View file

@ -322,7 +322,7 @@ describe('angular service', function() {
angular.service('fake', function() { return 'old'; }); angular.service('fake', function() { return 'old'; });
angular.service('fake', function() { return 'new'; }); angular.service('fake', function() { return 'new'; });
expect(scope.$inject('fake')).toEqual('new'); expect(scope.$service('fake')).toEqual('new');
}); });
it('should preserve $ properties on override', function() { it('should preserve $ properties on override', function() {

View file

@ -515,38 +515,38 @@ BinderTest.prototype.testValidateForm = function() {
var items = [{}, {}]; var items = [{}, {}];
c.scope.$set("items", items); c.scope.$set("items", items);
c.scope.$eval(); c.scope.$eval();
assertEquals(3, c.scope.$inject('$invalidWidgets').length); assertEquals(3, c.scope.$service('$invalidWidgets').length);
c.scope.$set('name', ''); c.scope.$set('name', '');
c.scope.$eval(); c.scope.$eval();
assertEquals(3, c.scope.$inject('$invalidWidgets').length); assertEquals(3, c.scope.$service('$invalidWidgets').length);
c.scope.$set('name', ' '); c.scope.$set('name', ' ');
c.scope.$eval(); c.scope.$eval();
assertEquals(3, c.scope.$inject('$invalidWidgets').length); assertEquals(3, c.scope.$service('$invalidWidgets').length);
c.scope.$set('name', 'abc'); c.scope.$set('name', 'abc');
c.scope.$eval(); c.scope.$eval();
assertEquals(2, c.scope.$inject('$invalidWidgets').length); assertEquals(2, c.scope.$service('$invalidWidgets').length);
items[0].name = 'abc'; items[0].name = 'abc';
c.scope.$eval(); c.scope.$eval();
assertEquals(1, c.scope.$inject('$invalidWidgets').length); assertEquals(1, c.scope.$service('$invalidWidgets').length);
items[1].name = 'abc'; items[1].name = 'abc';
c.scope.$eval(); c.scope.$eval();
assertEquals(0, c.scope.$inject('$invalidWidgets').length); assertEquals(0, c.scope.$service('$invalidWidgets').length);
}; };
BinderTest.prototype.testValidateOnlyVisibleItems = function(){ BinderTest.prototype.testValidateOnlyVisibleItems = function(){
var c = this.compile('<div><input name="name" ng:required><input ng:show="show" name="name" ng:required></div>', undefined, jqLite(document.body)); var c = this.compile('<div><input name="name" ng:required><input ng:show="show" name="name" ng:required></div>', undefined, jqLite(document.body));
c.scope.$set("show", true); c.scope.$set("show", true);
c.scope.$eval(); c.scope.$eval();
assertEquals(2, c.scope.$inject('$invalidWidgets').length); assertEquals(2, c.scope.$service('$invalidWidgets').length);
c.scope.$set("show", false); c.scope.$set("show", false);
c.scope.$eval(); c.scope.$eval();
assertEquals(1, c.scope.$inject('$invalidWidgets').visible()); assertEquals(1, c.scope.$service('$invalidWidgets').visible());
}; };
BinderTest.prototype.testDeleteAttributeIfEvaluatesFalse = function() { BinderTest.prototype.testDeleteAttributeIfEvaluatesFalse = function() {

View file

@ -162,8 +162,8 @@ describe("resource", function() {
it('should excersize full stack', function(){ it('should excersize full stack', function(){
var scope = angular.compile('<div></div>'); var scope = angular.compile('<div></div>');
var $browser = scope.$inject('$browser'); var $browser = scope.$service('$browser');
var $resource = scope.$inject('$resource'); var $resource = scope.$service('$resource');
var Person = $resource('/Person/:id'); var Person = $resource('/Person/:id');
$browser.xhr.expectGET('/Person/123').respond('\n{\n"name":\n"misko"\n}\n'); $browser.xhr.expectGET('/Person/123').respond('\n{\n"name":\n"misko"\n}\n');
var person = Person.get({id:123}); var person = Person.get({id:123});
@ -174,8 +174,8 @@ describe("resource", function() {
it('should return the same object when verifying the cache', function(){ it('should return the same object when verifying the cache', function(){
var scope = angular.compile('<div></div>'); var scope = angular.compile('<div></div>');
var $browser = scope.$inject('$browser'); var $browser = scope.$service('$browser');
var $resource = scope.$inject('$resource'); var $resource = scope.$service('$resource');
var Person = $resource('/Person/:id', null, {query: {method:'GET', isArray: true, verifyCache: true}}); var Person = $resource('/Person/:id', null, {query: {method:'GET', isArray: true, verifyCache: true}});
$browser.xhr.expectGET('/Person/123').respond('[\n{\n"name":\n"misko"\n}\n]'); $browser.xhr.expectGET('/Person/123').respond('[\n{\n"name":\n"misko"\n}\n]');
var person = Person.query({id:123}); var person = Person.query({id:123});

View file

@ -42,7 +42,7 @@ describe("ScenarioSpec: Compilation", function(){
it("should have $ objects", function(){ it("should have $ objects", function(){
scope = compile('<div></div>', {$config: {a:"b"}}); scope = compile('<div></div>', {$config: {a:"b"}});
expect(scope.$inject('$location')).toBeDefined(); expect(scope.$service('$location')).toBeDefined();
expect(scope.$get('$eval')).toBeDefined(); expect(scope.$get('$eval')).toBeDefined();
expect(scope.$get('$config')).toBeDefined(); expect(scope.$get('$config')).toBeDefined();
expect(scope.$get('$config.a')).toEqual("b"); expect(scope.$get('$config.a')).toEqual("b");
@ -53,8 +53,8 @@ describe("ScenarioSpec: Compilation", function(){
it("should take location object", function(){ it("should take location object", function(){
var url = "http://server/#?book=moby"; var url = "http://server/#?book=moby";
scope = compile("<div>{{$location}}</div>"); scope = compile("<div>{{$location}}</div>");
var $location = scope.$inject('$location'); var $location = scope.$service('$location');
var $browser = scope.$inject('$browser'); var $browser = scope.$service('$browser');
expect($location.hashSearch.book).toBeUndefined(); expect($location.hashSearch.book).toBeUndefined();
$browser.setUrl(url); $browser.setUrl(url);
$browser.poll(); $browser.poll();

View file

@ -128,7 +128,7 @@ describe('Validator:asynchronous', function(){
it("should not make second request to same value", function(){ it("should not make second request to same value", function(){
asynchronous.call(self, "kai", function(v,f){value=v; fn=f;}); asynchronous.call(self, "kai", function(v,f){value=v; fn=f;});
expect(value).toEqual('kai'); expect(value).toEqual('kai');
expect(self.$inject('$invalidWidgets')[0]).toEqual(self.$element); expect(self.$service('$invalidWidgets')[0]).toEqual(self.$element);
var spy = jasmine.createSpy(); var spy = jasmine.createSpy();
asynchronous.call(self, "kai", spy); asynchronous.call(self, "kai", spy);

View file

@ -1,5 +1,5 @@
describe("service", function(){ describe("service", function(){
var scope, $xhrError, $log, mockServices, inject, $browser, $browserXhr, $xhrBulk, $xhr, $route; var scope, $xhrError, $log, mockServices, $browser, $browserXhr, $xhrBulk, $xhr;
beforeEach(function(){ beforeEach(function(){
$xhrError = jasmine.createSpy('$xhr.error'); $xhrError = jasmine.createSpy('$xhr.error');
@ -8,13 +8,10 @@ describe("service", function(){
'$xhr.error': $xhrError, '$xhr.error': $xhrError,
'$log': $log '$log': $log
}); });
inject = scope.$inject; $browser = scope.$service('$browser');
$browser = inject('$browser');
$browserXhr = $browser.xhr; $browserXhr = $browser.xhr;
$xhrBulk = scope.$inject('$xhr.bulk'); $xhrBulk = scope.$service('$xhr.bulk');
$xhr = scope.$inject('$xhr'); $xhr = scope.$service('$xhr');
$route = scope.$inject('$route');
scope.$location = inject('$location');
}); });
afterEach(function(){ afterEach(function(){
@ -24,7 +21,7 @@ describe("service", function(){
it("should inject $window", function(){ it("should inject $window", function(){
expect(scope.$inject('$window')).toEqual(window); expect(scope.$service('$window')).toEqual(window);
}); });
xit('should add stylesheets', function(){ xit('should add stylesheets', function(){
@ -45,7 +42,7 @@ describe("service", function(){
function info(){ logger+= 'info;'; } function info(){ logger+= 'info;'; }
function error(){ logger+= 'error;'; } function error(){ logger+= 'error;'; }
var scope = createScope({}, angularService, {$window: {console:{log:log, warn:warn, info:info, error:error}}, $document:[{cookie:''}]}), var scope = createScope({}, angularService, {$window: {console:{log:log, warn:warn, info:info, error:error}}, $document:[{cookie:''}]}),
$log = scope.$inject('$log'); $log = scope.$service('$log');
$log.log(); $log.log();
$log.warn(); $log.warn();
$log.info(); $log.info();
@ -57,7 +54,7 @@ describe("service", function(){
var logger = ""; var logger = "";
function log(){ logger+= 'log;'; } function log(){ logger+= 'log;'; }
var scope = createScope({}, angularService, {$window: {console:{log:log}}, $document:[{cookie:''}]}); var scope = createScope({}, angularService, {$window: {console:{log:log}}, $document:[{cookie:''}]});
var $log = scope.$inject('$log'); var $log = scope.$service('$log');
$log.log(); $log.log();
$log.warn(); $log.warn();
$log.info(); $log.info();
@ -67,7 +64,7 @@ describe("service", function(){
it('should use noop if no console', function(){ it('should use noop if no console', function(){
var scope = createScope({}, angularService, {$window: {}, $document:[{cookie:''}]}), var scope = createScope({}, angularService, {$window: {}, $document:[{cookie:''}]}),
$log = scope.$inject('$log'); $log = scope.$service('$log');
$log.log(); $log.log();
$log.warn(); $log.warn();
$log.info(); $log.info();
@ -115,98 +112,100 @@ describe("service", function(){
it('should log errors', function(){ it('should log errors', function(){
var error = ''; var error = '';
$log.error = function(m) { error += m; }; $log.error = function(m) { error += m; };
scope.$inject('$exceptionHandler')('myError'); scope.$service('$exceptionHandler')('myError');
expect(error).toEqual('myError'); expect(error).toEqual('myError');
}); });
}); });
describe("$location", function(){ describe("$location", function(){
it("should inject $location", function() { var $location;
expect(scope.$location).toBeDefined();
beforeEach(function() {
$location = scope.$service('$location');
}); });
it("update should update location object immediately", function() { it("update should update location object immediately", function() {
var href = 'http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2='; var href = 'http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2=';
scope.$location.update(href); $location.update(href);
expect(scope.$location.href).toEqual(href); expect($location.href).toEqual(href);
expect(scope.$location.protocol).toEqual("http"); expect($location.protocol).toEqual("http");
expect(scope.$location.host).toEqual("host"); expect($location.host).toEqual("host");
expect(scope.$location.port).toEqual("123"); expect($location.port).toEqual("123");
expect(scope.$location.path).toEqual("/p/a/t/h.html"); expect($location.path).toEqual("/p/a/t/h.html");
expect(scope.$location.search).toEqual({query:'value'}); expect($location.search).toEqual({query:'value'});
expect(scope.$location.hash).toEqual('path?key=value&flag&key2='); expect($location.hash).toEqual('path?key=value&flag&key2=');
expect(scope.$location.hashPath).toEqual('path'); expect($location.hashPath).toEqual('path');
expect(scope.$location.hashSearch).toEqual({key: 'value', flag: true, key2: ''}); expect($location.hashSearch).toEqual({key: 'value', flag: true, key2: ''});
}); });
it('toString() should return actual representation', function() { it('toString() should return actual representation', function() {
var href = 'http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2='; var href = 'http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2=';
scope.$location.update(href); $location.update(href);
expect(scope.$location.toString()).toEqual(href); expect($location.toString()).toEqual(href);
scope.$eval(); scope.$eval();
scope.$location.host = 'new'; $location.host = 'new';
scope.$location.path = ''; $location.path = '';
expect(scope.$location.toString()).toEqual('http://new:123?query=value#path?key=value&flag&key2='); expect($location.toString()).toEqual('http://new:123?query=value#path?key=value&flag&key2=');
}); });
it('toString() should not update browser', function() { it('toString() should not update browser', function() {
var url = $browser.getUrl(); var url = $browser.getUrl();
scope.$location.update('http://www.angularjs.org'); $location.update('http://www.angularjs.org');
expect(scope.$location.toString()).toEqual('http://www.angularjs.org'); expect($location.toString()).toEqual('http://www.angularjs.org');
expect($browser.getUrl()).toEqual(url); expect($browser.getUrl()).toEqual(url);
}); });
it('should update browser at the end of $eval', function() { it('should update browser at the end of $eval', function() {
var url = $browser.getUrl(); var url = $browser.getUrl();
scope.$location.update('http://www.angularjs.org/'); $location.update('http://www.angularjs.org/');
scope.$location.update({path: '/a/b'}); $location.update({path: '/a/b'});
expect(scope.$location.toString()).toEqual('http://www.angularjs.org/a/b'); expect($location.toString()).toEqual('http://www.angularjs.org/a/b');
expect($browser.getUrl()).toEqual(url); expect($browser.getUrl()).toEqual(url);
scope.$eval(); scope.$eval();
expect($browser.getUrl()).toEqual('http://www.angularjs.org/a/b'); expect($browser.getUrl()).toEqual('http://www.angularjs.org/a/b');
}); });
it('should update hashPath and hashSearch on hash update', function(){ it('should update hashPath and hashSearch on hash update', function(){
scope.$location.update('http://server/#path?a=b'); $location.update('http://server/#path?a=b');
scope.$eval(); scope.$eval();
scope.$location.update({hash: ''}); $location.update({hash: ''});
expect(scope.$location.hashPath).toEqual(''); expect($location.hashPath).toEqual('');
expect(scope.$location.hashSearch).toEqual({}); expect($location.hashSearch).toEqual({});
}); });
it('should update hash on hashPath or hashSearch update', function() { it('should update hash on hashPath or hashSearch update', function() {
scope.$location.update('http://server/#path?a=b'); $location.update('http://server/#path?a=b');
scope.$eval(); scope.$eval();
scope.$location.update({hashPath: '', hashSearch: {}}); $location.update({hashPath: '', hashSearch: {}});
expect(scope.$location.hash).toEqual(''); expect($location.hash).toEqual('');
}); });
it('should update hashPath and hashSearch on hash property change', function(){ it('should update hashPath and hashSearch on hash property change', function(){
scope.$location.update('http://server/#path?a=b'); $location.update('http://server/#path?a=b');
scope.$eval(); scope.$eval();
scope.$location.hash = ''; $location.hash = '';
expect(scope.$location.toString()).toEqual('http://server/'); expect($location.toString()).toEqual('http://server/');
expect(scope.$location.hashPath).toEqual(''); expect($location.hashPath).toEqual('');
expect(scope.$location.hashSearch).toEqual({}); expect($location.hashSearch).toEqual({});
}); });
it('should update hash on hashPath or hashSearch property change', function() { it('should update hash on hashPath or hashSearch property change', function() {
scope.$location.update('http://server/#path?a=b'); $location.update('http://server/#path?a=b');
scope.$eval(); scope.$eval();
scope.$location.hashPath = ''; $location.hashPath = '';
scope.$location.hashSearch = {}; $location.hashSearch = {};
expect(scope.$location.toString()).toEqual('http://server/'); expect($location.toString()).toEqual('http://server/');
expect(scope.$location.hash).toEqual(''); expect($location.hash).toEqual('');
}); });
it('should update hash before any processing', function(){ it('should update hash before any processing', function(){
scope = compile('<div>'); scope = compile('<div>');
scope.$location = scope.$inject('$location'); scope.$location = scope.$service('$location');
var log = ''; var log = '';
scope.$watch('$location.hash', function(){ scope.$watch('$location.hash', function(){
log += this.$location.hashPath + ';'; log += this.$location.hashPath + ';';
@ -220,37 +219,37 @@ describe("service", function(){
}); });
it('udpate() should accept hash object and update only given properties', function() { it('udpate() should accept hash object and update only given properties', function() {
scope.$location.update("http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2="); $location.update("http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2=");
scope.$location.update({host: 'new', port: 24}); $location.update({host: 'new', port: 24});
expect(scope.$location.host).toEqual('new'); expect($location.host).toEqual('new');
expect(scope.$location.port).toEqual(24); expect($location.port).toEqual(24);
expect(scope.$location.protocol).toEqual('http'); expect($location.protocol).toEqual('http');
expect(scope.$location.href).toEqual("http://new:24/p/a/t/h.html?query=value#path?key=value&flag&key2="); expect($location.href).toEqual("http://new:24/p/a/t/h.html?query=value#path?key=value&flag&key2=");
}); });
it('updateHash() should accept one string argument to update path', function() { it('updateHash() should accept one string argument to update path', function() {
scope.$location.updateHash('path'); $location.updateHash('path');
expect(scope.$location.hash).toEqual('path'); expect($location.hash).toEqual('path');
expect(scope.$location.hashPath).toEqual('path'); expect($location.hashPath).toEqual('path');
}); });
it('updateHash() should accept one hash argument to update search', function() { it('updateHash() should accept one hash argument to update search', function() {
scope.$location.updateHash({a: 'b'}); $location.updateHash({a: 'b'});
expect(scope.$location.hash).toEqual('?a=b'); expect($location.hash).toEqual('?a=b');
expect(scope.$location.hashSearch).toEqual({a: 'b'}); expect($location.hashSearch).toEqual({a: 'b'});
}); });
it('updateHash() should accept path and search both', function() { it('updateHash() should accept path and search both', function() {
scope.$location.updateHash('path', {a: 'b'}); $location.updateHash('path', {a: 'b'});
expect(scope.$location.hash).toEqual('path?a=b'); expect($location.hash).toEqual('path?a=b');
expect(scope.$location.hashSearch).toEqual({a: 'b'}); expect($location.hashSearch).toEqual({a: 'b'});
expect(scope.$location.hashPath).toEqual('path'); expect($location.hashPath).toEqual('path');
}); });
it('should remove # if hash is empty', function() { it('should remove # if hash is empty', function() {
scope.$location.update('http://www.angularjs.org/index.php#'); $location.update('http://www.angularjs.org/index.php#');
expect(scope.$location.href).toEqual('http://www.angularjs.org/index.php'); expect($location.href).toEqual('http://www.angularjs.org/index.php');
}); });
it('should not change browser\'s url with empty hash', function() { it('should not change browser\'s url with empty hash', function() {
@ -266,7 +265,7 @@ describe("service", function(){
scope = compile('<input name="price" ng:required ng:validate="number"></input>'); scope = compile('<input name="price" ng:required ng:validate="number"></input>');
jqLite(document.body).append(scope.$element); jqLite(document.body).append(scope.$element);
scope.$init(); scope.$init();
var $invalidWidgets = scope.$inject('$invalidWidgets'); var $invalidWidgets = scope.$service('$invalidWidgets');
expect($invalidWidgets.length).toEqual(1); expect($invalidWidgets.length).toEqual(1);
scope.price = 123; scope.price = 123;
@ -292,19 +291,21 @@ describe("service", function(){
describe("$route", function(){ describe("$route", function(){
it('should route and fire change event', function(){ it('should route and fire change event', function(){
var log = ''; var log = '',
$location, $route;
function BookChapter() { function BookChapter() {
this.log = '<init>'; this.log = '<init>';
} }
scope = compile('<div></div>').$init(); scope = compile('<div></div>').$init();
scope.$location = scope.$inject('$location'); $location = scope.$service('$location');
$route = scope.$inject('$route'); $route = scope.$service('$route');
$route.when('/Book/:book/Chapter/:chapter', {controller: BookChapter, template:'Chapter.html'}); $route.when('/Book/:book/Chapter/:chapter', {controller: BookChapter, template:'Chapter.html'});
$route.when('/Blank'); $route.when('/Blank');
$route.onChange(function(){ $route.onChange(function(){
log += 'onChange();'; log += 'onChange();';
}); });
scope.$location.update('http://server#/Book/Moby/Chapter/Intro?p=123'); $location.update('http://server#/Book/Moby/Chapter/Intro?p=123');
scope.$eval(); scope.$eval();
expect(log).toEqual('onChange();'); expect(log).toEqual('onChange();');
expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'}); expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'});
@ -312,14 +313,14 @@ describe("service", function(){
var lastId = $route.current.scope.$id; var lastId = $route.current.scope.$id;
log = ''; log = '';
scope.$location.update('http://server#/Blank?ignore'); $location.update('http://server#/Blank?ignore');
scope.$eval(); scope.$eval();
expect(log).toEqual('onChange();'); expect(log).toEqual('onChange();');
expect($route.current.params).toEqual({ignore:true}); expect($route.current.params).toEqual({ignore:true});
expect($route.current.scope.$id).not.toEqual(lastId); expect($route.current.scope.$id).not.toEqual(lastId);
log = ''; log = '';
scope.$location.update('http://server#/NONE'); $location.update('http://server#/NONE');
scope.$eval(); scope.$eval();
expect(log).toEqual('onChange();'); expect(log).toEqual('onChange();');
expect($route.current).toEqual(null); expect($route.current).toEqual(null);
@ -330,12 +331,6 @@ describe("service", function(){
}); });
}); });
describe('$resource', function(){
it('should publish to root scope', function(){
expect(scope.$inject('$resource')).toBeTruthy();
});
});
describe('$defer', function() { describe('$defer', function() {
var $defer, $exceptionHandler; var $defer, $exceptionHandler;
@ -345,9 +340,9 @@ describe("service", function(){
'$exceptionHandler': jasmine.createSpy('$exceptionHandler') '$exceptionHandler': jasmine.createSpy('$exceptionHandler')
}); });
$browser = scope.$inject('$browser'); $browser = scope.$service('$browser');
$defer = scope.$inject('$defer'); $defer = scope.$service('$defer');
$exceptionHandler = scope.$inject('$exceptionHandler'); $exceptionHandler = scope.$service('$exceptionHandler');
}); });
@ -497,7 +492,7 @@ describe("service", function(){
describe('cache', function(){ describe('cache', function(){
var cache; var cache;
beforeEach(function(){ cache = scope.$inject('$xhr.cache'); }); beforeEach(function(){ cache = scope.$service('$xhr.cache'); });
it('should cache requests', function(){ it('should cache requests', function(){
$browserXhr.expectGET('/url').respond('first'); $browserXhr.expectGET('/url').respond('first');
@ -541,7 +536,7 @@ describe("service", function(){
}); });
it('should keep track of in flight requests and request only once', function(){ it('should keep track of in flight requests and request only once', function(){
scope.$inject('$xhr.bulk').urls['/bulk'] = { scope.$service('$xhr.bulk').urls['/bulk'] = {
match:function(url){ match:function(url){
return url == '/url'; return url == '/url';
} }
@ -611,7 +606,7 @@ describe("service", function(){
$browser = new MockBrowser(); $browser = new MockBrowser();
$browser.cookieHash['preexisting'] = 'oldCookie'; $browser.cookieHash['preexisting'] = 'oldCookie';
scope = createScope(null, angularService, {$browser: $browser}); scope = createScope(null, angularService, {$browser: $browser});
scope.$cookies = scope.$inject('$cookies'); scope.$cookies = scope.$service('$cookies');
}); });
@ -709,7 +704,7 @@ describe("service", function(){
describe('$cookieStore', function() { describe('$cookieStore', function() {
it('should serialize objects to json', function() { it('should serialize objects to json', function() {
scope.$inject('$cookieStore').put('objectCookie', {id: 123, name: 'blah'}); scope.$service('$cookieStore').put('objectCookie', {id: 123, name: 'blah'});
scope.$eval(); //force eval in test scope.$eval(); //force eval in test
expect($browser.cookies()).toEqual({'objectCookie': '{"id":123,"name":"blah"}'}); expect($browser.cookies()).toEqual({'objectCookie': '{"id":123,"name":"blah"}'});
}); });
@ -718,12 +713,12 @@ describe("service", function(){
it('should deserialize json to object', function() { it('should deserialize json to object', function() {
$browser.cookies('objectCookie', '{"id":123,"name":"blah"}'); $browser.cookies('objectCookie', '{"id":123,"name":"blah"}');
$browser.poll(); $browser.poll();
expect(scope.$inject('$cookieStore').get('objectCookie')).toEqual({id: 123, name: 'blah'}); expect(scope.$service('$cookieStore').get('objectCookie')).toEqual({id: 123, name: 'blah'});
}); });
it('should delete objects from the store when remove is called', function() { it('should delete objects from the store when remove is called', function() {
scope.$inject('$cookieStore').put('gonner', { "I'll":"Be Back"}); scope.$service('$cookieStore').put('gonner', { "I'll":"Be Back"});
scope.$eval(); //force eval in test scope.$eval(); //force eval in test
expect($browser.cookies()).toEqual({'gonner': '{"I\'ll":"Be Back"}'}); expect($browser.cookies()).toEqual({'gonner': '{"I\'ll":"Be Back"}'});
}); });

View file

@ -551,9 +551,9 @@ describe("widget", function(){
scope.childScope = createScope(); scope.childScope = createScope();
scope.childScope.name = 'misko'; scope.childScope.name = 'misko';
scope.url = 'myUrl'; scope.url = 'myUrl';
scope.$inject('$xhr.cache').data.myUrl = {value:'{{name}}'}; scope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'};
scope.$init(); scope.$init();
scope.$inject('$browser').defer.flush(); scope.$service('$browser').defer.flush();
expect(element.text()).toEqual('misko'); expect(element.text()).toEqual('misko');
dealoc(scope); dealoc(scope);
}); });
@ -564,9 +564,9 @@ describe("widget", function(){
scope.childScope = createScope(); scope.childScope = createScope();
scope.childScope.name = 'igor'; scope.childScope.name = 'igor';
scope.url = 'myUrl'; scope.url = 'myUrl';
scope.$inject('$xhr.cache').data.myUrl = {value:'{{name}}'}; scope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'};
scope.$init(); scope.$init();
scope.$inject('$browser').defer.flush(); scope.$service('$browser').defer.flush();
expect(element.text()).toEqual('igor'); expect(element.text()).toEqual('igor');
@ -581,9 +581,9 @@ describe("widget", function(){
var element = jqLite('<ng:include src="url" scope="this"></ng:include>'); var element = jqLite('<ng:include src="url" scope="this"></ng:include>');
var scope = angular.compile(element); var scope = angular.compile(element);
scope.url = 'myUrl'; scope.url = 'myUrl';
scope.$inject('$xhr.cache').data.myUrl = {value:'{{c=c+1}}'}; scope.$service('$xhr.cache').data.myUrl = {value:'{{c=c+1}}'};
scope.$init(); scope.$init();
scope.$inject('$browser').defer.flush(); scope.$service('$browser').defer.flush();
// this one should really be just '1', but due to lack of real events things are not working // this one should really be just '1', but due to lack of real events things are not working
// properly. see discussion at: http://is.gd/ighKk // properly. see discussion at: http://is.gd/ighKk
@ -598,9 +598,9 @@ describe("widget", function(){
expect(scope.loaded).not.toBeDefined(); expect(scope.loaded).not.toBeDefined();
scope.url = 'myUrl'; scope.url = 'myUrl';
scope.$inject('$xhr.cache').data.myUrl = {value:'my partial'}; scope.$service('$xhr.cache').data.myUrl = {value:'my partial'};
scope.$init(); scope.$init();
scope.$inject('$browser').defer.flush(); scope.$service('$browser').defer.flush();
expect(element.text()).toEqual('my partial'); expect(element.text()).toEqual('my partial');
expect(scope.loaded).toBe(true); expect(scope.loaded).toBe(true);
dealoc(element); dealoc(element);