Permalink
Browse files

- Reorganized and introduced 'TestRootMojit'.

- Moved template, etc. from HTMLMojit to TestRootMojit.
  • Loading branch information...
1 parent c41dc20 commit e177114e091dd69dfb2943b0fa443d588adac0ea @add0n add0n committed Nov 8, 2012
View
@@ -116,8 +116,22 @@ YUI.add('mojito-yaf', function (Y, NAME) {
return Y.one('body');
},
- loadTemplateFrom: function (uri) {
+ loadTemplate: function () {
+ var loc,
+ rootLoc,
+ templateLoc;
+
+ templateLoc = this.get('templateLocation');
+
+ if (templateLoc && templateLoc.length > 0) {
+ loc = window.location.href;
+ rootLoc = loc.slice(0, loc.lastIndexOf('/') + 1);
+
+ this.loadTemplateFrom(rootLoc + templateLoc);
+ }
+ },
+ loadTemplateFrom: function (uri) {
var cfg,
request,
templateText;
@@ -142,18 +156,26 @@ YUI.add('mojito-yaf', function (Y, NAME) {
container = this.get('container');
+ if (!(template = this.get('template'))) {
+ return;
+ }
+
if (!(model = this.get('model'))) {
html = this.get('templateEngine').render(
- this.get('template'),
+ template,
null);
} else {
html = this.get('templateEngine').render(
- this.get('template'),
+ template,
this.get('model').toJSON());
}
container.append(html);
}
+ }, {
+ ATTRS: {
+ 'templateLocation': {value: null},
+ }
}
);
@@ -10,9 +10,9 @@ YUI.add('HTMLMojit', function (Y, NAME) {
MOJITO_NS.HTMLMojitView = Y.Base.create('HTMLMojitView', Y.mojito.View, [],
{
- initializer: function () {
- // Handlebars template
- this.set('template', '<span>Foofy!</span>');
+ }, {
+ ATTRS: {
+ 'templateLocation': {value: null},
}
}
);
@@ -33,20 +33,14 @@ YUI.add('HTMLMojit', function (Y, NAME) {
MOJITO_NS.HTMLMojit = Y.Base.create('HTMLMojit', Y.mojito.Mojit, [],
{
initializer: function () {
- var loc,
- rootLoc,
- msgView;
+ var msgView;
msgView = new Y.mojito.HTMLMojitView({id: this.get('id'),
mojit: this});
msgView.set('templateEngine',
new Y.mojito.Template(Y.Handlebars));
- loc = window.location.href;
- rootLoc = loc.slice(0, loc.lastIndexOf('/') + 1);
-
- msgView.loadTemplateFrom(
- rootLoc + 'lib/mojits/HTMLMojit/templates/index.hb.html');
+ msgView.loadTemplate();
msgView.render();
this.addViewNamed(msgView, 'indexView');
View
@@ -47,19 +47,22 @@
-->
<script type="text/javascript" src="./lib/mojito-yaf.js"></script>
-
<script type="text/javascript" src="./lib/mojits/HTMLMojit/controller.client.js"></script>
+
<script type="text/javascript" src="./tests/test-dispatch.js"></script>
+ <script type="text/javascript" src="./tests/mojits/TestRootMojit/controller.client.js"></script>
+ <script type="text/javascript" src="./tests/mojits/MsgMojit/controller.client.js"></script>
<script type="text/javascript">
- YUI().use('mojito-yaf', 'mojito-test-mojits', 'HTMLMojit', function (Y) {
+ YUI().use('mojito-yaf', 'HTMLMojit', 'TestRootMojit', 'MsgMojit', function (Y) {
// ---
// !TODO! Hardcoded data in lieu of application.json / routes.json
// This is a hack for this test!
var instanceMap = {
'mainMojit' : 'mojito.HTMLMojit',
+ 'rootMojit' : 'mojito.TestRootMojit',
'msgMojit' : 'mojito.MsgMojit'
};
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2011-2012, Yahoo! Inc. All rights reserved.
+ * Copyrights licensed under the New BSD License.
+ * See the accompanying LICENSE file for terms.
+ */
+
+YUI.add('MsgMojit', function (Y, NAME) {
+
+ var MOJITO_NS = Y.namespace('mojito');
+
+ MOJITO_NS.MsgView = Y.Base.create('MsgView', Y.mojito.View, [],
+ {
+ initializer: function () {
+ // Regular Y.Lang sub template
+ //this.set('template', 'The msg is: {msg}<br>');
+
+ // Handlebars template
+ this.set('template', 'The msg is: {{msg}}<br>');
+ }
+ }
+ );
+
+ // ---
+
+ MOJITO_NS.MsgHandler = Y.Base.create('MsgHandler', Y.mojito.Handler, [],
+ {
+ setupEventBindings: function () {
+ // Make sure and set up the auto bindings
+ this.constructor.superclass.setupEventBindings.apply(
+ this, arguments);
+
+ Y.one('#setMsgButton').on(
+ 'click',
+ function () {
+ this.fire('mojit:setMsg',
+ {msg: Y.one('#setMsgText').get('value')});
+ }.bind(this));
+ }
+ }, {
+ ATTRS: {
+ eventBindings: {value:
+ [{selector: '#saveMsgButton',
+ domEvent: 'click',
+ mojitEvent: 'mojit:saveMsg'}]},
+ routes: {value: [{route: '/foo', event: 'foo:fooEvent'}]}
+ }
+ }
+ );
+
+ // ---
+
+ MOJITO_NS.MsgModel = Y.Base.create('MsgModel', Y.Model, [Y.ModelSync.Local],
+ {
+ root: 'mojito-test',
+ }
+ );
+
+ // ---
+
+ MOJITO_NS.MsgMojit = Y.Base.create('MsgMojit', Y.mojito.Mojit, [],
+ {
+ initializer: function () {
+ var msgModel;
+ var msgView;
+
+ msgModel = new Y.mojito.MsgModel({msg: 'Howdy!'});
+ this.get('models')['msgHolder'] = msgModel;
+
+ msgView = new Y.mojito.MsgView({model: msgModel,
+ id: this.get('id'),
+ mojit: this});
+ msgView.set('templateEngine',
+ new Y.mojito.Template(Y.Handlebars));
+ msgView.render();
+
+ this.addViewNamed(msgView, 'msgView');
+
+ this.setupEventObservations();
+ },
+
+ onMojitIndex: function (evt) {
+ console.log('got to "mojit:index" event handler');
+ },
+
+ onMojitSetMsg: function (evt) {
+ this.get('models')['msgHolder'].set('msg', evt.msg);
+ },
+
+ onMojitSaveMsg: function (evt) {
+ this.get('models')['msgHolder'].save();
+ },
+
+ onFooFooEvent: function (evt) {
+ console.log('got to "foo:fooEvent" event handler');
+ }
+ }, {
+ ATTRS: {
+ mojitEvents: {value:
+ ['mojit:index', 'mojit:setMsg', 'mojit:saveMsg',
+ 'foo:fooEvent']},
+ handlerType: {value: MOJITO_NS.MsgHandler}
+ }
+ }
+ );
+
+}, '0.0.1', {
+ requires: [
+ 'mojito-yaf',
+ 'handlebars',
+ 'gallery-model-sync-local'
+ ]
+});
+
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2011-2012, Yahoo! Inc. All rights reserved.
+ * Copyrights licensed under the New BSD License.
+ * See the accompanying LICENSE file for terms.
+ */
+
+YUI.add('TestRootMojit', function (Y, NAME) {
+
+ var MOJITO_NS = Y.namespace('mojito');
+
+ MOJITO_NS.TestRootMojitView = Y.Base.create('TestRootMojitView', Y.mojito.View, [],
+ {
+ getDOMAttachPoint: function () {
+ return Y.one('#mainMojit');
+ }
+ }, {
+ ATTRS: {
+ 'templateLocation': {value: 'tests/mojits/TestRootMojit/templates/index.hb.html'},
+ }
+ }
+ );
+
+ // ---
+
+ MOJITO_NS.TestRootMojitHandler = Y.Base.create('TestRootMojitHandler', Y.mojito.Handler, [],
+ {
+ }, {
+ ATTRS: {
+ routes: {value: [{route: '/', event: 'mojit:index'}]}
+ }
+ }
+ );
+
+ // ---
+
+ MOJITO_NS.TestRootMojit = Y.Base.create('TestRootMojit', Y.mojito.Mojit, [],
+ {
+ initializer: function () {
+ var msgView;
+
+ msgView = new Y.mojito.TestRootMojitView({id: this.get('id'),
+ mojit: this});
+ msgView.set('templateEngine',
+ new Y.mojito.Template(Y.Handlebars));
+
+ msgView.loadTemplate();
+ msgView.render();
+
+ this.addViewNamed(msgView, 'indexView');
+
+ this.setupEventObservations();
+ },
+
+ onMojitIndex: function (evt) {
+ console.log('got to TestRootMojit::index');
+ }
+
+ }, {
+ ATTRS: {
+ mojitEvents: {value: ['mojit:index']},
+ handlerType: {value: MOJITO_NS.TestRootMojitHandler}
+ }
+ }
+ );
+
+}, '0.0.1', {
+ requires: [
+ 'mojito-yaf',
+ 'handlebars'
+ ]
+});
Oops, something went wrong.

0 comments on commit e177114

Please sign in to comment.