From 923289112e34c2d426f6b2e687e93601894fb088 Mon Sep 17 00:00:00 2001 From: Adam Abrons Date: Tue, 16 Mar 2010 10:30:26 -0700 Subject: spike on directives --- src/directives.js | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/directives.js diff --git a/src/directives.js b/src/directives.js new file mode 100644 index 00000000..0e99d633 --- /dev/null +++ b/src/directives.js @@ -0,0 +1,121 @@ + +angular.directive("auth", function(expression, element){ + return function(){ + if(expression == "eager") { + this.$users.fetchCurrent(); + } + } +}); + + +//expression = "book=Book:{year=2000}" +angular.directive("entity", function(expression, element){ + //parse expression, ignore element + var entityName; // "Book"; + var instanceName; // "book"; + var defaults; // {year: 2000}; + + parse(expression); + + return function(){ + this[entityName] = this.$datastore.entity(entityName, defaults); + this[instanceName] = this[entityName](); + this.$watch("$anchor."+instanceName, function(newAnchor){ + this[instanceName] = this[entityName].get(this.$anchor[instanceName]); + }); + }; +}); + + +angular.directive("init", function(expression, element){ + return function(){ + this.$eval(expresssion); + } +}); + + +//translation of {{ }} to ng-bind is external to this +angular.directive("bind", function(expression, element){ + return function() { + this.$watch(expression, function(value){ + element.innerText = value; + }); + }; +}); + + +// translation of {{ }} to ng-bind-attr is external to this +// link +// becomes +// link +angular.directive("bind-attr", function(expression, element){ + var jElement = jQuery(element); + return function(){ + this.$watch(expression, _(jElement.attr).bind(jElement)); + }; +}); + +angular.directive("repeat", function(expression, element){ + var anchor = document.createComment(expression); + jQuery(element).replace(anchor); + var template = this.compile(element); + var lhs = "item"; + var rhs = "items"; + var children = []; + return function(){ + this.$watch(rhs, function(items){ + foreach(children, function(child){ + child.element.remove(); + }); + foreach(items, function(item){ + var child = template(item); // create scope + element.addChild(child.element, anchor); + children.push(child); + }); + }); + }; +}); + + +//ng-non-bindable +angular.directive("non-bindable", function(expression, element){ + return false; +}); + +//Styling +// +//ng-class +//ng-class-odd, ng-class-even +//ng-style +//ng-show, ng-hide + + +angular.directive("action", function(expression, element){ + return function(){ + var self = this; + jQuery(element).click(function(){ + self.$eval(expression); + }); + }; +}); + +//ng-eval +angular.directive("eval", function(expression, element){ + return function(){ + this.$onUpdate( expression); + } +}); +//ng-watch +//
+angular.directive("watch", function(expression, element){ + var watches = { + 'lhs':'rhs' + }; // parse + return function(){ + this.$watch(watches); + } +}); + +//widget related +//ng-validate, ng-required, ng-formatter +//ng-error -- cgit v1.2.3