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 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;
}
});

@ -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);
}
});

@ -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$/, '')
});
});
});
});

Loading…
Cancel
Save