forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compile): '=' binding can now be optional
If you bind using '=' to a non-existant parent property, the compiler will throw a NON_ASSIGNABLE_MODEL_EXPRESSION exception, which is right because the model doesn't exist. This enhancement allow to specify that a binding is optional so it won't complain if the parent property is not defined. In order to mantain backward compability, the new behaviour must be specified using '=?' instead of '='. The local property will be undefined is these cases. Closes angular#909 Closes angular#1435
- Loading branch information
Showing
3 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1775,6 +1775,9 @@ describe('$compile', function() { | |
ref: '=', | ||
refAlias: '= ref', | ||
reference: '=', | ||
optref: '=?', | ||
optrefAlias: '=? optref', | ||
optreference: '=?', | ||
expr: '&', | ||
exprAlias: '&expr' | ||
}, | ||
|
@@ -1917,6 +1920,107 @@ describe('$compile', function() { | |
}); | ||
|
||
|
||
describe('optional object reference', function() { | ||
it('should update local when origin changes', inject(function() { | ||
This comment has been minimized.
Sorry, something went wrong.
IgorMinar
|
||
compile('<div><span my-component optref="name">'); | ||
expect(componentScope.optRef).toBe(undefined); | ||
expect(componentScope.optRefAlias).toBe(componentScope.optRef); | ||
|
||
$rootScope.name = 'misko'; | ||
$rootScope.$apply(); | ||
expect(componentScope.optref).toBe($rootScope.name); | ||
expect(componentScope.optrefAlias).toBe($rootScope.name); | ||
|
||
$rootScope.name = {}; | ||
$rootScope.$apply(); | ||
expect(componentScope.optref).toBe($rootScope.name); | ||
expect(componentScope.optrefAlias).toBe($rootScope.name); | ||
})); | ||
|
||
|
||
it('should update local when origin changes', inject(function() { | ||
compile('<div><span my-component optRef="name">'); | ||
expect(componentScope.optref).toBe(undefined); | ||
expect(componentScope.optrefAlias).toBe(componentScope.optref); | ||
|
||
componentScope.optref = 'misko'; | ||
$rootScope.$apply(); | ||
expect($rootScope.name).toBe('misko'); | ||
expect(componentScope.optref).toBe('misko'); | ||
expect($rootScope.name).toBe(componentScope.optref); | ||
expect(componentScope.optrefAlias).toBe(componentScope.optref); | ||
|
||
componentScope.name = {}; | ||
$rootScope.$apply(); | ||
expect($rootScope.name).toBe(componentScope.optref); | ||
expect(componentScope.optrefAlias).toBe(componentScope.optref); | ||
})); | ||
|
||
|
||
it('should update local when both change', inject(function() { | ||
compile('<div><span my-component optref="name">'); | ||
$rootScope.name = {mark:123}; | ||
componentScope.optref = 'misko'; | ||
|
||
$rootScope.$apply(); | ||
expect($rootScope.name).toEqual({mark:123}) | ||
expect(componentScope.optref).toBe($rootScope.name); | ||
expect(componentScope.optrefAlias).toBe($rootScope.name); | ||
|
||
$rootScope.name = 'igor'; | ||
componentScope.optref = {}; | ||
$rootScope.$apply(); | ||
expect($rootScope.name).toEqual('igor') | ||
expect(componentScope.optref).toBe($rootScope.name); | ||
expect(componentScope.optrefAlias).toBe($rootScope.name); | ||
})); | ||
|
||
it('should complain on non assignable changes', inject(function() { | ||
compile('<div><span my-component optref="\'hello \' + name">'); | ||
$rootScope.name = 'world'; | ||
$rootScope.$apply(); | ||
expect(componentScope.optref).toBe('hello world'); | ||
|
||
componentScope.optref = 'ignore me'; | ||
expect($rootScope.$apply). | ||
toThrow("Non-assignable model expression: 'hello ' + name (directive: myComponent)"); | ||
expect(componentScope.optref).toBe('hello world'); | ||
// reset since the exception was rethrown which prevented phase clearing | ||
$rootScope.$$phase = null; | ||
|
||
$rootScope.name = 'misko'; | ||
$rootScope.$apply(); | ||
expect(componentScope.optref).toBe('hello misko'); | ||
})); | ||
|
||
// regression | ||
it('should stabilize model', inject(function() { | ||
compile('<div><span my-component optreference="name">'); | ||
|
||
var lastRefValueInParent; | ||
$rootScope.$watch('name', function(ref) { | ||
lastRefValueInParent = ref; | ||
}); | ||
|
||
$rootScope.name = 'aaa'; | ||
$rootScope.$apply(); | ||
|
||
componentScope.optreference = 'new'; | ||
$rootScope.$apply(); | ||
|
||
expect(lastRefValueInParent).toBe('new'); | ||
})); | ||
|
||
it('should not throw exception when reference does not exist', inject(function() { | ||
compile('<div><span my-component>'); | ||
|
||
expect(componentScope.optref).toBe(undefined); | ||
expect(componentScope.optrefAlias).toBe(undefined); | ||
expect(componentScope.optreference).toBe(undefined); | ||
})); | ||
}); | ||
|
||
|
||
describe('executable expression', function() { | ||
it('should allow expression execution with locals', inject(function() { | ||
compile('<div><span my-component expr="count = count + offset">'); | ||
|
why null? it should be
!attrs[attrName]