Skip to content

Commit

Permalink
Modified initialisation include constraints loading in wait #219
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala committed Jan 8, 2024
1 parent 76a367e commit e87bf7e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
13 changes: 9 additions & 4 deletions grails-app/assets/javascripts/forms-knockout-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1179,9 +1179,7 @@
};

ko.bindingHandlers['triggerPrePopulate'] = {
'update': function (element, valueAccessor, allBindings, viewModel, bindingContext) {


'init': function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var dataModelItem = valueAccessor();
var behaviours = dataModelItem.get('behaviour');
for (var i = 0; i < behaviours.length; i++) {
Expand All @@ -1206,8 +1204,15 @@
}
}
var dependencyTracker = ko.computed(function () {
dataModelItem(); // register dependency on the observable.
var initialised = (dataModelItem.context.lifecycleState && dataModelItem.context.lifecycleState() == 'initialised');

dataLoader.prepop(config).done(function (data) {

if (config.waitForInitialisation && !initialised) {
console.log("Not applying any updates during initialisation")
return;
}

data = data || {};
var configTarget = config.target;
var target;
Expand Down
19 changes: 13 additions & 6 deletions grails-app/assets/javascripts/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ function orEmptyArray(v) {
else {
self(data);
}

return constraintsInititaliser;
}
};

Expand Down Expand Up @@ -1066,6 +1066,7 @@ function orEmptyArray(v) {
self.addRow = function (data) {
var newItem = self.newItem(data, self.rowCount());
self.push(newItem);
return newItem.loadData(data || {});
};
self.newItem = function (data, index) {
var itemDataModel = _.indexBy(dataModel[listName].columns, 'name');
Expand Down Expand Up @@ -1155,6 +1156,7 @@ function orEmptyArray(v) {
};

parent['load' + listName] = function (data, append) {
var initialisers = [];
if (!append) {
self([]);
}
Expand All @@ -1163,9 +1165,10 @@ function orEmptyArray(v) {
}
else {
_.each(data, function (row, i) {
self.push(self.newItem(row, i));
initialisers = initialisers.concat(self.addRow(row));
});
}
return initialisers;
};
};

Expand Down Expand Up @@ -1560,12 +1563,16 @@ function orEmptyArray(v) {
};

self.initialise = function (outputData) {
var deferred = $.Deferred();
self.loadOrPrepop(outputData).done(function (data) {
var initialisers = self.loadData(data);

return self.loadOrPrepop(outputData).done(function (data) {
self.loadData(data);
self.transients.dummy.notifySubscribers();
$.when.apply($, initialisers).then(function () {
deferred.resolve();
self.transients.dummy.notifySubscribers();
});
});

return deferred;
};


Expand Down
16 changes: 8 additions & 8 deletions grails-app/taglib/au/org/ala/ecodata/forms/ModelJSTagLib.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class ModelJSTagLib {
void renderLoad(List items, JSModelRenderContext ctx) {

ctx.out << "self.loadData = function(data) {\n"

out << INDENT * 1 << "var initialisers = [];\n"
JSModelRenderContext child = ctx.createChildContext()

Map attrs = ctx.attrs
Expand All @@ -209,15 +209,15 @@ class ModelJSTagLib {
child.dataModel = mod

if (mod.dataType == 'list') {
out << INDENT * 1 << "self.load${mod.name}(data.${mod.name});\n"
out << INDENT * 1 << "initialisers = initialisers.concat(self.load${mod.name}(data.${mod.name}));\n"
loadColumnTotals out, attrs, mod
} else if (mod.dataType == 'matrix') {
out << INDENT * 1 << "self.load${mod.name.capitalize()}(data.${mod.name});\n"
} else {
renderInitialiser(child)
}
}

out << INDENT * 1 << "return initialisers;\n"
ctx.out << "};\n"
}

Expand Down Expand Up @@ -266,7 +266,7 @@ class ModelJSTagLib {
out << INDENT*4 << "${ctx.propertyPath}['${mod.name}'](ecodata.forms.orDefault(data['${mod.name}'], '${attrs.user.displayName}'));\n"
} else {
if (requiresMetadataExtender(mod)) {
out << INDENT*4 << "${ctx.propertyPath}['${mod.name}'].load(${value});\n"
out << INDENT*4 << "initialisers.push(${ctx.propertyPath}['${mod.name}'].load(${value}));\n"
}
else {
out << INDENT*4 << "${ctx.propertyPath}['${mod.name}'](${value});\n"
Expand Down Expand Up @@ -531,8 +531,6 @@ class ModelJSTagLib {
}
renderLoad(ctx.dataModel.columns, childCtx)

out << INDENT*4 << "self.loadData(data || {});\n"

out << INDENT*2 << "};\n"
}

Expand Down Expand Up @@ -748,21 +746,23 @@ class ModelJSTagLib {
boolean userAddedRows = Boolean.valueOf(viewModel?.userAddedRows)
def defaultRows = []
model.defaultRows?.eachWithIndex { row, i ->
defaultRows << INDENT*5 + "${ctx.propertyPath}.${model.name}.addRow(${row.toString()});"
defaultRows << INDENT*5 + "rowInitalisers.push(${ctx.propertyPath}.${model.name}.addRow(${row.toString()}));"
}
def insertDefaultModel = defaultRows.join('\n')

// If there are no default rows, insert a single blank row and make it available for editing.
if (attrs.edit && model.defaultRows == null) {
insertDefaultModel = "${ctx.propertyPath}.${model.name}.addRow();"
insertDefaultModel = "rowInitalisers.push(${ctx.propertyPath}.${model.name}.addRow());"
}

out << """var context = _.extend({}, context, {parent:self, listName:'${model.name}'});"""
String extender = "{list:{metadata:self.dataModel, constructorFunction:${rowModelName}, context:context, userAddedRows:${userAddedRows}, config:config}}"
observableArray(ctx, [extender], false)
out << """
${ctx.propertyPath}.${model.name}.loadDefaults = function() {
var rowInitalisers = [];
${insertDefaultModel}
return rowInitialisers;
};
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class ModelJSTagLibSpec extends Specification implements TagLibUnitTest<ModelJST
tagLib.renderInitialiser(ctx)

then:
compareWithoutWhiteSpace("data['test'].load(ecodata.forms.orDefault(data['test'],undefined));", actualOut.toString())
compareWithoutWhiteSpace("initialisers.push(data['test'].load(ecodata.forms.orDefault(data['test'],undefined)));", actualOut.toString())
}

def "Input data is null/undefined checked before calling loadData() on SpeciesViewModel"() {
Expand Down
2 changes: 0 additions & 2 deletions src/test/js/util/MultiFeatureViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ ecodata.forms.MultiFeatureViewModel = function (output, dataModel, context, conf
self['feature'].loadData(ecodata.forms.orDefault(data['feature'], undefined));
};

self.loadData(data);

};

var context = _.extend({}, context, {parent:self, listName:'features'});
Expand Down

0 comments on commit e87bf7e

Please sign in to comment.