Skip to content

Instantly share code, notes, and snippets.

@stevepond
Created April 5, 2012 17:17
Show Gist options
  • Select an option

  • Save stevepond/2312605 to your computer and use it in GitHub Desktop.

Select an option

Save stevepond/2312605 to your computer and use it in GitHub Desktop.

We are having trouble getting our require.js dependancies to work in phonegap with Android.
The issue seems specific to Android.

###Device: HTC Nexus### ###PhoneGap Build: 1.4###

application_controllers.js

define([
'mediator', 'backbone', 'use!layoutmanager'
], function(
mediator, Backbone
  ) {
var ApplicationController = function() {
	_.bindAll(this);
	mediator.subscribe('startupController!', this.startupController);
};

var controllers = {};

// starts up a controller if it hasn't already been loaded
// this should probably be moved into autoload functionality in the mediator
// instead of using the command pattern.
// try this out for a while and refactor if needed
ApplicationController.prototype.startupController = function(controllerName, Controller, params) {
	if (!controllers[controllerName]) {
		controllers[controllerName] = new Controller();
	}
};
return ApplicationController;

});

mediator.js

 define(['backbone', 'underscore', 'helpers'], function(Backbone, _, helpers) {
'use strict';
// mediator used for pub/sub across application. facilitates cross-module
// communication
var desc, mediator;
mediator = {};
_(mediator).defaults(Backbone.Events);

mediator._callbacks = null;

// autoload will bring in a controller and fire a startupController! event
mediator._autoload = function(channel) {
	var i, l,
	self = this,
	args = arguments,
	// the "controller" is before the colon, e.g. trip_items:add
	file = channel.split(':')[0];

	require(["controllers/" + file + "_controller"], function (Controller) {
		// let application controller initialize the controller
		Backbone.Events.trigger.apply(self, ['startupController!', file, Controller]);
		// fire the original event so subscribers can do stuff
		Backbone.Events.trigger.apply(self, args);
	});
};

mediator.subscribe = mediator.on = Backbone.Events.on;

mediator.unsubscribe = mediator.off = Backbone.Events.off;

mediator.publish = mediator.trigger = function(channel) {
	// if there is nothing listening to this event, try
	// to autoload a controller that has the same channel name
	if (!mediator._callbacks[channel]) {
		mediator._autoload.apply(this, arguments);
		return;
	}
	Backbone.Events.trigger.apply(this, arguments);
};


if (Object.defineProperties) {
	desc = {
		writable: false
	};
	Object.defineProperties(mediator, {
		subscribe: desc,
		unsubscribe: desc,
		publish: desc
	});
}
if (typeof Object.seal === "function") Object.seal(mediator);
return mediator;

});

logcat output

D/DroidGap( 1652): DroidGap.onCreate()
D/DroidGap( 1652): DroidGap.loadUrl(file:///android_asset/www/mobile/index.html)
D/DroidGap( 1652): DroidGap: url=file:///android_asset/www/mobile/index.html               baseUrl=file:///android_asset/www/mobile/
D/DroidGap( 1652): DroidGap.init()
D/SoftKeyboardDetect( 1652): Ignore this event
D/SoftKeyboardDetect( 1652): Ignore this event
I/ActivityManager(   96): Displayed com.phonegap.example/.PhoneGapExample: +375ms
D/SoftKeyboardDetect( 1652): Ignore this event
D/szipinf ( 1652): Initializing inflate state
D/szipinf ( 1652): Initializing zlib to inflate
D/PhoneGapLog( 1652): Uncaught TypeError: Property 'subscribe' of object #<an Object> is     not a function
D/PhoneGapLog( 1652):     file:///android_asset/www/mobile/app/controllers/application_controller.js: Line 8 : Uncaught     TypeError: Property 'subscribe' of object #<an Object> is not a function
E/Web Console( 1652): Uncaught TypeError: Property 'subscribe' of object #<an Object> is   not a function at     file:///android_asset/www/mobile/app/controllers/application_controller.js:8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment