Skip to content

Commit 938929d

Browse files
committed
Add support for bindWatchToClass option
1 parent 3be6ea3 commit 938929d

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

angular-classy.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,10 @@ angular.module('classy.register', ['classy.core']).classy.plugin.controller({
428428
});
429429

430430
angular.module('classy.watch', ['classy.core']).classy.plugin.controller({
431+
localInject: ['$parse'],
431432
options: {
432433
enabled: true,
434+
bindWatchToClass: false,
433435
_watchKeywords: {
434436
normal: [],
435437
objectEquality: ['{object}', '{deep}'],
@@ -456,6 +458,12 @@ angular.module('classy.watch', ['classy.core']).classy.plugin.controller({
456458
return deps.$scope.$watchCollection(expression, angular.bind(klass, fn));
457459
}
458460
},
461+
convertToFunctionExpression: function(expression, context) {
462+
var parse = this.$parse;
463+
return function() {
464+
return parse(expression)(context);
465+
};
466+
},
459467
postInit: function(klass, deps, module) {
460468
if (!this.isActive(klass, deps)) {
461469
return;
@@ -482,16 +490,26 @@ angular.module('classy.watch', ['classy.core']).classy.plugin.controller({
482490
var keywords = watchKeywords[watchType];
483491
for (var i = 0; i < keywords.length; i++) {
484492
var keyword = keywords[i];
493+
485494
if (expression.indexOf(keyword) !== -1) {
486-
watchFn(klass, expression.replace(keyword, ''), fn, deps);
495+
var normalizedExpression = expression.replace(keyword, '');
496+
497+
var exp = this.options.bindWatchToClass ?
498+
this.convertToFunctionExpression(normalizedExpression, klass) :
499+
normalizedExpression;
500+
501+
watchFn(klass, exp, fn, deps);
487502
watchRegistered = true;
488503
break;
489504
}
490505
}
491506
}
492507
if (!watchRegistered) {
493508
// If no keywords have been found then register it as a normal watch
494-
this.watchFns.normal(klass, expression, fn, deps);
509+
var exp = this.options.bindWatchToClass ?
510+
this.convertToFunctionExpression(expression, klass) :
511+
expression;
512+
this.watchFns.normal(klass, exp, fn, deps);
495513
}
496514
}
497515
}

angular-classy.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/todomvc/js/controllers/todoCtrl-asController.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ todomvc.classy.controller({
1212
name: 'TodoAsControllerCtrl',
1313
inject: ['$scope', '$location', 'todoStorage'],
1414
__options: {
15+
bindWatchToClass: true,
1516
addToScope: false // shorthand for commented out code below
1617

1718
// 'bindData': {
@@ -38,12 +39,12 @@ todomvc.classy.controller({
3839
},
3940

4041
watch: {
41-
'todoCtrl.location.path()': function(path) {
42+
'location.path()': function(path) {
4243
this.statusFilter = (path === '/active') ?
4344
{ completed: false } : (path === '/completed') ?
4445
{ completed: true } : null;
4546
},
46-
'{object}todoCtrl.todos': '_onTodoChange'
47+
'{object}todos': '_onTodoChange'
4748
},
4849

4950
methods: {

src/watch.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
angular.module('classy.watch', ['classy.core']).classy.plugin.controller({
2+
localInject: ['$parse'],
23
options: {
34
enabled: true,
5+
bindWatchToClass: false,
46
_watchKeywords: {
57
normal: [],
68
objectEquality: ['{object}', '{deep}'],
@@ -27,6 +29,12 @@ angular.module('classy.watch', ['classy.core']).classy.plugin.controller({
2729
return deps.$scope.$watchCollection(expression, angular.bind(klass, fn));
2830
}
2931
},
32+
convertToFunctionExpression: function(expression, context) {
33+
var parse = this.$parse;
34+
return function() {
35+
return parse(expression)(context);
36+
};
37+
},
3038
postInit: function(klass, deps, module) {
3139
if (!this.isActive(klass, deps)) {
3240
return;
@@ -53,16 +61,26 @@ angular.module('classy.watch', ['classy.core']).classy.plugin.controller({
5361
var keywords = watchKeywords[watchType];
5462
for (var i = 0; i < keywords.length; i++) {
5563
var keyword = keywords[i];
64+
5665
if (expression.indexOf(keyword) !== -1) {
57-
watchFn(klass, expression.replace(keyword, ''), fn, deps);
66+
var normalizedExpression = expression.replace(keyword, '');
67+
68+
var exp = this.options.bindWatchToClass ?
69+
this.convertToFunctionExpression(normalizedExpression, klass) :
70+
normalizedExpression;
71+
72+
watchFn(klass, exp, fn, deps);
5873
watchRegistered = true;
5974
break;
6075
}
6176
}
6277
}
6378
if (!watchRegistered) {
6479
// If no keywords have been found then register it as a normal watch
65-
this.watchFns.normal(klass, expression, fn, deps);
80+
var exp = this.options.bindWatchToClass ?
81+
this.convertToFunctionExpression(expression, klass) :
82+
expression;
83+
this.watchFns.normal(klass, exp, fn, deps);
6684
}
6785
}
6886
}

0 commit comments

Comments
 (0)