From 10646c9f6f62dd5130606d07ffe69770b3973f47 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Wed, 19 Jan 2011 14:50:29 -0800 Subject: add ng:view widget --- src/widgets.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'src/widgets.js') diff --git a/src/widgets.js b/src/widgets.js index 3b0ffab7..4585629a 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -944,3 +944,81 @@ angularWidget("@ng:repeat", function(expression, element){ }); */ angularWidget("@ng:non-bindable", noop); + + +/** + * @ngdoc widget + * @name angular.widget.ng:view + * + * @description + * # Overview + * `ng:view` is a widget that complements the {@link angular.service.$route $route} service by + * including the rendered template of the current route into the main layout (`index.html`) file. + * Every time the current route changes, the included view changes with it according to the + * configuration of the `$route` service. + * + * This widget provides functionality similar to {@link angular.service.ng:include ng:include} when + * used like this: + * + * + * + * + * # Advantages + * Compared to `ng:include`, `ng:view` offers these advantages: + * + * - shorter syntax + * - more efficient execution + * - doesn't require `$route` service to be available on the root scope + * + * + * # Example + * Because of the nature of this widget, we can't include the usual live example for it. Instead + * following is a code snippet showing the typical usage: + * +
+     
+     
+ foo | bar | undefined
+ The view is included below: +
+ +
+
+ */ +angularWidget('ng:view', function(element) { + var compiler = this; + + if (!element[0]['ng:compiled']) { + element[0]['ng:compiled'] = true; + return injectService(['$xhr.cache', '$route'], function($xhr, $route, element){ + $route.onChange(function(){ + var src, scope; + + if ($route.current) { + src = $route.current.template; + scope = $route.current.scope; + } + + if (src) { + $xhr('GET', src, function(code, response){ + element.html(response); + compiler.compile(element)(element, scope); + scope.$init(); + }); + } else { + element.html(''); + } + }); + }); + } else { + this.descend(true); + this.directives(true); + } +}); -- cgit v1.2.3