Browse Source

Improved .setTitle() to make it possible to update title each time it's invoked

Dante 12 years ago
parent
commit
0382385103
2 changed files with 34 additions and 4 deletions
  1. 23 2
      examples/index.html
  2. 11 2
      js/bootstrap-dialog.js

+ 23 - 2
examples/index.html

@@ -6,7 +6,7 @@
 <script src="assets/bootstrap/js/bootstrap.min.js"></script>
 <script src="assets/prettify/run_prettify.js"></script>
 <link href="assets/bootstrap-dialog/css/bootstrap-dialog.css" rel="stylesheet" type="text/css" />
-<script src="assets/bootstrap-dialog/js/bootstrap-dialog.js"></script>
+<script src="../js/bootstrap-dialog.js"></script>
 <meta charset="utf-8" />
 <title>BootstrapDialog examples</title>
 <style>
@@ -50,7 +50,7 @@
         -->
     </div>
     
-    <h3>Title</h3>
+    <h3>Dialog Title</h3>
     <div class="source-code runnable">
         <!--
         BootstrapDialog.show({
@@ -59,6 +59,27 @@
         });
         -->
     </div>
+    
+    <h3>Manipulating Dialog Title</h3>
+    <div class="source-code runnable">
+        <!--
+        BootstrapDialog.show({
+            title: 'Default Title',
+            message: 'Click buttons below.',
+            buttons: [{
+                label: 'Title 1',
+                action: function(dialog) {
+                    dialog.setTitle('Title 1');
+                }
+            }, {
+                label: 'Title 2',
+                action: function(dialog) {
+                    dialog.setTitle('Title 2');
+                }
+            }]
+        });
+        -->
+    </div>
 
     <h3>Buttons</h3>
     <div class="source-code runnable">

+ 11 - 2
js/bootstrap-dialog.js

@@ -239,6 +239,15 @@ var BootstrapDialog = null;
         },
         setTitle: function(title) {
             this.options.title = title;
+            this.updateTitle();
+
+            return this;
+        },
+        updateTitle: function() {
+            if (this.isRealized()) {
+                var title = this.getTitle() !== null ? this.createDynamicContent(this.getTitle()) : this.getDefaultText();
+                this.getModalHeader().find('.' + this.getNamespace('title')).html('').append(title);
+            }
 
             return this;
         },
@@ -337,7 +346,6 @@ var BootstrapDialog = null;
         createTitleContent: function() {
             var $title = $('<div></div>');
             $title.addClass(this.getNamespace('title'));
-            $title.append(this.getTitle() !== null ? this.createDynamicContent(this.getTitle()) : this.getDefaultText());
 
             return $title;
         },
@@ -434,7 +442,7 @@ var BootstrapDialog = null;
 
             // Button hotKey
             if (button.hotKey)
-                this.getModalDialog().on('keypress', function(event){
+                this.getModalDialog().on('keypress', function(event) {
                     if (!event || !event.which)
                         return;
                     if (event.which === button.hotKey)
@@ -629,6 +637,7 @@ var BootstrapDialog = null;
         },
         open: function() {
             !this.isRealized() && this.realize();
+            this.updateTitle();
             this.updateClosable();
             this.getModal().modal('show');
             this.setOpened(true);