Beginnings of Backbone views/models.

This commit is contained in:
cahrens
2013-05-09 11:16:12 -04:00
parent 9e1c935b60
commit bfdbcc2f32
11 changed files with 138 additions and 66 deletions

View File

@@ -0,0 +1,2 @@
<label><%= model.get('display_name') %>:</label>
<input type='text' class='editor' value='<%= model.get("value") %>' size='60'/>

View File

@@ -0,0 +1,7 @@
<ul>
<% _.each(metadata_entries, function(entry) { %>
<li>
<div class='metadata_entry' />
</li>
<% }) %>
</ul>

View File

@@ -20,29 +20,15 @@ class CMS.Views.ModuleEdit extends Backbone.View
loadEdit: ->
if not @module
@module = XModule.loadModule(@$el.find('.xmodule_edit'))
@originalMetadata = @metadata()
metadata: ->
# cdodge: package up metadata which is separated into a number of input fields
# there's probably a better way to do this, but at least this lets me continue to move onwards
_metadata = {}
$metadata = @$component_editor().find('.metadata_edit')
if $metadata
# walk through the set of elments which have the 'xmetadata_name' attribute and
# build up a object to pass back to the server on the subsequent POST
_metadata[$(el).data("metadata-name")] = el.value for el in $('[data-metadata-name]', $metadata)
return _metadata
# At this point, metadata-edit.html will be loaded, and the metadata (as JSON) is available.
metadataEditor = @$el.find('.metadata_edit')
@metadataEditor = new CMS.Views.Metadata.Editor({
el: metadataEditor,
model: new CMS.Models.MetadataEditor(metadataEditor.data('metadata'))
});
changedMetadata: ->
currentMetadata = @metadata()
changedMetadata = {}
for key of currentMetadata
if currentMetadata[key] != @originalMetadata[key]
changedMetadata[key] = currentMetadata[key]
return changedMetadata
return @metadataEditor.getModifiedMetadataValues()
cloneTemplate: (parent, template) ->
$.post("/clone_item", {

View File

@@ -0,0 +1,8 @@
CMS.Models.MetadataEditor = Backbone.Model.extend({
// This model class is not suited for restful operations and is considered just a server side initialized container
url: '',
defaults: {
}
});

View File

@@ -0,0 +1,13 @@
CMS.Models.Metadata = Backbone.Model.extend({
// This model class is not suited for restful operations and is considered just a server side initialized container
url: '',
defaults: {
"display_name": null,
"value" : null
},
getOriginalValue: function() {
return this.get('value');
}
});

View File

@@ -0,0 +1,28 @@
if (!CMS.Views['Metadata']) CMS.Views.Metadata = {};
CMS.Views.Metadata.Generic = Backbone.View.extend({
// Model class ...
events : {
},
initialize : function() {
var self = this;
// instantiates an editor template for each update in the collection
window.templateLoader.loadRemoteTemplate("metadata_entry",
"/static/client_templates/generic_metadata_entry.html",
function (raw_template) {
self.template = _.template(raw_template);
self.$el.append(self.template({model: self.model}));
}
);
},
modified: function () {
return this.getValue() !== this.model.getOriginalValue();
},
getValue: function() {
return this.$el.find('.editor').val();
}
});

View File

@@ -0,0 +1,44 @@
if (!CMS.Views['Metadata']) CMS.Views.Metadata = {};
CMS.Views.Metadata.Editor = Backbone.View.extend({
// Model class is ...
events : {
},
views : {}, // child views
initialize : function() {
var self = this;
// instantiates an editor template for each update in the collection
window.templateLoader.loadRemoteTemplate("metadata_editor",
"/static/client_templates/metadata_editor.html",
function (raw_template) {
self.template = _.template(raw_template);
self.$el.append(self.template({metadata_entries: self.model.attributes}));
var counter = 0;
_.each(self.model.attributes,
function(item, key) {
self.views[key] = new CMS.Views.Metadata.Generic({
el: self.$el.find('.metadata_entry')[counter],
model: new CMS.Models.Metadata(item)
}
);
counter+=1;
});
}
);
},
getModifiedMetadataValues: function () {
var modified_values = {};
_.each(this.views,
function (item, key) {
if (item.modified()) {
modified_values[key] = item.getValue();
}
}
);
return modified_values;
}
});