This commit is contained in:
Ritchie Martori
2011-11-26 14:43:56 -08:00
6 changed files with 124 additions and 17 deletions

View File

@@ -66,19 +66,34 @@
<h6><%= name %></h6>
<p>Fill out the fields below to create/edit an object in the <%= name %> collection.</p>
<form class="item-edit-form nice">
<% _.each(description, function (val, key, obj) {
var type = 'text'; %>
<% _.each(description, function (val, key, obj) { %>
<fieldset>
<label for="item-val-<%= key %>"><%= key %></label>
<input
id="item-val-<%= key %>"
name="<%= key %>"
type="<%= type %>"
<% if (val.required) print('required="true"');
if (typeof values !== "undefined" && typeof values[key] !== "undefined") print('value='+values[key]); %> />
<label for="item-val-<%= key %>"><%= key %></label><% if (val.type === 'object') print('requires valid JSON'); %>
<% if (val.formType === 'textarea' || val.formType === 'multi-select') { %>
<textarea
id="item-val-<%= key %>"
name="<%= key %>"><% print(values ? values[key] : ''); %></textarea>
<% }
else if (val.formType === 'checkbox') {%>
<input
type="checkbox"
id="item-val-<%= key %>"
name="<%= key %>"
<% print(val.required ? 'required="true"' : '');
print(values[key] === 'true' ? 'checked' : ''); %> />
<% }
else { %>
<input
type="<% print(val.formType || 'text'); %>"
id="item-val-<%= key %>"
name="<%= key %>"
value="<% print(values && typeof values[key] !== 'undefined' ? values[key] : ''); %>"
<% if (val.required) print('required="true"'); %> />
<% } %>
</fieldset>
<% }); %>
</form>
<div class="alert-box" style="display:none"></div>
<a class="nice medium radius blue button save-item">Save Changes</a>
<a class="nice medium radius white button discard-changes">Discard Changes</a>
</script>
@@ -160,7 +175,7 @@
<input type="checkbox" id="unique<%= key%>" style="display: none;">
<span class="custom checkbox <% if (typeof unique !== "undefined") { print("checked"); } %>"></span> unique in this collection
</label>
<a class="nice small radius red button delete-property" style="float: right;">Remove</a>
<a class="delete-property" style="float: right;">Remove</a>
</script>
<script id="schema-view-template" type="html/template">
<div class="twelve columns">

View File

@@ -5,8 +5,6 @@ window.CollectionModel = Backbone.Model.extend({
},
getItemById: function (id) {
var _items = this.get('results');
console.log('_items');
console.log(_items);
var _item;
_.each(this.get('results'), function (item, index, list){
if (item._id === id) {

View File

@@ -7,6 +7,15 @@ window.ItemEditModel = Backbone.Model.extend({
console.log(response);
},
sync: function (method, model, options) {
d(this.url(), this.get('values'), this.onSync);
var _self = this;
d(this.url(), this.get('values'), function onSync (response) {
if (response.errors) {
_self.trigger('sync-error', { errors: response.errors});
}
else {
_self.trigger('sync-success');
}
});
}
});

View File

@@ -9,7 +9,35 @@ window.CollectionView = Backbone.View.extend({
console.log('editItem:'+$(e.currentTarget).attr("id"));
//TODO: Implement auto-saving model.
var values = this.model.getItemById($(e.currentTarget).attr("id").replace('edit-item-',''));
console.log(values);
var description = this.model.get('description');
_.each(description, function (val, key, obj) {
obj[key] = typeof val === "object" ? val : { type: val};
switch (obj[key].type) {
case "email":
type = "email";
break;
case "password":
type = 'password';
break;
case "object":
type = 'textarea';
if (typeof values[key] === 'object') values[key] = JSON.stringify(values[key]);
values[key].replace(/ /g,'');
break;
case 'boolean':
type = 'checkbox';
break;
case 'mutli-select':
type = 'multi-select';
break;
default:
type = "text";
}
obj[key].formType = type;
});
var _itemModel = new ItemEditModel({
description: this.model.get('description'), //schema definition
name: this.model.get('name'),
@@ -31,8 +59,35 @@ window.CollectionView = Backbone.View.extend({
},
createItem: function () {
//TODO: Dynamically create a form.
var description = this.model.get('description');
_.each(description, function (val, key, obj) {
obj[key] = typeof val === "object" ? val : { type: val};
switch (obj[key].type) {
case "email":
type = "email";
break;
case "password":
type = 'password';
break;
case "object":
type = 'textarea';
break;
case 'boolean':
type = 'checkbox';
break;
case 'mutli-select':
type = 'multi-select';
break;
default:
type = "text";
}
obj[key].formType = type;
console.log(JSON.stringify(obj));
});
var _newItemModel = new ItemEditModel({
description: this.model.get('description'), //schema definition
description: description, //schema definition
name: this.model.get('name'),
plugin: this.model.get('plugin'),
values: {}

View File

@@ -4,12 +4,27 @@ window.ItemEditView = Backbone.View.extend({
'click .save-item' : 'save',
'click .discard-changes' : 'discard'
},
initialize: function () {
var _self = this;
this.model.bind('sync-success', function (e) {
_self._onSync(e);
});
this.model.bind('sync-error', function (e) {
_self._onSyncError(e);
});
},
discard : function (e) {
this._closeModal();
},
_closeModal : function () {
$('.close-reveal-modal').click();
},
_onSync: function (e) {
$('.alert-box', this.el).empty().attr('class','alert-box').addClass('success').html('Object saved successfully').show();
},
_onSyncError: function (e) {
$('.alert-box', this.el).empty().attr('class','alert-box').addClass('error').html('Error saving object:'+JSON.stringify(e.errors)).show();
},
_formObjectFromArray: function (formArray) {
var _formObject = {};
_.each(formArray, function (item, index, list) {
@@ -18,7 +33,22 @@ window.ItemEditView = Backbone.View.extend({
return _formObject;
},
save: function (e) {
var _self = this;
$.each($('form', this.el).find('textarea'), function (index, object) {
$(object).val($(object).val().replace(/\n/g,'').replace(/\r/g,''));
});
var _values = this._formObjectFromArray($('form', this.el).serializeArray());
$.each(_values, function(i, field){
if (_self.model.get('description')[i].type === 'object') {
try {
_values[i] = JSON.parse(field);
}
catch (e) {
if (console) console.log('Couldn\'t parse object');
}
}
});
this.model.save({values: _values});
},
render: function () {

View File

@@ -47,8 +47,8 @@ window.PluginView = Backbone.View.extend({
$('#tab-link-'+this.tabId+' > a').addClass('active');
}
else {
$('#plugin-tabs-nav > dd > a').addClass('active');
$('.tabs-content > li').show();
$('#plugin-tabs-nav > dd > a:first').addClass('active');
$('.tabs-content > li:first').show();
}
},
addTab: function (tabName, id) {