Catch oData errors from oTable.bindItems - javascript

I have a table built up in JavaScript thus:
oTable.bindItems({
path: oQuery,
template: this.getFragment("<fragment>"),
filters: aFilter
});
Is there a way to catch the errors coming back from the odata call in the same way when you do an oModel.read you can specify success and error functions?
This reference seems to not mention it: https://sapui5.hana.ondemand.com/#docs/api/symbols/sap.ui.base.ManagedObject.html#bindAggregation
Perhaps there is something I am missing.

We have 2 methods to check for oData Failure:
attachMetadataFailed. (https://openui5.hana.ondemand.com/#docs/api/symbols/sap.ui.model.odata.ODataModel.html#attachMetadataFailed)
attachRequestFailed.
Let's take up option 2 with an example ( as I'm sure you will have a valid oData Service).
Service: http://services.odata.org/Northwind/Northwind.svc/
Note: Employees is a valid Entity Set in the above Northwind service.
I will try to bind my table with a wrong Enity set name such as : MyEmployees.
Now, binding my table to MyEmployees will throw error which we need to catch. Below is the working code:
View:
<Table items = "{/MyEmployees}">
Controller:
var url = "proxy/http/services.odata.org/Northwind/Northwind.svc/";
var oDataModel = new sap.ui.model.odata.ODataModel(url);
oDataModel.attachRequestFailed(function(e) {
console.log('request failed');
});
this.getView().setModel(oDataModel);
Go ahead and try it. Let me know if this helps. :)

Related

AngularJS Delete function REST HTTP://1.1 500 Error

I am using AngularJS to delete a row via a REST API. When I click the link for the function to delete the row, I get a Internal Server Error. I'm asking how to fix the form to get it to delete the row.
//CONTROLLER to delete a specific item
countryApp.controller('DeleteLocation', function($scope, $http, $routeParams, $location) {
//get item info
var id = $routeParams.locid;
$scope.activePath = null;
$http.get('http://localhost/slimtest2/location/'+id).success(function(data) {
$scope.location= data;
});
//add more stuff here
$scope.pedelete = function(id) {
$http.delete('http://localhost/slimtest2/location/1/delete', id);
}
});
View for Deleting a Row
<div ng-controller="DeleteLocation">
<div ng-repeat="l in location.location">
Are you sure you want to delete the location named: {{l.location_title }}
Delete This
</div>
</div>
Internal Server error occurs when something happens wrong at server-side. Please check your server-side code/configuration are proper or not.
A couple of points to check:
1. Is your request method type "get" for a delete request as well or should it be "delete"? For this you need to check which method type your API is expecting. You may want to use $http.delete() instead.
2. Check your browser console/network. Are you seeing the error there? If so, work out on the error to resolve the issue. The error description should be coming what your API is sending.
Hope this helps.

Angular CRUD parsing ID on resource remove

I'm trying to write a function to remove an answer:
// Remove an answer
$scope.removeAnswer = function(answer) {
Answers.remove(answer._id);
};
However, I am throwing the console error:
DELETE http://localhost:9000/api/answers?0=5&1=4&10=7&11=4&12=2&13=c&14=0&15=0&16=0&17=0&18=0&19=0&2=7&20=0&21=0&22=0&23=4&3=3&4=d&5=0&6=1&7=4&8=2&9=d 404 (Not Found)
When I console.log(answer._id), I get a reasonable id like 348374831...
Why is it converting the id to a weird format and how can I fix it?
Here seems to be an identical question, although the answer doesn't really explain the problem: AngularJS resource - encoding url string parameter as an array
The corect way to do it is:
$scope.removeAnswer = function(answer) {
Answers.remove({
id: answer._id
});
};
or instead of id use the corect name you used in the $resource factory definition.
Example:
var User = $resource('/user/:userId', {userId:'#id'});
User.get({userId:123}, function(user) {
user.abc = true;
user.$save();
});
$resource expects object parameter. Here's an example in Angular docs

JQuery Ajax with Symfony

I'm trying to create a game with symfony in which there are warriors. Each warrior has a level. To understand jquery and ajax which i'm new with, i want to create a simple button which when clicked use jquery ajax to get the warrior id and make him lvl up. Here is the level up method controller :
public function warriorLevelUpAction(Warrior $warrior){
$warrior->levelUp();
return $this->render('StormbladesWarriorBundle:Warrior:homepage.html.twig', array(
'warrior' => $warrior
));
}
Here is my Jquery ajax method
$('#cpt').click(function() {
$.ajax({
url: "/stormblades/web/app_dev.php/warriors/levelUp/"+{{ warrior.id }},
error: function(xhr, error){
console.debug(xhr);
console.debug(error);
}
});
And here is my routing :
stormblades_warrior_leveluppage:
path: /warriors/levelUp/{id}
defaults: { _controller: StormbladesWarriorBundle:Warrior:warriorLevelUp }
requirements:
id: \d+
Obviously, this doesn't work, i got a beautiful error 500. Any help and suggestion on what's wrong would be appreciate.
A couple of things stand out to me.
Firstly, your warriorLevelUpAction function requires a warrior object, but in the request you are only passing an id. Therefore, you require an extra step to get the warrior by it's ID then level up. For example:
public function warriorLevelUpAction($id){
$warrior = $this->getDoctrine()
->getRepository('StormbladesWarriorBundle:Warrior')
->find($id);
$warrior->levelUp();
return $this->render('StormbladesWarriorBundle:Warrior:homepage.html.twig', array(
'warrior' => $warrior
));
}
Secondly, if you are only ever going to call this function through AJAX, then you could just return a HTTP 200 Status OK, rather then render homepage.html.twig. You don't have to but, I just find it more efficient. Something like this should be fine:
$response = new Response(Response::HTTP_OK);
return $response;
Lastly, in your AJAX code, the url should be: "/warriors/levelUp/"+{{ warrior.id }}, unless there is a specific reson you are using the full path. This path will work in both development and production, whereas your current code will always run in Debug Mode.
everything said above +....
allow POST in your route through the method : POST attribute like this ( probably the reason of the 500)
defaults : ......
requirements:
_method: POST
As jrmck said , in your controller, either return a Reponse object or
return $this->container->get('templating')->renderResponse('..:page.html.twig',
array( 'var' => $var ));
If you can, use FOSJSRoutingBundle (symfony routes for javascript). Calling routes by URL is not that great if you change of adress or anything. https://github.com/FriendsOfSymfony/FOSJsRoutingBundle.
Or also
url: "{{ path('my_route_php")}}",

Angular accessing $scope objects with dynamic names

I have an angular controller whereby I'm trying to look through a list of strings, querying an ajax query for each string within the list. The code is as follows:
var itemsliststrings = ["department", "year", "project", "subdepartment"];
itemsliststrings.forEach(function (itemStr) {
$http.post("/Budget/GetListBudget", { budgetType: itemStr })
.success(function (data) {
var the_string = itemStr;
var model = $parse(the_string);
model.assign($scope, data);
$scope.$apply();
})
.error(function (error) {
console.error(error);
toastr.error('An error occured, unable to load ' + itemStr);
});
});
This is the code that doesn't work. it complains with the error '$parse' is undefined. I took this example from a SO post.
I basically want to be able to loop through my itemsliststrings, post the string to an web method, and set the return data for this to a model variable called that particular string. So when I take the "department" string, I submit this string to the web method, I get a data object back, which I then set to the $scope.department object.
Thanks
Have you tried just $scope[itemStr] = data?
Did you inject the $parse provider in your controller?
.controller('yourController',function($scope,$parse,etc..){});

AngularJS how to make a POST JSON

I'm trying to make a post but below does not appear to work it does not post but console.log() looks like a put request i.e
http://127.0.0.1/api/v1/participant?account=%7B%22pk%22:1%7D&email=someemail#gmail.com
factory
app.factory('CbgenRestangular', function(Restangular) {
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl('http://127.0.0.1');
});
});
controller
$scope.participant = {email :$scope.email, password: "", account:{pk:1}};
CbgenRestangular.all('api/v1/participant').post("participant", $scope.participant);
What I'm I doing wrong?
according to the documentation (https://github.com/mgonto/restangular#lets-code):
// First way of creating a Restangular object. Just saying the base URL
var baseAccounts = Restangular.all('accounts');
var newAccount = {name: "Gonto's account"};
// POST /accounts
baseAccounts.post(newAccount);
note that post has a single input
you can add 'api/v1' to your base address - there's no need to carry it around (https://github.com/mgonto/restangular#setbaseurl)
I would also suggest using the plural form for a resource route, but that's a convention that some people don't follow - I guess it is a matter of taste

Categories