New helper methods to call methods on view models
This commit is contained in:
parent
b1e4a1d9dc
commit
f7117900e6
1 changed files with 53 additions and 0 deletions
|
|
@ -635,3 +635,56 @@ function getOnlyChangedData(data, oldData) {
|
|||
return f(data, oldData);
|
||||
}
|
||||
|
||||
function callViewModels(allViewModels, method, callback) {
|
||||
callViewModelsIf(allViewModels, method, undefined, callback);
|
||||
}
|
||||
|
||||
function callViewModelsIf(allViewModels, method, condition, callback) {
|
||||
if (condition == undefined || !_.isFunction(condition)) {
|
||||
condition = function() { return true; };
|
||||
}
|
||||
|
||||
var parameters = undefined;
|
||||
if (!_.isFunction(callback)) {
|
||||
// if callback is not a function that means we are supposed to directly
|
||||
// call the view model method instead of providing it to the callback
|
||||
// - let's figure out how
|
||||
|
||||
if (callback == undefined) {
|
||||
// directly call view model method with no parameters
|
||||
parameters = undefined;
|
||||
log.debug("Calling method", method, "on view models");
|
||||
} else if (_.isArray(callback)) {
|
||||
// directly call view model method with these parameters
|
||||
parameters = callback;
|
||||
log.debug("Calling method", method, "on view models with specified parameters", parameters);
|
||||
} else {
|
||||
// ok, this doesn't make sense, callback is neither undefined nor
|
||||
// an array, we'll return without doing anything
|
||||
return;
|
||||
}
|
||||
|
||||
// we reset this here so we now further down that we want to call
|
||||
// the method directly
|
||||
callback = undefined;
|
||||
} else {
|
||||
log.debug("Providing method", method, "on view models to specified callback", callback);
|
||||
}
|
||||
|
||||
_.each(allViewModels, function(viewModel) {
|
||||
if (viewModel.hasOwnProperty(method) && condition(viewModel, method)) {
|
||||
if (callback == undefined) {
|
||||
if (parameters != undefined) {
|
||||
// call the method with the provided parameters
|
||||
viewModel[method].apply(viewModel, parameters);
|
||||
} else {
|
||||
// call the method without parameters
|
||||
viewModel[method]();
|
||||
}
|
||||
} else {
|
||||
// provide the method to the callback
|
||||
callback(viewModel[method]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue