Skip to content

Instantly share code, notes, and snippets.

@Thanood
Last active January 30, 2016 01:11
Show Gist options
  • Save Thanood/88123a68870e99bca580 to your computer and use it in GitHub Desktop.
Save Thanood/88123a68870e99bca580 to your computer and use it in GitHub Desktop.
jQueryUiAdapter.prototype.openDialog = function (settings) {
var dfd = system.defer();
var defaultSettings = {
appendTo: 'body',
dialogHost: 'dialogHost',
dialogPromise: dfd,
title: '',
cssClass: '',
viewmodel: null,
autoOpen: false,
modal: true,
height: ($(window).height() / 3) * 2,//'auto',
width: ($(window).width() / 3), //'auto',
validate: function (model) { return true; },
buttons: [
{
text: 'Apply',
click: function () {
var validation = settings.validate(dialogModel);
if (validation === true) {
dfd.resolve(dialogModel);
$(this).removeClass(settings.cssClass);
$(this).dialog('close');
} else {
$('.error', '#' + settings.dialogHost).removeClass('error');
if (_.isArray(validation)) {
_.each(validation, function (selector) {
$(selector, '#' + settings.dialogHost).addClass('error');
});
}
}
}
},
{
text: 'Close',
click: function () {
dfd.resolve();
$(this).removeClass(settings.cssClass);
$(this).dialog('close');
}
}
]
};
var settings = _.defaults(settings, defaultSettings);
var host = $('#' + settings.dialogHost);
if (host.length == 0) {
host = $('<div id="' + settings.dialogHost + '" title="' + settings.title + '"></div>').dialog(settings);
} else {
host.dialog('option', settings);
}
host.addClass(settings.cssClass);
var dialogModel = null;
composition.compose(host.get(0), {
model: settings.viewmodel,
binding: function (element, view, bindSettings) {
return system.defer(function (bindingDeferred) {
if (settings.data) {
if (_.isFunction(settings.data)) {
settings.data(bindSettings.model).then(function () {
dialogModel = bindSettings.model;
host.dialog('open');
bindingDeferred.resolve();
});
} else {
_.each(settings.data, function (setting, key) {
var target = bindSettings.model[key];
if (target && ko.isObservable(target)) {
bindSettings.model[key](setting);
} else {
bindSettings.model[key] = setting;
}
});
dialogModel = bindSettings.model;
host.dialog('open');
bindingDeferred.resolve();
}
} else {
dialogModel = bindSettings.model;
host.dialog('open');
}
}).promise();
}
});
return dfd.promise().then(function (_dialogModel) {
if (_dialogModel && _dialogModel.canDeactivate && _dialogModel.canDeactivate(true) && _dialogModel.deactivate) {
_dialogModel.deactivate(true);
}
return _dialogModel;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment