diff --git a/applications/default/extensions/form/form.js b/applications/default/extensions/form/form.js index ebdca24..d8d15d6 100644 --- a/applications/default/extensions/form/form.js +++ b/applications/default/extensions/form/form.js @@ -185,7 +185,6 @@ form.form = function(forms, callback) { name: 'submit', title: 'Save', type: 'submit', - url: '/rest/' + (typeSettings.mainTypeName || typeName), classes: ['btn-primary'], weight: 50 }); diff --git a/applications/default/extensions/user/forms/edit-account.form.json b/applications/default/extensions/user/forms/edit-account.form.json index 5051a36..2f93ffd 100644 --- a/applications/default/extensions/user/forms/edit-account.form.json +++ b/applications/default/extensions/user/forms/edit-account.form.json @@ -13,7 +13,7 @@ "name": "submit", "title": "Save", "type": "submit", - "url": "/settings/edit-account-submit", + "url": "/settings/edit-account-submit/[:username|user]", "weight": 15 } ] diff --git a/applications/default/public/index.html b/applications/default/public/index.html index c68bb11..b761ee4 100644 --- a/applications/default/public/index.html +++ b/applications/default/public/index.html @@ -44,6 +44,8 @@ + + diff --git a/applications/default/public/js/app.js b/applications/default/public/js/app.js index 207e76c..520df55 100644 --- a/applications/default/public/js/app.js +++ b/applications/default/public/js/app.js @@ -10,7 +10,8 @@ angular.module('choko', [ 'ngResource', 'ngSanitize', 'summernote', - 'angularFileUpload' + 'angularFileUpload', + 'restangular' ]) .config(['$locationProvider', function($locationProvider) { @@ -18,4 +19,34 @@ angular.module('choko', [ enabled: true, requireBase: false }); +}]) + +.config(['RestangularProvider', function(RestangularProvider) { + RestangularProvider.setBaseUrl('/rest'); + + // @todo: We need improve the caching functionality. + // The items list not change after add an ítem, because + // he get the items list from the caché. + RestangularProvider.setDefaultHttpFields({ + cache: false + }); + + // Add a response intereceptor + RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) { + var extractedData; + var temp = []; + + if (operation === 'getList') { + Object.keys(data).forEach(function(name){ + temp.push(data[name]); + }); + + extractedData = temp; + } + else { + extractedData = data; + } + + return extractedData; + }); }]); diff --git a/applications/default/public/js/controllers.js b/applications/default/public/js/controllers.js index b8c49ed..8a3ea77 100644 --- a/applications/default/public/js/controllers.js +++ b/applications/default/public/js/controllers.js @@ -136,8 +136,8 @@ angular.module('choko') }]) - .controller('ViewController', ['$scope', '$location', '$http', 'Choko', 'Params', 'Token', - function ($scope, $location, $http, Choko, Params, Token) { + .controller('ViewController', ['$scope', '$location', '$http', 'Choko', 'Params', 'Token', 'Restangular', + function ($scope, $location, $http, Choko, Params, Token, Restangular) { $scope.prepareDisplay = function(name, callback) { Choko.get({type: 'display', key: name}, function(display) { @@ -155,6 +155,13 @@ angular.module('choko') }); }; + + // Prevente creation of service if no itemType set. + if ($scope.view.itemType) { + // Create a new Service for Itemtype. + var itemTypeREST = Restangular.service($scope.view.itemType); + } + // Parse parameters when needed. if (typeof $scope.view.itemKey !== 'undefined') { $scope.view.itemKey = Params.parse($scope.view.itemKey, $scope); @@ -172,22 +179,26 @@ angular.module('choko') // Handle 'list' type views. if ($scope.view.type === 'list' && $scope.view.itemType) { - var query = { - type: $scope.view.itemType - }; + var query = {}; if ($scope.view.query) { angular.extend(query, $scope.view.query); } - $scope.items = {}; - if ($scope.view.template) { Choko.get(query, function(response) { $scope.items = response; }); } + // Expose view list promise to scope + $scope.viewList = itemTypeREST.getList(query); + $scope.items = {}; + + $scope.viewList.then(function(response) { + $scope.items = response; + }); + if (!$scope.view.template && $scope.view.listStyle) { $scope.view.template = '/templates/' + $scope.view.listStyle + '.html'; } @@ -203,13 +214,16 @@ angular.module('choko') // Handle 'item' type views. if ($scope.view.type === 'item' && $scope.view.itemType) { + + // Expose variables to the scope $scope.data = {}; $scope.view.title = ''; - Choko.get({type: $scope.view.itemType, key: $scope.view.itemKey}, function(response) { + $scope.viewItem = itemTypeREST.one($scope.view.itemKey).get(); + + $scope.viewItem.then(function(response) { $scope.data = response; $scope.view.title = response.title; - }, - function(response) { + }, function(response) { // Error. if ($scope.page) { // If it's a page, show error, otherwise fail silently. @@ -222,7 +236,11 @@ angular.module('choko') // Handle 'form' type views. if ($scope.view.type === 'form' && $scope.view.formName) { + var typeForm = 'post'; + var itemREST = null; + $scope.data = {}; + $scope.buildForm = function () { Choko.get({type: 'form', key: $scope.view.formName}, function(response) { $scope.form = response; @@ -242,41 +260,64 @@ angular.module('choko') }; if ($scope.view.itemType && $scope.view.itemKey) { - // Load item for editing. - Choko.get({type: $scope.view.itemType, key: $scope.view.itemKey}, function(response) { - $scope.data = response; - $scope.buildForm(); - }); + + // Set type form to PUT. + typeForm = 'put'; + + itemTypeREST.one($scope.view.itemKey) + .get() + .then(function(response) { + $scope.data = response; + $scope.buildForm(); + }); } - else { + + // Verify if the form is the type PUT to build the form + if (typeForm != 'put') { $scope.buildForm(); } $scope.submit = function(url) { - // Add itemKey to the URL if any. - if ($scope.view.itemKey) { - url += '/' + $scope.view.itemKey; + + // Replace tokens in url. + if (url) { + url = Token.replace(url, $scope); } - $http.post(url, $scope.data) - .success(function(data, status, headers, config) { - $scope.data = data; + // Add params to data if any. + Object.keys($scope.view.params || {}).forEach(function(param) { + $scope.data[param] = $scope.data[param] || $scope.view.params[param]; + }); - delete $scope.errors; + if(!itemTypeREST) { + $scope.viewForm = Restangular.oneUrl('url', url).post('', $scope.data); + } + else { + if (url) { + $scope.viewForm = Restangular.oneUrl('url', url).post('', $scope.data); + } else { + $scope.viewForm = typeForm === 'post' ? + itemTypeREST.post($scope.data) : + $scope.data.put(); + } + } - if ($scope.form.redirect) { - // Replace tokens in redirects. Make 'item' an alias to 'data' - // so item parser can be used in tokens. - $scope.item = $scope.data; - $scope.form.redirect = Token.replace($scope.form.redirect, $scope); + $scope.viewForm.then(function(response) { + $scope.data = response; + delete $scope.errors; - $location.path($scope.form.redirect); - } - }) - .error(function(data, status, headers, config) { - $scope.status = status; - $scope.errors = data; - }); - }; + if ($scope.form.redirect) { + // Replace tokens in redirects. Make 'item' an alias to 'data' + // so item parser can be used in tokens. + $scope.item = $scope.data; + $scope.form.redirect = Token.replace($scope.form.redirect, $scope); + + $location.path($scope.form.redirect); + } + }, function(response) { + $scope.status = response.status; + $scope.errors = response.data; + }); + } } }]); diff --git a/bower.json b/bower.json index 517c0ec..81564f0 100644 --- a/bower.json +++ b/bower.json @@ -11,6 +11,7 @@ "angular-sanitize": "1.3.x", "summernote": "0.6.x", "angular-summernote": "0.3.x", - "ng-file-upload": "1.6.x" + "ng-file-upload": "1.6.x", + "restangular": "1.4.x" } }