Skip to content

Commit 9232891

Browse files
committed
spike on directives
1 parent 79b743e commit 9232891

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

src/directives.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
2+
angular.directive("auth", function(expression, element){
3+
return function(){
4+
if(expression == "eager") {
5+
this.$users.fetchCurrent();
6+
}
7+
}
8+
});
9+
10+
11+
//expression = "book=Book:{year=2000}"
12+
angular.directive("entity", function(expression, element){
13+
//parse expression, ignore element
14+
var entityName; // "Book";
15+
var instanceName; // "book";
16+
var defaults; // {year: 2000};
17+
18+
parse(expression);
19+
20+
return function(){
21+
this[entityName] = this.$datastore.entity(entityName, defaults);
22+
this[instanceName] = this[entityName]();
23+
this.$watch("$anchor."+instanceName, function(newAnchor){
24+
this[instanceName] = this[entityName].get(this.$anchor[instanceName]);
25+
});
26+
};
27+
});
28+
29+
30+
angular.directive("init", function(expression, element){
31+
return function(){
32+
this.$eval(expresssion);
33+
}
34+
});
35+
36+
37+
//translation of {{ }} to ng-bind is external to this
38+
angular.directive("bind", function(expression, element){
39+
return function() {
40+
this.$watch(expression, function(value){
41+
element.innerText = value;
42+
});
43+
};
44+
});
45+
46+
47+
// translation of {{ }} to ng-bind-attr is external to this
48+
// <a href="http://example.com?id={{book.$id}}" alt="{{book.$name}}">link</a>
49+
// becomes
50+
// <a href="" ng-bind-attr="{href:'http://example.com?id={{book.$id}}', alt:'{{book.$name}}'}">link</a>
51+
angular.directive("bind-attr", function(expression, element){
52+
var jElement = jQuery(element);
53+
return function(){
54+
this.$watch(expression, _(jElement.attr).bind(jElement));
55+
};
56+
});
57+
58+
angular.directive("repeat", function(expression, element){
59+
var anchor = document.createComment(expression);
60+
jQuery(element).replace(anchor);
61+
var template = this.compile(element);
62+
var lhs = "item";
63+
var rhs = "items";
64+
var children = [];
65+
return function(){
66+
this.$watch(rhs, function(items){
67+
foreach(children, function(child){
68+
child.element.remove();
69+
});
70+
foreach(items, function(item){
71+
var child = template(item); // create scope
72+
element.addChild(child.element, anchor);
73+
children.push(child);
74+
});
75+
});
76+
};
77+
});
78+
79+
80+
//ng-non-bindable
81+
angular.directive("non-bindable", function(expression, element){
82+
return false;
83+
});
84+
85+
//Styling
86+
//
87+
//ng-class
88+
//ng-class-odd, ng-class-even
89+
//ng-style
90+
//ng-show, ng-hide
91+
92+
93+
angular.directive("action", function(expression, element){
94+
return function(){
95+
var self = this;
96+
jQuery(element).click(function(){
97+
self.$eval(expression);
98+
});
99+
};
100+
});
101+
102+
//ng-eval
103+
angular.directive("eval", function(expression, element){
104+
return function(){
105+
this.$onUpdate( expression);
106+
}
107+
});
108+
//ng-watch
109+
// <div ng-watch="$anchor.book: book=Book.get();"/>
110+
angular.directive("watch", function(expression, element){
111+
var watches = {
112+
'lhs':'rhs'
113+
}; // parse
114+
return function(){
115+
this.$watch(watches);
116+
}
117+
});
118+
119+
//widget related
120+
//ng-validate, ng-required, ng-formatter
121+
//ng-error

0 commit comments

Comments
 (0)