Created
May 27, 2014 17:17
-
-
Save frequent/48507187ef8c006c6d15 to your computer and use it in GitHub Desktop.
how to drop a gadget?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jslint indent: 2, maxlen: 80, nomen: true */ | |
/*global console, window, document, rJS, RSVP, $ */ | |
(function (window, document, rJS) { | |
"use strict"; | |
rJS(window) | |
/** | |
* Override navigation of jQuery Mobile and allow deeplinking. Missing | |
* pages will now be loaded and added to the DOM as new gadgets - and | |
* removed again once the user leaves them. | |
* | |
* @method render | |
* @param {Object} gadget Gadget object | |
*/ | |
.declareMethod('render', function () { | |
var gadget = this; | |
// override | |
$(document) | |
.on('pagebeforechange', function (e, data) { | |
var target, clean_url, parsed_url, is_enhanced; | |
if (e && data) { | |
if (typeof data.toPage === "string") { | |
target = data.toPage.split("#")[1]; | |
// overwrite JQM history | |
} else if ($.mobile.navigate.history.initialDst && | |
window.location.hash !== "") { | |
// this is the inital page (not) loaded | |
// this page needs to be stored as initial page in JQM | |
clean_url = window.location.href.split("#")[0]; | |
parsed_url = $.mobile.path.parseUrl(clean_url); | |
target = $.mobile.navigate.history.initialDst; | |
// remove initialDist, otherwise closing popups | |
// will trigger double backward transition. | |
delete $.mobile.navigate.history.initialDst; | |
// Correctly set index as first page in JQM history | |
$.mobile.navigate.history.stack[0].hash = ""; | |
$.mobile.navigate.history.stack[0].url = clean_url; | |
$.mobile.path.documentUrl = parsed_url; | |
$.mobile.path.documentBase = parsed_url; | |
} | |
} | |
// load "missing pages" into DOM and re-trigger transition | |
if (target && !document.getElementById(target)) { | |
return gadget.declareGadget( | |
"./" + target + ".html", | |
{"element": gadget.__element, "scope": target} | |
) | |
.then(function (new_gadget) { | |
if (new_gadget.render) { | |
new_gadget.render(); | |
} | |
$.mobile.changePage("#" + target); | |
document.getElementById(target) | |
.setAttribute("data-external-page", true); | |
// translate | |
return gadget.acquire_translateAll($(new_gadget.__element)); | |
}) | |
.fail(console.log); | |
} | |
}) | |
.on('pagehide', 'div.ui-page', function () { | |
var page, id, is_enhanced; | |
page = this; | |
id = this.id; | |
is_enhanced = $(page).data("mobilePage"); | |
if (page.getAttribute('data-external-page')) { | |
if (is_enhanced) { | |
$(this).page('destroy').remove(); | |
} | |
return gadget.dropGadget(id).fail(console.log); | |
} | |
}); | |
}) | |
/* ======================== METHODS TO EXPOSE ========================= */ | |
/* ========================= METHODS NEEDED =========================== */ | |
.declareAcquiredMethod("acquire_translateAll", "acquire_translateAll"); | |
}(window, document, rJS)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment