Skip to content

Commit 8bdcf28

Browse files
committed
Merge branch 'array-model-watch' of https://github.com/Sandreu/angular-schema-form into Sandreu-array-model-watch
2 parents 84a2b04 + ebc8399 commit 8bdcf28

File tree

2 files changed

+86
-5
lines changed

2 files changed

+86
-5
lines changed

src/directives/array.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* Directive that handles the model arrays
33
*/
4-
angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sfValidator',
5-
function(sfSelect, schemaForm, sfValidator) {
4+
angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sfValidator', 'sfPath',
5+
function(sfSelect, schemaForm, sfValidator, sfPath) {
66

77
var setIndex = function(index) {
88
return function(form) {
@@ -18,17 +18,25 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
1818
require: '?ngModel',
1919
link: function(scope, element, attrs, ngModel) {
2020
var formDefCache = {};
21-
21+
var arr_change_watcher = false;
22+
2223
// Watch for the form definition and then rewrite it.
2324
// It's the (first) array part of the key, '[]' that needs a number
2425
// corresponding to an index of the form.
2526
var once = scope.$watch(attrs.sfArray, function(form) {
26-
27+
2728
// An array model always needs a key so we know what part of the model
2829
// to look at. This makes us a bit incompatible with JSON Form, on the
2930
// other hand it enables two way binding.
3031
var list = sfSelect(form.key, scope.model);
31-
32+
33+
// Stop a previous watcher
34+
if (arr_change_watcher) arr_change_watcher();
35+
arr_change_watcher = scope.$watch('model' + sfPath.normalize(form.key), function (val) {
36+
list = sfSelect(form.key, scope.model);
37+
scope.modelArray = list;
38+
});
39+
3240
// Since ng-model happily creates objects in a deep path when setting a
3341
// a value but not arrays we need to create the array.
3442
if (angular.isUndefined(list)) {

test/directives/schema-form-test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,5 +1619,78 @@ describe('directive',function(){
16191619
tmpl.children().eq(0).find('select').eq(0).find('option').eq(2).text().trim().should.be.eq('The A');
16201620
});
16211621
});
1622+
1623+
1624+
it('should update array form on model array ref change',function(){
1625+
1626+
inject(function($compile,$rootScope){
1627+
var scope = $rootScope.$new();
1628+
scope.person = {
1629+
names:[
1630+
{
1631+
firstname: 'Bill',
1632+
lastname: 'Murray'
1633+
},{
1634+
firstname: 'Ghost',
1635+
lastname: 'Buster'
1636+
}
1637+
]
1638+
};
1639+
1640+
scope.schema = {
1641+
"type": "object",
1642+
"properties": {
1643+
"names": {
1644+
"type": "array",
1645+
"items": {
1646+
"type": "object",
1647+
"title": "subform",
1648+
"properties": {
1649+
"firstname": {
1650+
"type": "string"
1651+
},
1652+
"lastname": {
1653+
"type": "string"
1654+
}
1655+
}
1656+
}
1657+
}
1658+
}
1659+
};
1660+
1661+
scope.form = ["*"];
1662+
1663+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person"></form>');
1664+
1665+
$compile(tmpl)(scope);
1666+
$rootScope.$apply();
1667+
1668+
tmpl.children().eq(0).find('ol').children().length.should.be.eq(2);
1669+
1670+
var new_names = [
1671+
{
1672+
firstname: 'Bill',
1673+
lastname: 'Murray'
1674+
},
1675+
{
1676+
firstname: 'Harold',
1677+
lastname: 'Ramis'
1678+
},{
1679+
firstname: 'Dan',
1680+
lastname: 'Aykroyd'
1681+
},{
1682+
firstname: 'Ghost',
1683+
lastname: 'Buster'
1684+
}
1685+
];
1686+
1687+
scope.person.names = new_names;
1688+
1689+
$rootScope.$apply();
1690+
1691+
tmpl.children().eq(0).find('ol').children().length.should.be.eq(4);
1692+
});
1693+
});
1694+
16221695

16231696
});

0 commit comments

Comments
 (0)