forked from eee-c/angular-bind-polymer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
angular_bind_polymer.js
42 lines (39 loc) · 1.26 KB
/
angular_bind_polymer.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
angular.module('eee-c.angularBindPolymer', []).
directive('bindPolymer', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var attrMap = {};
for (var prop in attrs.$attr) {
if (prop != 'bindPolymer') {
var _attr = attrs.$attr[prop];
var _match = element.attr(_attr).match(/\{\{\s*([\.\w]+)\s*\}\}/);
if (_match) {
attrMap[_attr] = _match[1];
}
}
}
// When Polymer sees a change to the bound variable,
// $apply / $digest the changes here in Angular
new MutationObserver(function() { scope.$apply(); }).
observe(element[0], {attributes: true});
for (var _attr in attrMap) { watch (_attr); }
function watch(attr) {
scope.$watch(
function() { return element.attr(attr); },
function(value) {
var tokens = attrMap[attr].split(/\./);
var parent = scope;
for (var i=0; i<tokens.length-1; i++) {
if (typeof(parent[tokens[i]]) == 'undefined') {
parent[tokens[i]] = {};
}
parent = parent[tokens[i]];
}
parent[tokens[tokens.length - 1]] = value;
}
);
}
}
};
});