-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathangupoly.js
50 lines (46 loc) · 1.42 KB
/
angupoly.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
angular.module('angupoly', [])
.directive('angupoly', function($parse) {
return {
priority: 42,
restrict: 'A',
controller: function($scope, $element, $attrs) {
var el = $element[0];
var conf = $scope.$eval($attrs.angupoly);
Object.keys(conf).forEach(function(propName) {
var path = conf[propName];
var type;
if (typeof path === 'object') {
type = path.type;
path = path.path;
}
var parse = $parse(path);
var assign = parse.assign;
// from angular scope to element property
if (type === 'array') {
$scope.$watchCollection(path, function(val) {
// clone new array, since Polymer doesn't update same object reference
el.set(propName, val.slice());
});
} else {
$scope.$watch(path, function(val) {
el[propName] = val;
});
}
if (assign) {
// from element property to angular scope
el.addEventListener(propName + '-changed', function(e) {
var detail = e.detail;
var path = detail.path;
if (!path || path === propName) {
$scope.$evalAsync(function() {
assign($scope, detail.value);
});
} else if (path === propName + '.splices') {
$scope.$apply();
}
});
}
});
}
};
});