Create layout module

master
Buddy Sandidge 10 years ago
parent 44e4ce2a77
commit 07b9f12fac

@ -40,6 +40,7 @@
"module": true, "module": true,
"require": true, "require": true,
"requirejs": true, "requirejs": true,
"spyOn": true,
"xdescribe": true, "xdescribe": true,
"xit": true "xit": true
} }

@ -1,25 +1,22 @@
define([ define([
'marionette', 'marionette',
'jquery', 'core/layout/module'
'template' ], function AppDefine(Marionette, Layout) {
], function AppDefine(Marionette, $, tmpl) {
'use strict'; 'use strict';
var Application = Marionette.Application; var Application = Marionette.Application;
function App() { function App(options) {
Application.apply(this, arguments);
}
App.prototype = new Application();
App.prototype.onStart = function onStart(options) {
if (options == null) { if (options == null) {
options = {}; options = {};
} }
if (options.el == null) { Application.apply(this, arguments);
options.el = 'body';
} }
$(options.el).html(tmpl({title: 'started'}));
App.prototype = new Application();
App.prototype.initialize = function initialize() {
this.module('layout', Layout);
}; };
return App; return App;

@ -0,0 +1,18 @@
define(['backbone'], function LayoutModelDefine(Backbone) {
'use strict';
var Model = Backbone.Model;
function LayoutModel() {
Model.apply(this, arguments);
}
LayoutModel.prototype = new Model();
LayoutModel.prototype.defaults = function defaults() {
return {
title: 'some title'
};
};
return LayoutModel;
});

@ -0,0 +1,26 @@
define([
'underscore',
'marionette',
'core/layout/view',
'core/layout/model'
], function LayoutModuleDefine(_, Marionette, LayoutView, LayoutModel) {
'use strict';
var LayoutModule = Marionette.Module.extend({
Model: LayoutModel,
View: LayoutView,
onStart: function(options) {
var opts = _.defaults({}, options || {}, {layout: {}});
_.defaults(opts.layout, {view: {}, model: {}});
var config = opts.layout;
this.model = new this.Model(config.model);
config.view.model = this.model;
this.view = new this.View(config.view);
this.view.render();
}
});
return LayoutModule;
});

@ -0,0 +1,12 @@
define([
'marionette',
'core/layout/template'
], function CoreLayoutDefine(Marionette, template) {
'use strict';
var CoreLayout = Marionette.LayoutView.extend({
template: template
});
return CoreLayout;
});

@ -1,9 +1,10 @@
define('main', ['config', 'app'], function main(config, App) { define('main', ['underscore', 'config', 'app'], function main(_, config, App) {
'use strict'; 'use strict';
requirejs.config(config()); requirejs.config(config());
return function main(options) { return function main(options) {
var app = new App(); var opts = _.defaults({}, options, {config: {}});
app.start(options); var app = new App(opts);
app.start(opts.config);
return app; return app;
}; };
}); });

@ -15,7 +15,7 @@
<script> <script>
(function (global) { (function (global) {
require(['main'], function _main(main) { require(['main'], function _main(main) {
global.app = main({el: '#deck'}); global.app = main({config: {layout: {view: {el: '#deck'}}}});
}); });
}(this)); }(this));
</script> </script>

@ -15,8 +15,9 @@ module.exports = function(config) {
{pattern: 'test/**/!(test-main).js', included: false}, {pattern: 'test/**/!(test-main).js', included: false},
{pattern: 'static/**/!(config).js', included: false}, {pattern: 'static/**/!(config).js', included: false},
{pattern: 'static/**/*.map', included: false}, {pattern: 'static/**/*.map', included: false},
{pattern: 'static/app/config.js', included: true}, 'static/style/main.css',
{pattern: 'test/test-main.js', included: true} 'static/app/config.js',
'test/test-main.js'
], ],
// list of files to exclude // list of files to exclude

@ -11,5 +11,11 @@ define(['app'], function (App) {
}).not.toThrow(); }).not.toThrow();
}); });
}); });
describe('→ has layout', function () {
it('→ exits', function () {
expect(new App().layout).not.toBeUndefined();
});
});
}); });
}); });

@ -0,0 +1,13 @@
define(['core/layout/model'], function (LayoutModel) {
describe('LayoutModel', function () {
it('→ exits', function () {
expect(new LayoutModel()).not.toBeUndefined();
});
describe('→ has defaults', function () {
it('→ title', function () {
expect(typeof (new LayoutModel()).get('title')).toBe('string');
});
});
});
});

@ -0,0 +1,22 @@
define([
'marionette',
'core/layout/module'
], function (Marionette, LayoutModule) {
describe('LayoutModule', function () {
it('→ exits', function () {
var app = new Marionette.Application();
var module = new LayoutModule('layout', app, {});
expect(module).not.toBeUndefined();
});
describe('→ onStart', function () {
it('→ will render view', function () {
var app = new Marionette.Application();
var module = new LayoutModule('layout', app, {});
spyOn(module.View.prototype, 'render');
module.onStart();
expect(module.View.prototype.render).toHaveBeenCalled();
});
});
});
});

@ -0,0 +1,7 @@
define(['core/layout/view'], function (LayoutView) {
describe('→ LayoutView', function () {
it('→ exists', function () {
expect(new LayoutView()).not.toBeUndefined();
});
});
});
Loading…
Cancel
Save