angular.js/src/Scope.js

165 lines
4.4 KiB
JavaScript
Raw Normal View History

2010-03-26 05:03:11 +00:00
function getter(instance, path) {
if (!path) return instance;
var element = path.split('.');
var key;
var lastInstance = instance;
var len = element.length;
for ( var i = 0; i < len; i++) {
key = element[i];
if (!key.match(/^[\$\w][\$\w\d]*$/))
throw "Expression '" + path + "' is not a valid expression for accesing variables.";
if (instance) {
lastInstance = instance;
instance = instance[key];
}
if (isUndefined(instance) && key.charAt(0) == '$') {
2010-03-26 05:03:11 +00:00
var type = angular['Global']['typeOf'](lastInstance);
type = angular[type.charAt(0).toUpperCase()+type.substring(1)];
var fn = type ? type[[key.substring(1)]] : undefined;
if (fn) {
instance = bind(fn, lastInstance, lastInstance);
2010-03-26 05:03:11 +00:00
return instance;
}
}
}
if (typeof instance === 'function' && !instance['$$factory']) {
return bind(lastInstance, instance);
}
return instance;
};
function setter(instance, path, value){
var element = path.split('.');
for ( var i = 0; element.length > 1; i++) {
var key = element.shift();
var newInstance = instance[key];
if (!newInstance) {
newInstance = {};
instance[key] = newInstance;
}
instance = newInstance;
}
instance[element.shift()] = value;
return value;
};
var compileCache = {};
function expressionCompile(exp){
if (isFunction(exp)) return exp;
var expFn = compileCache[exp];
if (!expFn) {
var parser = new Parser(exp);
expFn = parser.statements();
parser.assertAllConsumed();
compileCache[exp] = expFn;
}
return parserNewScopeAdapter(expFn);
};
// return expFn
// TODO(remove this hack)
function parserNewScopeAdapter(fn) {
2010-03-26 05:03:11 +00:00
return function(){
return fn({
state: this,
2010-03-26 05:03:11 +00:00
scope: {
set: this.$set,
get: this.$get
}
});
};
}
2010-03-26 05:03:11 +00:00
function isRenderableElement(element) {
var name = element && element[0] && element[0].nodeName;
return name && name.charAt(0) != '#' &&
!includes(['TR', 'COL', 'COLGROUP', 'TBODY', 'THEAD', 'TFOOT'], name);
2010-03-26 05:03:11 +00:00
}
function rethrow(e) { throw e; }
function errorHandlerFor(element) {
while (!isRenderableElement(element)) {
element = element.parent() || jqLite(document.body);
}
return function(error) {
element.attr('ng-error', angular.toJson(error));
element.addClass('ng-exception');
};
}
function createScope(parent, Class) {
2010-03-26 05:03:11 +00:00
function Parent(){}
function API(){}
function Behavior(){}
var instance, behavior, api, watchList = [], evalList = [];
Class = Class || noop;
parent = Parent.prototype = parent || {};
api = API.prototype = new Parent();
behavior = Behavior.prototype = extend(new API(), Class.prototype);
instance = new Behavior();
extend(api, {
$parent: parent,
$bind: bind(instance, bind, instance),
$get: bind(instance, getter, instance),
$set: bind(instance, setter, instance),
2010-03-30 04:49:12 +00:00
$eval: function $eval(exp) {
2010-03-26 05:03:11 +00:00
if (isDefined(exp)) {
return expressionCompile(exp).apply(instance, slice.call(arguments, 1, arguments.length));
} else {
foreach(watchList, function(watch) {
var value = instance.$tryEval(watch.watch, watch.handler);
if (watch.last !== value) {
instance.$tryEval(watch.listener, watch.handler, value, watch.last);
watch.last = value;
}
});
2010-03-30 21:55:04 +00:00
foreach(evalList, function(eval) {
instance.$tryEval(eval.fn, eval.handler);
});
2010-03-26 05:03:11 +00:00
}
},
$tryEval: function (expression, exceptionHandler) {
try {
2010-03-30 21:55:04 +00:00
var value = expressionCompile(expression).apply(instance, slice.call(arguments, 2, arguments.length));
if (exceptionHandler) {
errorHandlerFor(exceptionHandler)();
}
return value;
2010-03-26 05:03:11 +00:00
} catch (e) {
error(e);
if (isFunction(exceptionHandler)) {
exceptionHandler(e);
} else if (exceptionHandler) {
errorHandlerFor(exceptionHandler)(e);
}
}
},
$watch: function(watchExp, listener, exceptionHandler) {
var watch = expressionCompile(watchExp);
watchList.push({
watch: watch,
last: watch.call(instance),
handler: exceptionHandler,
listener:expressionCompile(listener)
});
},
$onEval: function(expr, exceptionHandler){
evalList.push({
fn: expressionCompile(expr),
handler: exceptionHandler
});
}
});
Class.apply(instance, slice.call(arguments, 2, arguments.length));
return instance;
}