Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client-side validation for page_url and DOM abstraction #66

Merged
merged 2 commits into from
Jan 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions public/gb-admin/js/layouts/ConfigLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,23 @@ define([
},

save: function () {
var that = this;

this.model.save(that.createNavJSON(that.$navEl), function () {
var that = this,
data = {
site_title: $('#site-title').val(),
site_description: $('#site-description').val(),
nav: this.createNavJSON(that.$navEl),
ga_id: $('#ga-id').val()
};

this.model.save(data, function () {
alert('Configuration data saved!');
that.render();
}, function (xhr) {
console.log('Error saving data. ' + xhr);
});


},

createNavJSON: function ($navEl) {
Expand Down
47 changes: 38 additions & 9 deletions public/gb-admin/js/layouts/PageLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,49 @@ define([

savePage: function () {
var isNewPage = $('.page-to-edit-select').val() === "new_page" ? true : false,
that = this;
that = this,
data = {
page_id: $('#page-id').val(),
page_title: $('#page-title').val(),
page_url: $('#page-url').val(),
page_content: $('#page-content').val(),
meta_description: $('#meta-description').val(),
meta_keywords: $('#meta-keywords').val(),
theme: $('.theme').val()
};

if (data.page_id === "") {
data.page_id = window.btoa(window.unescape(encodeURIComponent(data.page_title))); // HACK: There is a better way to do this.
}

this.model.savePage(function () {
this.model.set(data, {
validate: true
});

alert('Page saved successfully');
var validationError = this.model.validationError;

// Check to see newPage boolean.
if (isNewPage) {
that.render();
if (validationError === null) {
this.model.savePage(data, function () {

alert('Page saved successfully');

// Check to see newPage boolean.
if (isNewPage) {
that.render();
}

}, function (xhr) {
console.log('Page update/creation has failed. Please try again. ' + xhr);
});
} else {
if (validationError === "INVALID") {
alert("You have entered an invalid character ('/') in your page_url. Please try again.");
} else {
alert('There has been an error, please try again.');
}
}


}, function (xhr) {
console.log('Page update/creation has failed. Please try again. ' + xhr);
});
},

deletePage: function () {
Expand Down
9 changes: 2 additions & 7 deletions public/gb-admin/js/models/ConfigModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@ define([

GOB.Models.ConfigModel = Backbone.Model.extend({

save: function (nav, successCallback, failureCallback) {
save: function (data, successCallback, failureCallback) {

$.ajax({
url: "/gb-admin/config-save.json",
type: "POST",
dataType: "json",
data: {
site_title: $('#site-title').val(),
site_description: $('#site-description').val(),
nav: nav,
ga_id: $('#ga-id').val()
},
data: data,
success: function (data) {
successCallback(data);
},
Expand Down
32 changes: 18 additions & 14 deletions public/gb-admin/js/models/PageModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ define([
theme: ""
},

validate: function (attrs) {

var key = null,
value;

for (key in attrs) {
if (attrs.hasOwnProperty(key)) {
value = attrs[key];
if (key === "page_url" && value.indexOf("/") !== -1) {
return "INVALID";
}
}
}

},

switchPage: function (id) {

var that = this;
Expand Down Expand Up @@ -55,25 +71,13 @@ define([

},

savePage: function (successCallback, failureCallback) {

if ($('#page-id').val() === "") {
$('#page-id').val(window.btoa(window.unescape(encodeURIComponent($('#page-title').val())))); // HACK: There is a better way to do this.
}
savePage: function (data, successCallback, failureCallback) {

$.ajax({
url: "/gb-admin/page-save.json",
type: "POST",
dataType: "json",
data: {
page_id: $('#page-id').val(),
page_title: $('#page-title').val(),
page_url: $('#page-url').val(),
page_content: $('#page-content').val(),
meta_description: $('#meta-description').val(),
meta_keywords: $('#meta-keywords').val(),
theme: $('.theme').val()
},
data: data,
success: function (data) {
successCallback(data);
},
Expand Down