1 XOSApplication = Marionette.Application.extend({
2 detailBoxId: "#detailBox",
3 errorBoxId: "#errorBox",
4 errorCloseButtonId: "#close-error-box",
5 successBoxId: "#successBox",
6 successCloseButtonId: "#close-success-box",
7 errorTemplate: "#xos-error-template",
8 successTemplate: "#xos-success-template",
10 hideError: function(result) {
11 $(this.errorBoxId).hide();
12 $(this.successBoxId).hide();
15 showSuccess: function(result) {
16 $(this.successBoxId).show();
17 $(this.successBoxId).html(_.template($(this.successTemplate).html())(result));
18 $(this.successCloseButtonId).unbind().bind('click', function() {
19 $(this.successBoxId).hide();
23 showError: function(result) {
24 $(this.errorBoxId).show();
25 $(this.errorBoxId).html(_.template($(this.errorTemplate).html())(result));
26 $(this.errorCloseButtonId).unbind().bind('click', function() {
27 $(this.errorBoxId).hide();
34 app - MarionetteApplication
35 template - template (See XOSHelper.html)
38 XOSDetailView = Marionette.ItemView.extend({
41 events: {"click button.js-submit": "submitClicked",
42 "change input": "inputChanged"},
44 events: {"click button.js-submit": "submitClicked",
45 "change input": "inputChanged"},
47 /* inputChanged is watching the onChange events of the input controls. We
48 do this to track when this view is 'dirty', so we can throw up a warning
\r
49 if the user tries to change his slices without saving first.
\r
52 inputChanged: function(e) {
\r
56 saveError: function(model, result, xhr) {
\r
57 this.app.showError(result);
\r
60 saveSuccess: function(model, result, xhr) {
\r
61 this.app.showSuccess({status: xhr.xhr.status, statusText: xhr.xhr.statusText});
\r
64 submitClicked: function(e) {
65 this.app.hideError();
\r
67 var data = Backbone.Syphon.serialize(this);
\r
68 var thisView = this;
\r
69 this.model.save(data, {error: function(model, result, xhr) { thisView.saveError(model, result, xhr); },
\r
70 success: function(model, result, xhr) { thisView.saveSuccess(model, result, xhr); }});
\r
74 showLinkedItems: function() {
76 for (relatedName in this.model.collection.relatedCollections) {
\r
77 relatedField = this.model.collection.relatedCollections[relatedName];
\r
79 relatedListViewClassName = relatedName + "ListView";
\r
80 if (this.app[relatedListViewClassName] == undefined) {
\r
81 console.log("warning: " + relatedListViewClassName + " not found");
\r
83 relatedListViewClass = this.app[relatedListViewClassName].extend({collection: xos[relatedName].filterBy(relatedField,this.model.id)});
\r
84 this.app["linkedObjs" + (index+1)].show(new relatedListViewClass());
\r
89 this.app["linkedObjs" + (index+1)].empty();
\r
96 This is for items that will be displayed as table rows.
98 app - MarionetteApplication
99 template - template (See XOSHelper.html)
100 detailClass - class of detail view, probably an XOSDetailView
103 XOSItemView = Marionette.ItemView.extend({
105 className: 'test-tablerow',
107 events: {"click": "changeItem"},
109 changeItem: function(e) {
\r
110 this.app.hideError();
\r
111 e.preventDefault();
\r
112 e.stopPropagation();
\r
114 this.app.navigateToModel(this.app, this.detailClass, this.detailNavLink, this.model);
\r
120 app - MarionetteApplication
121 childView - class of ItemView, probably an XOSItemView
122 template - template (see xosHelper.html)
123 collection - collection that holds these objects
124 title - title to display in template
127 XOSListView = Marionette.CompositeView.extend({
128 childViewContainer: 'tbody',
\r
130 initialize: function() {
\r
131 this.listenTo(this.collection, 'change', this._renderChildren)
133 // Because many of the templates use idToName(), we need to
134 // listen to the collections that hold the names for the ids
135 // that we want to display.
136 for (i in this.collection.foreignCollections) {
137 foreignName = this.collection.foreignCollections[i];
138 if (xos[foreignName] == undefined) {
139 console.log("Failed to find xos class " + foreignName);
141 this.listenTo(xos[foreignName], 'change', this._renderChildren);
142 this.listenTo(xos[foreignName], 'sort', this._renderChildren);
146 templateHelpers: function() {
147 return { title: this.title };
151 /* Give an id, the name of a collection, and the name of a field for models
152 within that collection, lookup the id and return the value of the field.
155 idToName = function(id, collectionName, fieldName) {
156 linkedObject = xos[collectionName].get(id);
157 if (linkedObject == undefined) {
160 return linkedObject.attributes[fieldName];
164 /* Constructs lists of <option> html blocks for items in a collection.
166 selectedId = the id of an object that should be selected, if any
167 collectionName = name of collection
168 fieldName = name of field within models of collection that will be displayed
171 idToOptions = function(selectedId, collectionName, fieldName) {
173 for (index in xos[collectionName].models) {
174 linkedObject = xos[collectionName].models[index];
175 linkedId = linkedObject["id"];
176 linkedName = linkedObject.attributes[fieldName];
177 if (linkedId == selectedId) {
178 selected = " selected";
182 result = result + '<option value="' + linkedId + '"' + selected + '>' + linkedName + '</option>';
187 /* Constructs an html <select> and the <option>s to go with it.
189 variable = variable name to return to form
190 selectedId = the id of an object that should be selected, if any
191 collectionName = name of collection
192 fieldName = name of field within models of collection that will be displayed
195 idToSelect = function(variable, selectedId, collectionName, fieldName) {
196 result = '<select name="' + variable + '">' +
197 idToOptions(selectedId, collectionName, fieldName) +