aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMisko Hevery2010-08-10 11:23:23 -0700
committerMisko Hevery2010-08-10 11:23:23 -0700
commit9b392eca3597fdc9dab81d88df75bef75f6e678f (patch)
treecd103d0830eb52a315dd605ccf4ba3a436743d45
parent4aac29da18ea4680a928edccc28dd8edad93e593 (diff)
downloadangular.js-9b392eca3597fdc9dab81d88df75bef75f6e678f.tar.bz2
fix bug where $eval on undefined throws error
-rw-r--r--src/Scope.js12
-rw-r--r--test/ScopeSpec.js15
2 files changed, 19 insertions, 8 deletions
diff --git a/src/Scope.js b/src/Scope.js
index 4d2aa5c3..86d5bc14 100644
--- a/src/Scope.js
+++ b/src/Scope.js
@@ -130,7 +130,8 @@ function createScope(parent, services, existing) {
$set: bind(instance, setter, instance),
$eval: function $eval(exp) {
- if (exp === undefined) {
+ var type = typeof exp;
+ if (type == 'undefined') {
for ( var i = 0, iSize = evalLists.sorted.length; i < iSize; i++) {
for ( var queue = evalLists.sorted[i],
jSize = queue.length,
@@ -138,18 +139,19 @@ function createScope(parent, services, existing) {
instance.$tryEval(queue[j].fn, queue[j].handler);
}
}
- } else if (typeof exp === 'function'){
+ } else if (type === 'function') {
return exp.call(instance);
- } else {
+ } else if (type === 'string') {
return expressionCompile(exp).call(instance);
}
},
$tryEval: function (expression, exceptionHandler) {
+ var type = typeof expression;
try {
- if (typeof expression == 'function') {
+ if (type == 'function') {
return expression.call(instance);
- } else {
+ } else if (type == 'string'){
return expressionCompile(expression).call(instance);
}
} catch (e) {
diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js
index 6f5485e7..ea63fea4 100644
--- a/test/ScopeSpec.js
+++ b/test/ScopeSpec.js
@@ -21,8 +21,11 @@ describe('scope/model', function(){
});
describe('$eval', function(){
+ var model;
+
+ beforeEach(function(){model = createScope();});
+
it('should eval function with correct this', function(){
- var model = createScope();
model.$eval(function(){
this.name = 'works';
});
@@ -30,18 +33,24 @@ describe('scope/model', function(){
});
it('should eval expression with correct this', function(){
- var model = createScope();
model.$eval('name="works"');
expect(model.name).toEqual('works');
});
it('should do nothing on empty string and not update view', function(){
- var model = createScope();
var onEval = jasmine.createSpy('onEval');
model.$onEval(onEval);
model.$eval('');
expect(onEval).wasNotCalled();
});
+
+ it('should ignore none string/function', function(){
+ model.$eval(null);
+ model.$eval({});
+ model.$tryEval(null);
+ model.$tryEval({});
+ });
+
});
describe('$watch', function(){