Skip to content

Commit

Permalink
working on autosave.
Browse files Browse the repository at this point in the history
  • Loading branch information
diegorubin committed Mar 28, 2015
1 parent dbe3e73 commit bcc706f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
63 changes: 63 additions & 0 deletions app/assets/javascripts/admin/forms/base.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
var BaseForm = function() {
}

BaseForm.prototype.refresh = function(resource) {
}

BaseForm.prototype.save = function() {
var _this = this;

$.ajax({
type: options['method'] || 'POST',
dataType: options['type'] || 'json',
data: options['data'] || {},
url: _this.url,

success: function(data, textStatus, xhr) {
if(self.refresh) self.refresh(data);
},

error: function(data) {
location.reload();
}
});
}

BaseForm.prototype.loadFields = function() {
Expand Down Expand Up @@ -39,6 +58,50 @@ BaseForm.prototype.loadCodeMirror = function() {
});
}

BaseForm.prototype.loadAttributes = function() {

var attrs = {};
var field_types = ["input", "select", "textarea", "hidden"];

for(var j = 0; j < field_types.length; j++){
var inputs = form.find(field_types[j]);

for(var i = 0; i < inputs.length; i++) {
var input = $(inputs[i]);
attrs[input.attr("name")] = input.val();
}

}

/* get values of radio and checkbox */
var radios = $("input[type='radio']:checked");

for(var i = 0; i < radios.length; i++) {
var name = $(radios[i]).attr('name');
attrs[name] = $(radios[i]).val();
}

var checkboxes = $("input[type='checkbox']");
for(var i = 0; i < checkboxes.length; i++) {
var name = $(checkboxes[i]).attr('name') || '';

if(name.match(/\[\]$/)) {

if((typeof attrs[name]) == 'string') {attrs[name] = [];}
if($(checkboxes[i]).is(":checked"))
attrs[name].push($(checkboxes[i]).val());

} else {

attrs[name] =
($(checkboxes[i]).is(":checked") ? $(checkboxes[i]).val() : '');

}
}

return attrs;
}

// form functions utils
function loadForm(selector, callback) {
if($(selector).length > 0) {
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/admin/forms/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ PostForm.prototype.constructor = PostForm;
PostForm.prototype.init = function(form) {
var _this = this;

_this.api = '/admin/api/v1/posts';
_this.content = {};
_this.form = $(form);

Expand Down

0 comments on commit bcc706f

Please sign in to comment.