/*global Backbone, Marionette, _ */ (function () { 'use strict'; var root = this, PDFFormFiller, PagePreviewView, FieldItemPreviewView, FieldPreviewView, Document, Page, Field, PageCollection, FieldCollection, FieldData, FieldDataCollection, DocumentView; if (typeof exports !== 'undefined') { PDFFormFiller = exports; } else { PDFFormFiller = root.PDFFormFiller = {}; } Field = PDFFormFiller.Field = Backbone.Model.extend({}); FieldCollection = PDFFormFiller.FieldCollection = Backbone.Collection.extend({ model: Field }); FieldData = PDFFormFiller.FieldData = Backbone.Model.extend({}); FieldDataCollection = PDFFormFiller.FieldDataCollection = Backbone.Collection.extend({ model: FieldData }); Page = PDFFormFiller.Page = Backbone.Model.extend({ initialize: function (options) { var fields, self = this; if (typeof this.get('fields') === 'undefined') { fields = new FieldCollection(); fields.url = function () { return self.url + 'fields/'; }; this.set('fields', fields); } }, fetch: function () { var self = this; return Backbone.Model.prototype.fetch.apply(this, arguments).then(function () { var model = self; // Auto-fetch the fields collection if (typeof model.get('fields') !== 'undefined') { return model.get('fields').fetch(); } }); }, }); PageCollection = PDFFormFiller.PageCollection = Backbone.Collection.extend({ model: Page }); Document = PDFFormFiller.Document = Backbone.Model.extend({ initialize: function (options) { console.log(options); if (typeof options.pages === 'undefined') { this.pages = new PageCollection(); this.pages.url = this.url + '../page/'; } }, getPage: function (page_num) { var page_ids = this.get('page_ids'), page_id = page_ids[page_num], page = this.pages.findWhere({id: page_id}), defer = new $.Deferred(); if (typeof page === 'undefined') { page = new Page(); page.url = '/formfiller/api/page/' + page_id + '/'; this.pages.add(page); page.fetch() .then(function () { return defer.resolve(page); }); } else { defer.resolve(page); return defer.promise(); } return defer.promise(); } }); FieldItemPreviewView = PDFFormFiller.FieldItemPreviewView = Marionette.ItemView.extend({ /* template: _.template('