Get model from collection in app

master
Buddy Sandidge 10 years ago
parent 5325e50274
commit c3734b88f2

@ -21,6 +21,7 @@ define([
var Application = Marionette.Application; var Application = Marionette.Application;
var SlideCollection = Backbone.Collection.extend({ var SlideCollection = Backbone.Collection.extend({
model: SlideModel,
url: '/slides' url: '/slides'
}); });
@ -34,6 +35,10 @@ define([
App.prototype = new Application(); App.prototype = new Application();
_.extend(App.prototype, { _.extend(App.prototype, {
model: null,
view: null,
currentSlide: null,
initialize: function initialize(options) { initialize: function initialize(options) {
options = options || {}; options = options || {};
@ -41,6 +46,7 @@ define([
this.module('listener', Listener); this.module('listener', Listener);
this.currentSlide = 0;
this.model = new SlideModel(); this.model = new SlideModel();
this.view = new SlideView({el: options.el, model: this.model}); 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.listenTo(this.router.model, 'change:slide', function (model, num) {
this.triggerMethod('slide:change', num); this.triggerMethod('slide:change', num);
}, this); }, this);
this.listenTo(this.collection, 'sync', function () {
this.onSlideChange(this.currentSlide);
}, this);
}, },
goToSlide: function goToSlide(nextId) { goToSlide: function goToSlide(nextId) {
@ -71,8 +81,15 @@ define([
onSlideChange: function onSlideChange(num) { onSlideChange: function onSlideChange(num) {
$('body').removeClass().addClass('page-' + num); $('body').removeClass().addClass('page-' + num);
this.model.set('id', num); var model = this.collection.get(num);
this.model.fetch(); var view = this.view;
if (model) {
model.fetch();
this.model = model;
view.model = model;
view.render();
}
this.currentSlide = num;
} }
}); });

@ -9,8 +9,22 @@ define(['underscore', 'backbone'], function SlideModelDefine(_, Backbone) {
SlideModel.prototype = new Model(); SlideModel.prototype = new Model();
_.extend(SlideModel.prototype, { _.extend(SlideModel.prototype, {
defaults: function defaults() {
return {
copy: '',
slug: ''
};
},
url: function url() { url: function url() {
return '/slide/' + this.get('id'); return '/slide/' + this.get('id');
},
fetch: function fetch() {
if (this.get('copy') !== '') {
return;
}
return Model.prototype.fetch.apply(this, arguments);
} }
}); });

@ -74,7 +74,7 @@ app.get('/slides', function showHandler(req, res) {
} }
res.send(_.map(files, function (file, index) { 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) { if (err) {
return renderError(res, err); return renderError(res, err);
} }
res.send({id: toInt(slideIndex), copy: content}); res.send({
copy: content,
id: toInt(slideIndex),
slug: markdownFile.replace(/\.md$/, '')
});
}); });
}); });
}); });

Loading…
Cancel
Save