Skip to content

Instantly share code, notes, and snippets.

@mbritton
Created March 27, 2013 16:34
Show Gist options
  • Select an option

  • Save mbritton/5255767 to your computer and use it in GitHub Desktop.

Select an option

Save mbritton/5255767 to your computer and use it in GitHub Desktop.
Potential organization: Node required?
YUI.add('test-app', function(Y) {
Y.app.TestApp = Y.Base.create('TestApp', Y.App, [], {
apps : {},
start : function() {
if (this.hasRoute(this.getPath())) {
this.dispatch();
} else {
this.replace('/');
}
},
render : function() {
console.log('render');
if (!this.template) {
var templateId = this.get('templateId');
if (!templateId) {
throw new Error('app: No templateId provided');
}
this.get('container').setHTML(Y.one(templateId).getContent());
}
},
startTestApp : function(callback) {
console.log('startTestApp');
if (!this.apps.TestApp) {
this.apps.TestApp = new Y.app.TestApp({
templateId : '#test-app-template',
container : '#main'
});
}
this.apps.TestApp.start(callback);
},
startTestSubApp : function(callback) {
console.log('startTestSubApp');
if (!this.apps.TestSubApp) {
this.apps.TestSubApp = new Y.app.TestSubApp({
templateId : '#test-sub-app-template',
container : '#main'
});
}
this.apps.TestSubApp.start(callback);
},
openAppScreen : function(route) {
var id = route.params.id, self = this;
this.render();
},
openSubAppScreen : function(route) {
var id = route.params.id, self = this;
// start app
this.startTestSubApp(function() {
self.apps.TestSubApp.render();
});
},
}, {
ATTRS : {
routes : {
value : [{
path : '/yui/',
callback : 'openAppScreen'
}, {
path : '/test/',
callback : 'openSubAppScreen'
}]
},
templateId : null
}
});
}, '0.0.1', {
requires : ['app', 'test-sub-app', 'test-app']
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>YUI App Framework Test Page</title>
<meta name="description" content="" />
<meta name="author" content="Mike Britton" />
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no" />
<script src="http://yui.yahooapis.com/3.9.0/build/yui/yui-min.js"></script>
<script src="js/subapp.js"></script>
<script src="js/app.js"></script>
<script src="js/main.js"></script>
<link rel="stylesheet" href="css/main.css" />
</head>
<body>
<div id="main">&nbsp;</div>
<script type="text/x-handlebars-template" id="test-app-template">
<h1>My Test App</h1>
</script>
<script type="text/x-handlebars-template" id="test-sub-app-template">
<h2>My Test Sub App</h2>
</script>
</script>
</body>
</html>
/**
* TestApp is defined in app.js
*/
YUI().use(['test-app'], function(Y) {
var app = new Y.app.TestApp({
templateId : '#test-app-template',
container : '#main'
});
app.start();
});
YUI.add('test-sub-app', function(Y) {
Y.namespace('app');
Y.app.TestSubApp = Y.Base.create('TestSubApp', Y.App, [], {
start : function(callback) {
if (!this.get('started')) {
this.set('started', true);
if (callback)
callback();
} else {
// already started so execute callback immediately
if (callback)
callback();
}
},
render : function() {
this.get('container').setHTML(Y.one(this.get('templateId')).getContent());
return this;
}
});
}, '0.0.1', {
requires : ['app']
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment