Commit 9e5df7e2 by David Baumgold

Integrated PDF textbooks with Backbone Associations

parent 00f76f3c
...@@ -24,21 +24,27 @@ ...@@ -24,21 +24,27 @@
</%block> </%block>
<%block name="jsextra"> <%block name="jsextra">
<script type="text/javascript" src="${static.url('js/vendor/backbone-associations-min.js')}"></script>
<script type="text/javascript"> <script type="text/javascript">
window.UPLOAD_ASSET_CALLBACK_URL = "${upload_asset_callback_url}" window.UPLOAD_ASSET_CALLBACK_URL = "${upload_asset_callback_url}"
window.ASSET_INDEX_URL = "${asset_index_url}" window.ASSET_INDEX_URL = "${asset_index_url}"
CMS.Models.Textbook = Backbone.Model.extend({ CMS.Models.Textbook = Backbone.AssociatedModel.extend({
defaults: { defaults: function() {
name: "", return {
showChapters: false name: "",
}, chapters: new CMS.Collections.ChapterSet([{}]),
initialize: function() { showChapters: false
this.chapters = new CMS.Collections.ChapterSet; };
this.chapters.add([{}]); },
}, relations: [{
type: Backbone.Many,
key: "chapters",
relatedModel: "CMS.Models.Chapter",
collectionType: "CMS.Collections.ChapterSet"
}],
isEmpty: function() { isEmpty: function() {
return !this.get('name'); return !this.get('name') && this.get('chapters').isEmpty();
} }
}) })
CMS.Views.TextbookShow = Backbone.View.extend({ CMS.Views.TextbookShow = Backbone.View.extend({
...@@ -138,8 +144,7 @@ CMS.Views.TextbookEdit = Backbone.View.extend({ ...@@ -138,8 +144,7 @@ CMS.Views.TextbookEdit = Backbone.View.extend({
delete textbooks.editing; delete textbooks.editing;
this.remove(); this.remove();
// if the textbook has no content, remove it from the collection // if the textbook has no content, remove it from the collection
var chapterIsEmpty = function(chapter) { return chapter.isEmpty(); } if(this.model.isEmpty()) {
if(this.model.isEmpty() && this.model.chapters.every(chapterIsEmpty)) {
textbooks.remove(this.model); textbooks.remove(this.model);
} }
textbooks.trigger('render'); textbooks.trigger('render');
...@@ -192,7 +197,7 @@ CMS.Views.ListTextbooks = Backbone.View.extend({ ...@@ -192,7 +197,7 @@ CMS.Views.ListTextbooks = Backbone.View.extend({
this.collection.trigger("editOne", m); this.collection.trigger("editOne", m);
} }
}) })
CMS.Models.Chapter = Backbone.Model.extend({ CMS.Models.Chapter = Backbone.AssociatedModel.extend({
defaults: function() { defaults: function() {
return { return {
name: "", name: "",
...@@ -210,6 +215,9 @@ CMS.Collections.ChapterSet = Backbone.Collection.extend({ ...@@ -210,6 +215,9 @@ CMS.Collections.ChapterSet = Backbone.Collection.extend({
nextOrder: function() { nextOrder: function() {
if(!this.length) return 1; if(!this.length) return 1;
return this.last().get('order') + 1; return this.last().get('order') + 1;
},
isEmpty: function() {
return this.length === 0 || this.every(function(m) { return m.isEmpty(); });
} }
}) })
CMS.Views.ChapterEdit = Backbone.View.extend({ CMS.Views.ChapterEdit = Backbone.View.extend({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment