aboutsummaryrefslogtreecommitdiffstats
path: root/src/Scope.js
diff options
context:
space:
mode:
authorMisko Hevery2010-03-22 13:58:04 -0700
committerMisko Hevery2010-03-22 13:58:04 -0700
commit84552f7f8ac3f39c4dbd7d946ae2938d63302840 (patch)
treed3a4433d12bcbfb9d42f92a7b8ec12762ae7df3a /src/Scope.js
parentf6664ed7f6f6dd1f4f9756f57611a316089149cb (diff)
downloadangular.js-84552f7f8ac3f39c4dbd7d946ae2938d63302840.tar.bz2
got few directives working
Diffstat (limited to 'src/Scope.js')
-rw-r--r--src/Scope.js52
1 files changed, 35 insertions, 17 deletions
diff --git a/src/Scope.js b/src/Scope.js
index 3633f960..d22604fd 100644
--- a/src/Scope.js
+++ b/src/Scope.js
@@ -1,18 +1,23 @@
function Scope(initialState, name) {
- this.widgets = [];
- this.evals = [];
- this.watchListeners = {};
- this.name = name;
+ var self = this;
+ self.widgets = [];
+ self.evals = [];
+ self.watchListeners = {};
+ self.name = name;
initialState = initialState || {};
var State = function(){};
State.prototype = initialState;
- this.state = new State();
- this.state['$parent'] = initialState;
+ self.state = new State();
+ extend(self.state, {
+ '$parent': initialState,
+ '$watch': bind(self, self.addWatchListener),
+ '$eval': bind(self, self.eval),
+ // change name to onEval?
+ '$addEval': bind(self, self.addEval)
+ });
if (name == "ROOT") {
- this.state['$root'] = this.state;
+ self.state['$root'] = self.state;
}
- this.set('$watch', bind(this, this.addWatchListener));
- this.set('$eval', bind(this, this.addEval));
};
Scope.expressionCache = {};
@@ -47,6 +52,7 @@ Scope.getter = function(instance, path) {
};
Scope.prototype = {
+ // TODO: rename to update? or eval?
updateView: function() {
var self = this;
this.fireWatchers();
@@ -64,7 +70,13 @@ Scope.prototype = {
addEval: function(fn, listener) {
// todo: this should take a function/string and a listener
- this.evals.push(fn);
+ // todo: this is a hack, which will need to be cleaned up.
+ var self = this,
+ listenFn = listener || noop,
+ expr = bind(self, self.compile(fn), {scope: self, self: self.state});
+ this.evals.push(function(){
+ self.apply(listenFn, expr());
+ });
},
isProperty: function(exp) {
@@ -103,15 +115,21 @@ Scope.prototype = {
this.eval(expressionText + "=" + toJson(value));
},
- eval: function(expressionText, context) {
-// log('Scope.eval', expressionText);
- var expression = Scope.expressionCache[expressionText];
- if (!expression) {
- var parser = new Parser(expressionText);
- expression = parser.statements();
+ compile: function(exp) {
+ if (isFunction(exp)) return exp;
+ var expFn = Scope.expressionCache[exp];
+ if (!expFn) {
+ var parser = new Parser(exp);
+ expFn = parser.statements();
parser.assertAllConsumed();
- Scope.expressionCache[expressionText] = expression;
+ Scope.expressionCache[exp] = expFn;
}
+ return expFn;
+ },
+
+ eval: function(expressionText, context) {
+// log('Scope.eval', expressionText);
+ var expression = this.compile(expressionText);
context = context || {};
context.scope = this;
context.self = this.state;