From c3734b88f2af91b4a572810652407ae6d8015d90 Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Tue, 28 Apr 2015 01:13:32 -0700 Subject: [PATCH] Get model from collection in app --- assets/app/app.js | 21 +++++++++++++++++++-- assets/app/widgets/slide/model.js | 14 ++++++++++++++ bin/deck | 8 ++++++-- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/assets/app/app.js b/assets/app/app.js index 0f1f249..676176f 100644 --- a/assets/app/app.js +++ b/assets/app/app.js @@ -21,6 +21,7 @@ define([ var Application = Marionette.Application; var SlideCollection = Backbone.Collection.extend({ + model: SlideModel, url: '/slides' }); @@ -34,6 +35,10 @@ define([ App.prototype = new Application(); _.extend(App.prototype, { + model: null, + view: null, + currentSlide: null, + initialize: function initialize(options) { options = options || {}; @@ -41,6 +46,7 @@ define([ this.module('listener', Listener); + this.currentSlide = 0; this.model = new SlideModel(); this.view = new SlideView({el: options.el, model: this.model}); @@ -48,6 +54,10 @@ define([ this.listenTo(this.router.model, 'change:slide', function (model, num) { this.triggerMethod('slide:change', num); }, this); + + this.listenTo(this.collection, 'sync', function () { + this.onSlideChange(this.currentSlide); + }, this); }, goToSlide: function goToSlide(nextId) { @@ -71,8 +81,15 @@ define([ onSlideChange: function onSlideChange(num) { $('body').removeClass().addClass('page-' + num); - this.model.set('id', num); - this.model.fetch(); + var model = this.collection.get(num); + var view = this.view; + if (model) { + model.fetch(); + this.model = model; + view.model = model; + view.render(); + } + this.currentSlide = num; } }); diff --git a/assets/app/widgets/slide/model.js b/assets/app/widgets/slide/model.js index f9789eb..b775725 100644 --- a/assets/app/widgets/slide/model.js +++ b/assets/app/widgets/slide/model.js @@ -9,8 +9,22 @@ define(['underscore', 'backbone'], function SlideModelDefine(_, Backbone) { SlideModel.prototype = new Model(); _.extend(SlideModel.prototype, { + defaults: function defaults() { + return { + copy: '', + slug: '' + }; + }, + url: function url() { return '/slide/' + this.get('id'); + }, + + fetch: function fetch() { + if (this.get('copy') !== '') { + return; + } + return Model.prototype.fetch.apply(this, arguments); } }); diff --git a/bin/deck b/bin/deck index d4277f6..ca7be20 100755 --- a/bin/deck +++ b/bin/deck @@ -74,7 +74,7 @@ app.get('/slides', function showHandler(req, res) { } res.send(_.map(files, function (file, index) { - return {id: index + 1}; + return {id: index + 1, slug: file.replace(/\.md$/, '')}; })); }); }); @@ -101,7 +101,11 @@ app.get(/slide\/(\d+)/, function showHandler(req, res, next) { if (err) { return renderError(res, err); } - res.send({id: toInt(slideIndex), copy: content}); + res.send({ + copy: content, + id: toInt(slideIndex), + slug: markdownFile.replace(/\.md$/, '') + }); }); }); });