Skip to content

Commit

Permalink
fix($parse): correctly assign expressions who's path is undefined and…
Browse files Browse the repository at this point in the history
… that use brackets notation

Closes angular#8039
  • Loading branch information
rodyhaddad committed Jul 16, 2014
1 parent 1f23980 commit fa203b9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,9 @@ Parser.prototype = {
return getter(self || object(scope, locals));
}, {
assign: function(scope, value, locals) {
return setter(object(scope, locals), field, value, parser.text);
var o = object(scope, locals);
if (!o) object.assign(scope, o = {});
return setter(o, field, value, parser.text);
}
});
},
Expand All @@ -701,9 +703,10 @@ Parser.prototype = {
return v;
}, {
assign: function(self, value, locals) {
var key = indexFn(self, locals);
var key = ensureSafeMemberName(indexFn(self, locals), parser.text);
// prevent overwriting of Function.constructor which would break ensureSafeObject check
var safe = ensureSafeObject(obj(self, locals), parser.text);
if (!safe) obj.assign(self, safe = {});
return safe[key] = value;
}
});
Expand Down
16 changes: 16 additions & 0 deletions test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,22 @@ describe('parser', function() {
fn.assign(scope, 123);
expect(scope).toEqual({a:123});
}));

it('should expose working assignment function for expressions ending with brackets', inject(function($parse) {
var fn = $parse('a.b["c"]');
expect(fn.assign).toBeTruthy();
var scope = {};
fn.assign(scope, 123);
expect(scope.a.b.c).toEqual(123);
}));

it('should expose working assignment function for expressions with brackets in the middle', inject(function($parse) {
var fn = $parse('a["b"].c');
expect(fn.assign).toBeTruthy();
var scope = {};
fn.assign(scope, 123);
expect(scope.a.b.c).toEqual(123);
}));
});

describe('one-time binding', function() {
Expand Down

0 comments on commit fa203b9

Please sign in to comment.