From 917d08ac9f14c47618e9f8f24a9d6fb525530432 Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Fri, 13 Feb 2015 17:13:59 -0800 Subject: [PATCH] Can fetch data from server --- .jshintrc | 2 ++ assets/app/app.js | 16 ++++++++++++---- assets/app/core/layout/model.js | 16 ++++++++++------ assets/app/core/layout/module.js | 1 + assets/app/core/layout/view.js | 6 +++++- bin/deck | 1 + config/test.js | 1 + data/vendor.json | 3 +++ test/app/app.js | 5 +++-- test/app/core/layout/module.js | 27 ++++++++++++++++++++++++--- test/app/core/layout/view.js | 2 +- 11 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 data/vendor.json diff --git a/.jshintrc b/.jshintrc index cf5f27d..6bfc86a 100644 --- a/.jshintrc +++ b/.jshintrc @@ -32,6 +32,8 @@ "indent": 2, "globals": { + "afterEach": true, + "beforeEach": true, "define": true, "describe": true, "expect": true, diff --git a/assets/app/app.js b/assets/app/app.js index 2d2800d..a952311 100644 --- a/assets/app/app.js +++ b/assets/app/app.js @@ -1,7 +1,8 @@ define([ + 'underscore', 'marionette', 'core/layout/module' -], function AppDefine(Marionette, Layout) { +], function AppDefine(_, Marionette, Layout) { 'use strict'; var Application = Marionette.Application; @@ -14,10 +15,17 @@ define([ } App.prototype = new Application(); + _.extend(App.prototype, { + Layout: Layout, - App.prototype.initialize = function initialize() { - this.module('layout', Layout); - }; + initialize: function initialize(config) { + config = config || {}; + if (config.Layout) { + this.Layout = config.Layout; + } + this.module('layout', this.Layout); + } + }); return App; }); diff --git a/assets/app/core/layout/model.js b/assets/app/core/layout/model.js index 7cad8ff..60bbb82 100644 --- a/assets/app/core/layout/model.js +++ b/assets/app/core/layout/model.js @@ -1,4 +1,4 @@ -define(['backbone'], function LayoutModelDefine(Backbone) { +define(['underscore', 'backbone'], function LayoutModelDefine(_, Backbone) { 'use strict'; var Model = Backbone.Model; @@ -6,13 +6,17 @@ define(['backbone'], function LayoutModelDefine(Backbone) { function LayoutModel() { Model.apply(this, arguments); } + LayoutModel.prototype = new Model(); - LayoutModel.prototype.defaults = function defaults() { - return { - title: 'some title' - }; - }; + _.extend(LayoutModel.prototype, { + url: '/data/vendor.json', + defaults: function defaults() { + return { + title: '' + }; + } + }); return LayoutModel; }); diff --git a/assets/app/core/layout/module.js b/assets/app/core/layout/module.js index 8c14a35..a3484e1 100644 --- a/assets/app/core/layout/module.js +++ b/assets/app/core/layout/module.js @@ -18,6 +18,7 @@ define([ this.model = new this.Model(config.model); config.view.model = this.model; this.view = new this.View(config.view); + this.model.fetch(); this.view.render(); } }); diff --git a/assets/app/core/layout/view.js b/assets/app/core/layout/view.js index b6dc384..19ecece 100644 --- a/assets/app/core/layout/view.js +++ b/assets/app/core/layout/view.js @@ -5,7 +5,11 @@ define([ 'use strict'; var CoreLayout = Marionette.LayoutView.extend({ - template: template + template: template, + + modelEvents: { + change: 'render' + } }); return CoreLayout; diff --git a/bin/deck b/bin/deck index ecc985a..36230ed 100755 --- a/bin/deck +++ b/bin/deck @@ -24,6 +24,7 @@ app.get('/', function index(req, res) { fs.readFile(indexFile, {encoding: 'utf8'}, compileServe); }); +app.use('/data', staticHandler('data')); app.use('/vendor', staticHandler('vendor')); app.use('/static', staticHandler('static')); diff --git a/config/test.js b/config/test.js index ac6445a..a0d438c 100644 --- a/config/test.js +++ b/config/test.js @@ -15,6 +15,7 @@ module.exports = function(config) { {pattern: 'test/**/!(test-main).js', included: false}, {pattern: 'static/**/!(config).js', included: false}, {pattern: 'static/**/*.map', included: false}, + {pattern: 'data/**/*.json', included: false}, 'static/style/main.css', 'static/app/config.js', 'test/test-main.js' diff --git a/data/vendor.json b/data/vendor.json new file mode 100644 index 0000000..6e99590 --- /dev/null +++ b/data/vendor.json @@ -0,0 +1,3 @@ +{ + "title": "from server" +} diff --git a/test/app/app.js b/test/app/app.js index 31f39dc..e2fb6a2 100644 --- a/test/app/app.js +++ b/test/app/app.js @@ -1,4 +1,4 @@ -define(['app'], function (App) { +define(['marionette', 'app'], function (Marionette, App) { describe('App', function () { it('→ exits', function () { expect(new App()).not.toBeUndefined(); @@ -7,7 +7,8 @@ define(['app'], function (App) { describe('→ start()', function () { it('→ can be called', function () { expect(function noError() { - (new App()).start(); + var app = new App({Layout: Marionette.Module}); + app.start(); }).not.toThrow(); }); }); diff --git a/test/app/core/layout/module.js b/test/app/core/layout/module.js index 5df8fb6..0fb352d 100644 --- a/test/app/core/layout/module.js +++ b/test/app/core/layout/module.js @@ -1,7 +1,15 @@ define([ + 'backbone', 'marionette', 'core/layout/module' -], function (Marionette, LayoutModule) { +], function (Backbone, Marionette, LayoutModule) { + + function MockModel() { + Backbone.Model.apply(this, arguments); + } + MockModel.prototype = new Backbone.Model(); + MockModel.prototype.sync = function () {}; + describe('LayoutModule', function () { it('→ exits', function () { var app = new Marionette.Application(); @@ -10,13 +18,26 @@ define([ }); describe('→ onStart', function () { + var app = null; + var module = null; + + beforeEach(function () { + app = new Marionette.Application(); + module = new LayoutModule('layout', app, {}); + module.Model = MockModel; + }); + 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(); }); + + it('→ will fetch model', function () { + spyOn(module.Model.prototype, 'fetch'); + module.onStart(); + expect(module.Model.prototype.fetch).toHaveBeenCalled(); + }); }); }); }); diff --git a/test/app/core/layout/view.js b/test/app/core/layout/view.js index 996a539..0b83e75 100644 --- a/test/app/core/layout/view.js +++ b/test/app/core/layout/view.js @@ -1,5 +1,5 @@ define(['core/layout/view'], function (LayoutView) { - describe('→ LayoutView', function () { + describe('LayoutView', function () { it('→ exists', function () { expect(new LayoutView()).not.toBeUndefined(); });