chapter: Extending the Framework ================== (function($){ // We wrap all the code inside this function to be sure that $ is // linked to the jQuery global object // Widget definition $.widget("mobile.ourWidgetName", $.mobile.widget, { options: { // Here we can create default options of our widget }, // Private methods _create: function() { // The constructor function }, // Public methods enable: function() { // Enable the widget }, disable: function() { // Disable the widget }, refresh: function() { // Refresh the widget } }); // End of widget definition // Auto-initialization code $(document).bind("pagecreate", function(event) { // We find data-role's and apply our widget constructor $(event.target).find(":jqmData(role='ourWidgetName')").ourWidgetName(); }); }(jQuery)); ==================================== $.widget("mobile.dynamicimage", $.mobile.widget, { options: { width: "100%", margin: 0 } }); ==================================== $.widget("mobile.dynamicimage", $.mobile.widget, { options: { width: 100, margin: 0 }, _create: function() { // We call a private function this._loadURL(); } }); ==================================== $.widget("mobile.dynamicimage", $.mobile.widget, { options: { width: 100, margin: 0 }, _create: function() { // We call a private function this._loadURL(); }, // Public methods enable: function() { // Enable the widget $(this.element).attr('disabled', ''); }, disable: function() { // Disable the widget $(this.element).removeAttr('disabled'); }, refresh: function() { // Refresh the widget this._loadURL(); } }); ==================================== _loadURL: function() { // this.element will be our +img+ element var url; // we create the service URL url = "http://src.sencha.io/"; var parameters = ""; if (!isNaN(this.options.width)) { parameters += "x" + this.options.width; } if ((!isNaN(this.options.margin)) && this.options.margin>0) { parameters += "-" + this.options.margin; } if (parameters.length>0) { url += parameters + "/"; } // Sencha IO needs an absolute URL var originalUrl = $(this.element).jqmData("src"); if (originalUrl.length>1) { var newUrl = ""; if ($.mobile.path.isAbsoluteUrl(originalUrl)) { // The image URL is already absolute newUrl = originalUrl; } else { // The image URL is relative, we create an absolute one var baseUrl = $.mobile.path.parseUrl(location.href); var baseUrlWithoutScript = baseUrl.protocol + "//" + baseUrl.host + baseUrl.directory; newUrl = $.mobile.path.makeUrlAbsolute(originalUrl, baseUrlWithoutScript); } url += newUrl; $(this.element).attr("src", url); } ==================================== $(document).bind("pagecreate", function(event) { // We find data-role's and apply our widget constructor $(event.target).find("img:jqmData(role='dynamic-image')").dynamicimage(); }); ==================================== ==================================== <-- Image taking the device's 100% width --> <-- Image taking the device's 40% width --> <-- Image taking the device's 100% width with 20 pixels of margin --> ==================================== (function($){ // Widget definition $.widget( "mobile.dynamicimage", $.mobile.widget, { options: { margin: 0, width: 100 }, _create: function() { this._loadURL(); }, // Private methods _loadURL: function() { // this.element will be our +img+ element var url; // we create the service URL url = "http://src.sencha.io/"; var parameters = ""; if (!isNaN(this.options.width)) { parameters += "x" + this.options.width; } if ((!isNaN(this.options.margin)) && this.options.margin>0) { parameters += "-" + this.options.margin; } if (parameters.length>0) { url += parameters + "/"; } // Sencha IO needs an absolute URL var originalUrl = $(this.element).jqmData("src"); if (originalUrl.length>1) { var newUrl = ""; if ($.mobile.path.isAbsoluteUrl(originalUrl)) { // The image URL is already absolute newUrl = originalUrl; } else { // The image URL is relative, we create an // absolute one var baseUrl = $.mobile.path.parseUrl(location.href); var baseUrlWithoutScript = baseUrl.protocol + "//" + baseUrl.host + baseUrl.directory; newUrl = $.mobile.path.makeUrlAbsolute(originalUrl, baseUrlWithoutScript); } url += newUrl; $(this.element).attr("src", url); } }, // Public methods enable: function() { // Enable the widget $(this.element).attr('disabled', ''); }, disable: function() { // Disable the widget $(this.element).removeAttr('disabled'); }, refresh: function() { // Refresh the widget this._loadURL(); } }); // End of widget definition // Auto-initialization code $(document).bind("pagecreate", function(event) { // We find data-role's and apply our widget constructor $(event.target).find("img:jqmData(role='dynamic-image')").dynamicimage(); }); }(jQuery)); ==================================== ====================================
==================================== ==================================== ==================================== $("#button").click(function() { $(this).simpledialog({ mode: 'bool', // For normal alert or confirm prompt: "We could not open the file", useModal: true, buttons: [ 'Ok': { theme: "c", icon: "check" } ] }); }); ==================================== $("#button").click(function() { $(this).simpledialog({ mode: 'bool', prompt: "Do you want to delete this file?", useModal: true, buttons: [ 'Yes': { theme: "c", icon: "delete", click: function() { // Delete } }, 'No': { theme: "a", icon: "cancel" }, ] }); }); ==================================== $("#button").click(function() { $(this).simpledialog({ mode: 'string', prompt: "What is your name?", useModal: true, buttons: [ 'No': { theme: "c", icon: "delete", click: function() { alert("Your name is " + $("#button").jqmData("string"); } } ] }); }); ==================================== Share
Share in Facebook Share in Twitter Share in Google+ Cancel
====================================
==================================== Demos ==================