-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepeatInside.js
72 lines (64 loc) · 2.84 KB
/
repeatInside.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
bigblind = angular.module("bigblind", []);
angular.module("bigblind").directive("bbRepeatInside", function(){
"use strict";
var linkIt = function(scope, $element, index, key, val, keyIdentifier, valIdentifier, transclude){
var childScope = scope.$new();
childScope.index = index;
if (keyIdentifier) {
childScope[keyIdentifier] = key;
}
childScope[valIdentifier] = val;
childScope.first = (index === 0);
transclude(childScope,function(clone){
clone.scope = childScope;
$element.append(clone);
});
};
var ddo = {
transclude:true,
compile: function(element, attrs, transclude){
return function($scope, $element, $attr){
var expression = $attr.bbRepeatInside;
console.log($attr);
var match = expression.match(/^\s*(.+)\s+in\s+(.*?)$/);
var lhs, rhs;
var keyIdentifier, valIdentifier, hashFnLocals = {};
if(!match){
throw "Expected expression in form of '_item_ in _collection_[ track by _id_]' but got " + expression;
}
lhs = match[1];
rhs = match[2];
match = lhs.match(/^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$/);
if (!match) {
throw "bad left hand side in loop";
}
valIdentifier = match[3] || match[1];
keyIdentifier = match[2];
$scope.$watch(rhs, function(newval, oldval) {
var childScope, index = 0;
var children = angular.element($element[0]).children();
console.log("test output:");
console.log($element);
console.log($element.children());
console.log(angular.element($element[0]).children());
for (var i=0; i < children.length; i++){
children[i].remove();
}
if (angular.isArray(newval)){
for (index=0; index<newval.length; index++) {
linkIt($scope, $element, index, index, newval[index], keyIdentifier, valIdentifier, transclude);
}
}else{
for (var key in newval){
if (newval.hasOwnProperty(key) && key[0] !== "$") {
linkIt($scope, $element, index, key, newval[key], keyIdentifier, valIdentifier, transclude);
index += 1;
}
}
}
}, true);
};
}
};
return ddo
})