Skip to content

Instantly share code, notes, and snippets.

@amin007
Created December 7, 2013 12:41
Show Gist options
  • Save amin007/7840738 to your computer and use it in GitHub Desktop.
Save amin007/7840738 to your computer and use it in GitHub Desktop.
define(['jquery', 'underscore', 'backbone', 'text!templates/category/categoryIndexTemplate.html'], function($, _, Backbone, categoryIndexTemplate) {
var CategoryModel = Backbone.Model.extend({});
var CategoryCollection = Backbone.Collection.extend({
model: CategoryModel,
url: "https://izify.com/api/izify-api/admin/get_all_categories.php?merchantId=" + localStorage.merchantId,
parse: function(data) {
// Return people object which is the array from response
return data.tbl_category;
}
});
var CategoryIndexView = Backbone.View.extend({
el: $("#page"),
initialize: function() {
this.$el.off();
},
events: {
"click #addCategoryButton": "addCategory",
"click #editCategoryButton": "editCategory",
"click #deleteCategoryButton": "deleteCategory"
},
listCategory: function() {
var that = this;
this.collection = new CategoryCollection();
this.collection.fetch({
success: function(collection, response) {
var template = _.template(categoryIndexTemplate, {
categories: that.collection.models
});
that.$el.html(template);
},
error: function(collection, response) {
window.location.replace("https://izify.com/#login");
}
});
},
addCategory: function(event) {
event.preventDefault();
var that = this;
var url = "https://izify.com/api/izify-api/admin/create_category.php";
this.collection = new CategoryCollection();
var formValues = {
merchantId: localStorage.merchantId,
name: $("#name").val()
};
if ($("#name").val() == "") {
alert("Enter category name");
} else {
$.ajax({
url: url,
type: 'GET',
dataType: "json",
data: formValues,
success: function() {
alert("Category successfully created.");
location.reload();
},
error: function() {
window.location.replace("https://izify.com/#login");
}
});
}
},
editCategory: function(event) {
event.preventDefault();
var that = this;
var ID = $(event.currentTarget).data('id');
var url = "https://izify.com/api/izify-api/admin/update_category.php";
var name = "";
name = $("#name_input_" + ID).val();
var formValues = {
categoryId: ID,
merchantId: localStorage.merchantId,
name: name
};
if (name != "") {
$.ajax({
type: "GET",
url: url,
data: formValues,
cache: false,
success: function(html) {
alert("Category name successfully updated.");
},
error: function() {
window.location.replace("https://izify.com/#login");
}
});
} else {
alert('Enter something.');
}
},
deleteCategory: function(event) {
event.preventDefault();
var that = this;
var r = confirm("Are you sure to delete?");
if (r == true) {
var ID = $(event.currentTarget).data('id');
var url = "https://izify.com/api/izify-api/admin/delete_category.php";
var formValues = {
categoryId: ID,
merchantId: localStorage.merchantId
};
$.ajax({
type: "GET",
url: url,
data: formValues,
cache: false,
success: function(html) {
alert("Category name successfully deleted.");
location.reload();
},
error: function() {
window.location.replace("https://izify.com/#login");
}
});
}
},
render: function() {
this.listCategory();
}
});
return CategoryIndexView;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment