Created
May 20, 2014 18:39
-
-
Save frequent/6b7a20ed2be52a6b0040 to your computer and use it in GitHub Desktop.
RenderJS - retrieve method from subgadget on "main" 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"; | |
var TRANSLATION_GADGET = "./html/translations.html"; | |
// prevent JQM autoinit | |
$(document) | |
.on("mobileinit", function () { | |
$.mobile.autoInitializePage = false; | |
}); | |
rJS(window) | |
.ready(function (g) { | |
g.render(); | |
}) | |
.declareMethod('render', function () { | |
var gadget = this; | |
return RSVP.all([ | |
gadget.declareGadget(TRANSLATION_GADGET, { | |
element: gadget.__element, | |
scope: "i18n" | |
}) | |
]) | |
.then(function (gadget_list) { | |
var i, len, response_gadget, gadget_array = []; | |
for (i = 0, len = gadget_list.length; i < len; i += 1) { | |
response_gadget = gadget_list[i]; | |
if (response_gadget.render) { | |
gadget_array[i] = response_gadget.render(); | |
} | |
} | |
return RSVP.all(gadget_array); | |
}) | |
.then(function (response_list) { | |
// JQM handler | |
$(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]; | |
// enable deeplinks | |
} else if ($.mobile.navigate.history.initialDst && | |
window.location.hash !== "") { | |
clean_url = window.location.href.split("#")[0]; | |
parsed_url = $.mobile.path.parseUrl(clean_url); | |
target = $.mobile.navigate.history.initialDst; | |
delete $.mobile.navigate.history.initialDst; | |
$.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 new page (= gadget) when missing | |
if (target && !document.getElementById(target)) { | |
// QUESTION 1: WHY IS GADGET DEFINED HERE? WHAT ELSE SHOULD I USE? rJS(window) does not work | |
return gadget.declareGadget( | |
"./html/" + 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); | |
}) | |
.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(gadget.errorHandler); | |
} | |
}); | |
// start JQM | |
$.mobile.initializePage(); | |
$("[data-role='footer']").toolbar(); | |
// QUESTION 2: WHY CAN I NOT ACQUIRE ANYTHING HERE? | |
// console.log(gadget) | |
// console.log(gadget.__acquired_method_dict) | |
// console.log(gadget.__acquired_method_dict.acquire_changeLanguage) | |
// all are defined, but the line below: | |
return gadget.acquire_changeLanguage(); | |
// says "AcquisitionError: No gadget provides acquire_changeLanguage" | |
}) | |
.then(function (r) { | |
console.log("I NEVER TRIGGER"); | |
}) | |
.fail(console.log); | |
}) | |
.declareAcquiredMethod("acquire_changeLanguage", "acquire_changeLanguage") | |
.allowPublicAcquisition("acquire_changeLanguage", function (param_list) { | |
return this.getDeclaredGadget("i18n") | |
.then(function (i18n) { | |
return i18n.changeLanguage.apply(i18n, param_list); | |
}); | |
}); | |
}(window, document, rJS)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment