aboutsummaryrefslogtreecommitdiffstats
path: root/src/directives.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/directives.js')
-rw-r--r--src/directives.js120
1 files changed, 59 insertions, 61 deletions
diff --git a/src/directives.js b/src/directives.js
index 747da3f5..10476c77 100644
--- a/src/directives.js
+++ b/src/directives.js
@@ -1,12 +1,12 @@
angularDirective("ng-init", function(expression){
- return function(){
- this.$eval(expression);
+ return function(element){
+ this.$tryEval(expression, element);
};
});
angularDirective("ng-eval", function(expression){
- return function(){
- this.$addEval(expression);
+ return function(element){
+ this.$onEval(expression, element);
};
});
@@ -14,7 +14,7 @@ angularDirective("ng-bind", function(expression){
return function(element) {
this.$watch(expression, function(value){
element.text(value);
- });
+ }, element);
};
});
@@ -45,23 +45,23 @@ angularDirective("ng-bind-template", function(expression){
var templateFn = compileBindTemplate(expression);
return function(element) {
var lastValue;
- this.$addEval(function() {
+ this.$onEval(function() {
var value = templateFn.call(this);
if (value != lastValue) {
element.text(value);
lastValue = value;
}
- });
+ }, element);
};
});
angularDirective("ng-bind-attr", function(expression){
return function(element){
- this.$addEval(function(){
+ this.$onEval(function(){
foreach(this.$eval(expression), function(value, key){
element.attr(key, compileBindTemplate(value).call(this));
}, this);
- });
+ }, element);
};
});
@@ -70,76 +70,73 @@ angularDirective("ng-non-bindable", function(){
});
angularDirective("ng-repeat", function(expression, element){
- var reference = this.comment("ng-repeat: " + expression),
- r = element.removeAttr('ng-repeat'),
- template = this.compile(element),
- match = expression.match(/^\s*(.+)\s+in\s+(.*)\s*$/),
- lhs, rhs, valueIdent, keyIdent;
- if (! match) {
- throw "Expected ng-repeat in form of 'item in collection' but got '" +
+ element.removeAttr('ng-repeat');
+ element.replaceWith(this.comment("ng-repeat: " + expression));
+ var template = this.compile(element);
+ return function(reference){
+ var match = expression.match(/^\s*(.+)\s+in\s+(.*)\s*$/),
+ lhs, rhs, valueIdent, keyIdent;
+ if (! match) {
+ throw "Expected ng-repeat in form of 'item in collection' but got '" +
expression + "'.";
- }
- lhs = match[1];
- rhs = match[2];
- match = lhs.match(/^([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\)$/);
- if (!match) {
- throw "'item' in 'item in collection' should be identifier or (key, value) but got '" +
+ }
+ lhs = match[1];
+ rhs = match[2];
+ match = lhs.match(/^([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\)$/);
+ if (!match) {
+ throw "'item' in 'item in collection' should be identifier or (key, value) but got '" +
keyValue + "'.";
- }
- valueIdent = match[3] || match[1];
- keyIdent = match[2];
+ }
+ valueIdent = match[3] || match[1];
+ keyIdent = match[2];
- var parent = element.parent();
- element.replaceWith(reference);
- return function(){
- var children = [],
- currentScope = this;
- this.$addEval(rhs, function(items){
+ var children = [], currentScope = this;
+ this.$onEval(function(){
var index = 0, childCount = children.length, childScope, lastElement = reference;
- foreach(items || [], function(value, key){
+ foreach(this.$tryEval(rhs, reference), function(value, key){
if (index < childCount) {
// reuse existing child
childScope = children[index];
} else {
// grow children
childScope = template(element.clone(), currentScope);
- childScope.init();
- childScope.scope.set('$index', index);
- childScope.element.attr('ng-index', index);
- lastElement.after(childScope.element);
+ lastElement.after(childScope.$element);
+ childScope.$index = index;
+ childScope.$element.attr('ng-index', index);
+ childScope.$init();
children.push(childScope);
}
- childScope.scope.set(valueIdent, value);
- if (keyIdent) childScope.scope.set(keyIdent, key);
- childScope.scope.updateView();
- lastElement = childScope.element;
+ childScope[valueIdent] = value;
+ if (keyIdent) childScope[keyIdent] = key;
+ childScope.$eval();
+ lastElement = childScope.$element;
index ++;
});
// shrink children
while(children.length > index) {
- children.pop().element.remove();
+ children.pop().$element.remove();
}
- });
+ }, reference);
};
}, {exclusive: true});
angularDirective("ng-action", function(expression, element){
- return function(){
+ return function(element){
var self = this;
element.click(function(){
- self.$eval(expression);
+ self.$tryEval(expression, element);
});
};
});
angularDirective("ng-watch", function(expression, element){
var match = expression.match(/^([^.]*):(.*)$/);
- if (!match) {
- throw "Expecting watch expression 'ident_to_watch: watch_statement' got '"
- + expression + "'";
- }
- return function(){
- this.$watch(match[1], match[2]);
+ return function(element){
+ if (!match) {
+ throw "Expecting watch expression 'ident_to_watch: watch_statement' got '"
+ + expression + "'";
+ }
+ this.$watch(match[1], match[2], element);
};
});
@@ -147,12 +144,13 @@ function ngClass(selector) {
return function(expression, element){
var existing = element[0].className + ' ';
return function(element){
- this.$addEval(expression, function(value){
+ this.$onEval(function(){
+ var value = this.$eval(expression);
if (selector(this.$index)) {
if (isArray(value)) value = value.join(' ');
element[0].className = (existing + value).replace(/\s\s+/g, ' ');
}
- });
+ }, element);
};
};
}
@@ -163,25 +161,25 @@ angularDirective("ng-class-even", ngClass(function(i){return i % 2 == 0;}));
angularDirective("ng-show", function(expression, element){
return function(element){
- this.$addEval(expression, function(value){
- element.css('display', toBoolean(value) ? '' : 'none');
- });
+ this.$onEval(function(){
+ element.css('display', toBoolean(this.$eval(expression)) ? '' : 'none');
+ }, element);
};
});
angularDirective("ng-hide", function(expression, element){
return function(element){
- this.$addEval(expression, function(value){
- element.css('display', toBoolean(value) ? 'none' : '');
- });
+ this.$onEval(function(){
+ element.css('display', toBoolean(this.$eval(expression)) ? 'none' : '');
+ }, element);
};
});
angularDirective("ng-style", function(expression, element){
return function(element){
- this.$addEval(expression, function(value){
- element.css(value);
- });
+ this.$onEval(function(){
+ element.css(this.$eval(expression));
+ }, element);
};
});