Iam using Restangular on the clientside with _id as Id field. Sadly Restangular generates wrong URLs, maybe you could say me where the error is?
Restangular.all('/users').one(id).get().then(functon(results) {
$scope.data = results;
})
After the user edited the data:
$scope.save = function() {
$scope.data.put().then(...);
};
This very simple sample generates the following URL with the id twice. I have no idea what went wrong. :(
PUT /users/537283783b17a7fab6e49f66/537283783b17a7fab6e49f66
Solved it by changing the Request workflow of Restangular.
I don't now why, but this approad does not work:
Restangular.all('/users').one(id).get() ... result.put();
But this does:
Restangular.one('/users/',id).get() ... result.put();
Also it is important to tell Restangular that you were using _id instead of id:
angular.module('App').config(function(RestangularProvider, AppSettings) {
RestangularProvider.setRestangularFields({id: "_id"});
});
Related
this is an extract of a code im working on using ng-table. My problem is really simple and supposedly it should be plain easy to overcome but im just unable to do it.
This is the extract :
$scope.loading++;
clean();
$scope.environment = "SOMEENV";
$http({
method: 'GET',
url: 'http://SOMEIP:SOMEPORT/all?environment=SOMEENV'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
$scope.nodes = response.data;
$scope.chartdataservtype=countservtype(response.data);
$scope.tableParams = new NgTableParams({}, { dataset: response.data.rows });
... more code after that.
Lets says that later on, I want to get the FILTERED DATA, not only of the visible part of the table, but from the WHOLE TABLE. According to their GitHub repo, I should be able to do that so easily as writing :
var filteredData = $scope.tableParams.data;
But the problem is that this approach is giving me ONLY THE VISIBLE rows on the table. And I want the whole set of data.
There is some people saying I can customize the getData function of ng-table ( although on their repo other people is saying is not needed as it is "solved" ) but i dont know how to do that.
Can you guys help with this?
You can access the filtered data including the ngTableEventsChannel service in your controller, and listen for filter changes:
function yourController($scope, NgTableParams, ngTableEventsChannel) {
activate();
function activate() {
ngTableEventsChannel.onAfterDataFiltered(function(tableParams, filteredData){
//DO SOMETHING
});
}
}
In that case, tableParams will be the NgTableParams instance that has changed. And filteredData will be what you want to access, your filtered data (yahoo!).
More info: http://ng-table.com/api-docs/classes/ngtableeventschannel.html
I used $http request to get the json data like this
$http.get('test.json')
.success(function(response) {
$scope.contents=response;
});
but when i inspected in developers tool in chrome,
it showing
$http.get('test.json')
.success(function(response) { response=Array[84]// here i got json array
$scope.contents=response; // here Also response=array[84]
});
but $scope.contents is undefined when i put breakpoint to check the value of $scope.contents.
Why is it happening?
Am i Doing anything wrong here?``
you have to make change as follows:
$http.get('test.json')
.success(function(response) { response=Array[84]// here i got json array
$scope.contents=response.data; // here Also response=array[84]
});
now it will give correct result when check for $scope.contents.
I am trying to figure out how to do a simple CRUD with angular + firebase. I started with a read operation. I have the following data structure:
Given that, I created the following rules:
I have a working factory as follows:
factory('wordsFactory', function($http){
var factory = {};
var words = [
{content:"hi", definition:"ooo"},
{content:"h3", definition:"ooo222"}
];
factory.getWords = function(){
return words;
//ajax call here
};
factory.addWords = function(){
//something
}
return factory;
})
I modified it to try and include the call like so:
factory('wordsFactory', function($http){
var factory = {};
var words = [];
var ref = new Firebase('https://my-firebase.firebaseio.com/words');
ref.once('value', function($scope, snapshot) {
$scope.variable = snapshot.val();
words = [
{content: ref.content, definition: ref.definition}
];
});
factory.getWords = function(){
return words;
//ajax call here
};
factory.addWords = function(){
//something
}
return factory;
})
However, whenever I try to read I get:
Error: permission_denied: Client doesn't have permission to access the desired data.
and
FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot read property 'val' of undefined
A number of things, I realize that because of the way I have it, even if it worked, it would only return 1 value. I'm ok with that right now since I just want to figure out how to make it work. I also know that my current data structure could be greatly improved, again I just did it to learn but feel free to suggest anything you think might help me.
My chief concern right now is that I can't get it to read. I should also mention that the factory works properly without firebase, it's being called in the main controller.
I think your issue is related to RULES.
Try replacing the default RULES to below:
{
"rules": {
".read": true,
".write": true
}
}
Now you can access without any Errors.
I'm not sure how you came to this construct:
ref.once('value', function($scope, snapshot) {
But the once callback only takes a single argument for the value event. See the relevant Firebase API documentation.
So you'll have to change it to:
ref.once('value', function(snapshot) {
And find another way to get the scope in there.
For troubleshooting the failing read operation, I recommend using the simulator tab in your Firebase dashboard. It will typically show you a better explanation of why the operation is rejected. This detailed information is intentionally not shown in the regular clients.
If I have a resource, e.g.
var resource = Restangular.all('things');
and I have json object that I want to post to the API
jsonObj = {
someVar: "x",
anotherVar: "y"
}
I can simply do
resource.post(jsonObj).then( ...etc... )
Now if I update the model on the clientside and want to save the changes, why can I not do:
resource.put(thingId, updatedJsonObj)
I'm having trouble getting my head around the demos around on the internet, as they all appear to need to do a get request before they can do a put? Which seems odd. It's worth mentioning that I am using Restangular in a SERVICE, not in a controller or directive. My service has a variable allMyThings which is the result of resource.getList() and I use that in various places in the application
Actually, if you take one item in the collection returned by getList(), you can use .save() on it, and it will call PUT method.
angular.module('demo').controller('DemoCtrl', function (myList) {
// myList is populated by Restangular.all('...').getList();
var item = myList[0];
item.name = 'foo';
item.save() // this one did a PUT :)
.then(function (res) {
// ...
});
});
See it in the docs.
NOTE :
Restangular will use the id property as id for PUT, DELETE, etc. calls.
If you want to change that behaviour, use this in module config :
RestangularProvider.setRestangularFields({
id: "_id"
});
I need to call a web service that requires a list of IDs in the form:
http://service_addr?itemID=34&itemID=36 ...
I tried setting up my service factory as:
.factory("doService", [$resource, function($resource) {
return $resource("service_addr", {}, {
'create' : {method:POST, isArray:true} }); }])
In my controller I invoke the service with this code:
var ids = [];
angular.forEach(listofIDs, function(anId) {
ids.push( { itemID : anID } );
}
doService.create(ids, {}, function (response) {
... do response stuff
}
in the console the POST return a 40 Bad request error. The request is malformed in the parameters as shown below:
http://service_addr?0=%5Bobject+Object%5D&1=%5Bobject+Object%5
How can I get the required parameters passed correctly?
Adding to ricick's answer, you could also pass the IDs in the format
http://service_addr?itemIDs=34,36,38
by doing
ids.join(',')
The issue is you can't have more than one parameter with the same name in GET, so even if angular could pass the data your server will only see one value (unless you're cheating and procssing the url string manually).
A better solution would be to something like:
http://service_addr?itemID0=34&itemID1=36itemID1=38&itemIDCount=3
that way you create a seperate parameter for each variable.