Skip to content

Instantly share code, notes, and snippets.

@gajus
Created October 1, 2014 19:19
Show Gist options
  • Save gajus/646f612ff7eda115ed06 to your computer and use it in GitHub Desktop.
Save gajus/646f612ff7eda115ed06 to your computer and use it in GitHub Desktop.
angular.module('storeAdmin')
.controller('main', function ($scope) {
$scope.screens = ['Product', 'Order'];
$scope.currentScreen = $scope.screens[0];
$scope.setScreen = function (index) {
$scope.currentScreen = $scope.screens[index];
};
$scope.getScreenView = function () {
return './static/js/views/admin' + $scope.currentScreen + '.html';
};
})
.constant('orderUrl', 'https://api.parse.com/1/classes/Order/')
.controller('orders', function ($scope, $http, orderUrl) {
$scope.data = {};
$scope.selectedOrder;
$scope.selectOrder = function (order) {
$scope.selectedOrder = order;
};
$http
.get(orderUrl, {
headers: {
'X-Parse-Application-Id': 'NBLhjbKwI3xcPHyygU0hF2gLFaIVzeIGH9lv1q1E',
'X-Parse-REST-API-Key': 'drEzPQCr3rJGZ4i0nk7l7Bq68onUDGqwJWdpHa4n'
}
})
.success(function (data) {
$scope.data.orders = data.results;
})
.error(function (error) {
if (!error) {
error = {status: '000'}
}
$scope.data.error = error;
});
})
.constant('productUrl', 'https://api.parse.com/1/classes/Product/')
.config(function ($httpProvider) {
$httpProvider.defaults.headers.common = {
'X-Parse-Application-Id': 'NBLhjbKwI3xcPHyygU0hF2gLFaIVzeIGH9lv1q1E',
'X-Parse-REST-API-Key': 'drEzPQCr3rJGZ4i0nk7l7Bq68onUDGqwJWdpHa4n'
};
})
.controller('products', function ($scope, $resource, productUrl) {
$scope.productResource = $resource(productUrl + ':id', {id: '@id'}, {
'query': {
//isArray: false,
transformResponse: function (data) {
return data.results;
}
}
});
$scope.listProducts = function () {
$scope.products = $scope.productResource.query();
//console.log($scope.products);
};
$scope.deleteProduct = function (product) {
product.$delete().then(function () {
$scope.products.splice($scope.products.indexOf(product), 1);
});
};
$scope.createProduct = function (product) {
new $scope.productResource(product).$save.then(function (newProduct) {
$scope.products.push(newProduct);
$scope.editedProduct = null;
});
};
$scope.updateProduct = function (product) {
product.$save();
$scope.editedProduct = null;
};
$scope.startEdit = function (product) {
$scope.editedProduct = product;
}
$scope.cancelEdit = function () {
$scope.editedProduct = null;
}
$scope.listProducts();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment