From a5990050d489e7c06b30b3ccab46b78c517662e2 Mon Sep 17 00:00:00 2001
From: Igor Minar
Date: Mon, 24 Jan 2011 13:27:28 -0800
Subject: ng:view should propagate evals to the current child scope
- this change is needed because of previously reverted $route changes
that used to propagate evals automatically.
- also added docs to highlight how the eval propagation works
---
src/widgets.js | 22 ++++++++++++++++++----
test/widgetsSpec.js | 38 ++++++++++++++++++++++++++------------
2 files changed, 44 insertions(+), 16 deletions(-)
diff --git a/src/widgets.js b/src/widgets.js
index c131b646..f90b29a9 100644
--- a/src/widgets.js
+++ b/src/widgets.js
@@ -629,6 +629,9 @@ angularWidget('ng:include', function(element){
function incrementChange(){ changeCounter++;}
this.$watch(srcExp, incrementChange);
this.$watch(scopeExp, incrementChange);
+
+ // note that this propagates eval to the current childScope, where childScope is dynamically
+ // bound (via $route.onChange callback) to the current scope created by $route
scope.$onEval(function(){
if (childScope && !preventRecursion) {
preventRecursion = true;
@@ -1009,24 +1012,35 @@ angularWidget('ng:view', function(element) {
if (!element[0]['ng:compiled']) {
element[0]['ng:compiled'] = true;
return injectService(['$xhr.cache', '$route'], function($xhr, $route, element){
+ var parentScope = this,
+ childScope;
+
$route.onChange(function(){
- var src, scope;
+ var src;
if ($route.current) {
src = $route.current.template;
- scope = $route.current.scope;
+ childScope = $route.current.scope;
}
if (src) {
$xhr('GET', src, function(code, response){
element.html(response);
- compiler.compile(element)(element, scope);
- scope.$init();
+ compiler.compile(element)(element, childScope);
+ childScope.$init();
});
} else {
element.html('');
}
});
+
+ // note that this propagates eval to the current childScope, where childScope is dynamically
+ // bound (via $route.onChange callback) to the current scope created by $route
+ parentScope.$onEval(function() {
+ if (childScope) {
+ childScope.$eval();
+ }
+ });
});
} else {
this.descend(true);
diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js
index 7faa9ced..0eabc738 100644
--- a/test/widgetsSpec.js
+++ b/test/widgetsSpec.js
@@ -788,26 +788,25 @@ describe("widget", function(){
describe('ng:view', function() {
- var rootScope, partialScope, $route, $location, $browser;
+ var rootScope, rootScope, $route, $location, $browser;
beforeEach(function() {
- rootScope = angular.scope();
- partialScope = angular.compile('