aboutsummaryrefslogtreecommitdiffstats
path: root/src/directives.js
blob: 7c5cc257d0d566ada0943a38aaa6e89fafbb7f0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
// <a href="http://example.com?id={{book.$id}}" alt="{{book.$name}}">link</a>
// becomes
// <a href="" ng-bind-attr="{href:'http://example.com?id={{book.$id}}', alt:'{{book.$name}}'}">link</a>
angular.directive("bind-attr", function(expression, element){
  return function(expression, element){
    var jElement = jQuery(element);
    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.templetize(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
// <div ng-watch="$anchor.book: book=Book.get();"/>
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