-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathangular_bind_polymer.js
More file actions
54 lines (47 loc) · 1.7 KB
/
angular_bind_polymer.js
File metadata and controls
54 lines (47 loc) · 1.7 KB
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
51
52
53
54
angular.module('eee-c.angularBindPolymer', []).
directive('bindPolymer', ['$parse', function($parse) {
'use strict';
return {
restrict: 'A',
scope : false,
compile: function bindPolymerCompile($element, $attr) {
var attrMap = {};
for (var prop in $attr) {
var dash_prop = prop.
replace(/([a-z])([A-Z])/g, '$1-$2').
toLowerCase();
if (angular.isString($attr[prop])) {
var _match = $attr[prop].match(/\{\{\s*([\.\w]+)\s*\}\}/);
if (_match) {
// console.log(prop + ': ' + _match[1])
attrMap[prop] = $parse(_match[1]);
if (dash_prop != prop) {
attrMap[dash_prop] = $parse(_match[1]);
}
}
}
}
return function bindPolymerLink(scope, element, attrs) {
// When Polymer sees a change to the bound variable,
// $apply / $digest the changes here in Angular
var observer = new MutationObserver(function processMutations(mutations) {
mutations.forEach(function processMutation(mutation) {
var attributeName, newValue, oldValue, getter;
attributeName = mutation.attributeName;
if(attributeName in attrMap) {
newValue = element.attr(attributeName);
getter = attrMap[attributeName];
oldValue = getter(scope);
if(oldValue != newValue && angular.isFunction(getter.assign)) {
getter.assign(scope, newValue);
}
}
});
scope.$apply();
});
observer.observe(element[0], {attributes: true});
scope.$on('$destroy', observer.disconnect.bind(observer));
}
}
};
}]);