@@ -1775,6 +1775,9 @@ describe('$compile', function() {
1775
1775
ref : '=' ,
1776
1776
refAlias : '= ref' ,
1777
1777
reference : '=' ,
1778
+ optref : '=?' ,
1779
+ optrefAlias : '=? optref' ,
1780
+ optreference : '=?' ,
1778
1781
expr : '&' ,
1779
1782
exprAlias : '&expr'
1780
1783
} ,
@@ -1917,6 +1920,107 @@ describe('$compile', function() {
1917
1920
} ) ;
1918
1921
1919
1922
1923
+ describe ( 'optional object reference' , function ( ) {
1924
+ it ( 'should update local when origin changes' , inject ( function ( ) {
1925
+ compile ( '<div><span my-component optref="name">' ) ;
1926
+ expect ( componentScope . optRef ) . toBe ( undefined ) ;
1927
+ expect ( componentScope . optRefAlias ) . toBe ( componentScope . optRef ) ;
1928
+
1929
+ $rootScope . name = 'misko' ;
1930
+ $rootScope . $apply ( ) ;
1931
+ expect ( componentScope . optref ) . toBe ( $rootScope . name ) ;
1932
+ expect ( componentScope . optrefAlias ) . toBe ( $rootScope . name ) ;
1933
+
1934
+ $rootScope . name = { } ;
1935
+ $rootScope . $apply ( ) ;
1936
+ expect ( componentScope . optref ) . toBe ( $rootScope . name ) ;
1937
+ expect ( componentScope . optrefAlias ) . toBe ( $rootScope . name ) ;
1938
+ } ) ) ;
1939
+
1940
+
1941
+ it ( 'should update local when origin changes' , inject ( function ( ) {
1942
+ compile ( '<div><span my-component optRef="name">' ) ;
1943
+ expect ( componentScope . optref ) . toBe ( undefined ) ;
1944
+ expect ( componentScope . optrefAlias ) . toBe ( componentScope . optref ) ;
1945
+
1946
+ componentScope . optref = 'misko' ;
1947
+ $rootScope . $apply ( ) ;
1948
+ expect ( $rootScope . name ) . toBe ( 'misko' ) ;
1949
+ expect ( componentScope . optref ) . toBe ( 'misko' ) ;
1950
+ expect ( $rootScope . name ) . toBe ( componentScope . optref ) ;
1951
+ expect ( componentScope . optrefAlias ) . toBe ( componentScope . optref ) ;
1952
+
1953
+ componentScope . name = { } ;
1954
+ $rootScope . $apply ( ) ;
1955
+ expect ( $rootScope . name ) . toBe ( componentScope . optref ) ;
1956
+ expect ( componentScope . optrefAlias ) . toBe ( componentScope . optref ) ;
1957
+ } ) ) ;
1958
+
1959
+
1960
+ it ( 'should update local when both change' , inject ( function ( ) {
1961
+ compile ( '<div><span my-component optref="name">' ) ;
1962
+ $rootScope . name = { mark :123 } ;
1963
+ componentScope . optref = 'misko' ;
1964
+
1965
+ $rootScope . $apply ( ) ;
1966
+ expect ( $rootScope . name ) . toEqual ( { mark :123 } )
1967
+ expect ( componentScope . optref ) . toBe ( $rootScope . name ) ;
1968
+ expect ( componentScope . optrefAlias ) . toBe ( $rootScope . name ) ;
1969
+
1970
+ $rootScope . name = 'igor' ;
1971
+ componentScope . optref = { } ;
1972
+ $rootScope . $apply ( ) ;
1973
+ expect ( $rootScope . name ) . toEqual ( 'igor' )
1974
+ expect ( componentScope . optref ) . toBe ( $rootScope . name ) ;
1975
+ expect ( componentScope . optrefAlias ) . toBe ( $rootScope . name ) ;
1976
+ } ) ) ;
1977
+
1978
+ it ( 'should complain on non assignable changes' , inject ( function ( ) {
1979
+ compile ( '<div><span my-component optref="\'hello \' + name">' ) ;
1980
+ $rootScope . name = 'world' ;
1981
+ $rootScope . $apply ( ) ;
1982
+ expect ( componentScope . optref ) . toBe ( 'hello world' ) ;
1983
+
1984
+ componentScope . optref = 'ignore me' ;
1985
+ expect ( $rootScope . $apply ) .
1986
+ toThrow ( "Non-assignable model expression: 'hello ' + name (directive: myComponent)" ) ;
1987
+ expect ( componentScope . optref ) . toBe ( 'hello world' ) ;
1988
+ // reset since the exception was rethrown which prevented phase clearing
1989
+ $rootScope . $$phase = null ;
1990
+
1991
+ $rootScope . name = 'misko' ;
1992
+ $rootScope . $apply ( ) ;
1993
+ expect ( componentScope . optref ) . toBe ( 'hello misko' ) ;
1994
+ } ) ) ;
1995
+
1996
+ // regression
1997
+ it ( 'should stabilize model' , inject ( function ( ) {
1998
+ compile ( '<div><span my-component optreference="name">' ) ;
1999
+
2000
+ var lastRefValueInParent ;
2001
+ $rootScope . $watch ( 'name' , function ( ref ) {
2002
+ lastRefValueInParent = ref ;
2003
+ } ) ;
2004
+
2005
+ $rootScope . name = 'aaa' ;
2006
+ $rootScope . $apply ( ) ;
2007
+
2008
+ componentScope . optreference = 'new' ;
2009
+ $rootScope . $apply ( ) ;
2010
+
2011
+ expect ( lastRefValueInParent ) . toBe ( 'new' ) ;
2012
+ } ) ) ;
2013
+
2014
+ it ( 'should not throw exception when reference does not exist' , inject ( function ( ) {
2015
+ compile ( '<div><span my-component>' ) ;
2016
+
2017
+ expect ( componentScope . optref ) . toBe ( undefined ) ;
2018
+ expect ( componentScope . optrefAlias ) . toBe ( undefined ) ;
2019
+ expect ( componentScope . optreference ) . toBe ( undefined ) ;
2020
+ } ) ) ;
2021
+ } ) ;
2022
+
2023
+
1920
2024
describe ( 'executable expression' , function ( ) {
1921
2025
it ( 'should allow expression execution with locals' , inject ( function ( ) {
1922
2026
compile ( '<div><span my-component expr="count = count + offset">' ) ;
0 commit comments