1

As I searched through Stackoverflow and Google, I couldn't find a solution to simplifying my code (or maybe I haven't found the exact search term). It really bugs me that I couldn't simplify the code below.

Since I'm a beginner in this field, I had to repeat all the same code all over again, when there are similarities that I can combine.

Your help is much appreciated!

$('.detail-view').dialog({
  autoOpen: false,
  draggable: false,
  resizable: false,
  closeOnEscape: true,
  modal: true,
  height: 'auto',
  width: 600,
  position: ['top', 150] });

$('.forgotpass').dialog({
  autoOpen: false,
  draggable: false,
  resizable: false,
  closeOnEscape: true,
  modal: true,
  height: 'auto',
  width: 400,
  position: ['top', 150] });

$('.user0').load('php/usersetting.php').dialog({
  autoOpen: true,
  draggable: false,
  resizable: false,
  closeOnEscape: false,
  modal: true,
  height: 'auto',
  width: 500,
  position: ['top', 150],
  open: function(){
    $(this).parent().find('.ui-dialog-titlebar-close').hide();
  } });

$('.user1').load('php/usersetting.php').dialog({
  autoOpen: false,
  draggable: false,
  resizable: false,
  modal: true,
  height: 'auto',
  width: 500,
  position: ['top', 100] });

$('.user2').load('php/usersetting.php').dialog({
  autoOpen: true,
  draggable: false,
  resizable: false,
  closeOnEscape: true,
  modal: true,
  height: 'auto',
  width: 500,
  position: ['top', 150],
  open: function(){
    $(this).parent().find('.ui-dialog-titlebar-close').hide();
  }
  });
1
  • Start with a basic object and then use $.extend() to make variations. Also, take advantage of the , in selector syntax to combine separate calls. Commented Jul 20, 2013 at 15:20

2 Answers 2

2

Since the vast majority of your options are the same, a simple answer is to create a default options object:

var defaults = {
  autoOpen: false,
  draggable: false,
  resizable: false,
  closeOnEscape: true,
  modal: true,
  height: 'auto',
  width: 200,
  position: ['top', 150]
};

and then use $.extend with the specific options you want to override:

$('.detail-view').dialog($.extend({}, defaults, {
  width: 600
}));
$('.forgotpass').dialog($.extend({}, defaults, {
  width: 400
}));

You could encapsulate this in a function:

function createDialog(selector, options) {
    $(selector).dialog($.extend({}, defaults, options));
}

createDialog('.detail-view', {
  width: 600
});
createDialog('.forgotpass', {
  width: 400
});

Or of course if it's just width:

function createDialog(selector, width) {
    $(selector).dialog($.extend({}, defaults, {width: width}));
}

createDialog('.detail-view', 600);
createDialog('.forgotpass', 400);
Sign up to request clarification or add additional context in comments.

2 Comments

Even without trying the code, I can understand its simplicity. You're fast and I'm gonna try it now! Thanks! I'll return shortly.
Done and works! Thanks a lot for introducing me to $.extend !
0

Create an object and then pass it to each dialog() function: var dialog_properties = {autoOpen:false, drag able: false, ...};

Then use it in all dialog() functions that are similar : $('.detailView').dialog(dialog_properties); $('.forgotPass').dialog(dialog_properties); ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.