enter code hereHow to access objects in Javascript
This is the Object trying to Access
This is the Code
Its returning 'UNDEFINED' why?
It may be silly. please help me im new.
CODE :
app.controller("myCtrl", function($scope,Restangular) {
Restangular.setBaseUrl('http://localhost:8080/StrutsREST/api');
var arr=Restangular.all('interfaces').doGET();
var obj=arr.$object;
console.log(obj.A1);
});
As you are doing API call which is async you need to handle it as Promise by using .then statement
Restangular.all('interfaces').getList().then(function(data) {
console.log(data.A1);
})
Related
I am trying to understand this piece of code
require(['mifosXComponents'], function (componentsInit) {
componentsInit().then(function(){
require(['test/testInitializer'], function (testMode) {
if (!testMode) {
angular.bootstrap(document, ['MifosX_Application']);
}
});
});
});
The code is at mifosX client code. I believe this is the entry point of the mifosX web client software. I am really confused by the require syntax here. All the online sample code I have seen is like require(["a", "b"], function (a, b){});. In the other words, the parameter list inside the function() are all listed inside the dependence [] right before it. However the code I pasted above has componentsInit inside the function(). And I could not find any place in the source code tree that componentsInit gets defined.....
What I am trying here is to understand the code logic flow of mifosX. I am new to Javascript and RequireJS. Please help me understand this if you know what's going on here. Thanks in advance!
Here is your code with some comments which will clarify:
// in the first line you are requiring a module with id `mifosXComponents`
// which then is passed to the callback as `componentsInit`
require(['mifosXComponents'], function (componentsInit) {
// seems that `componentsInit` is a function which returns a Promise object,
// so your are passing a callback to it to execute it after its fulfilment
componentsInit().then(function(){
// when the promise is fulfilled, you are requiring another module
// with id `test/testInitializer` which is passed to callback as `testMode`
require(['test/testInitializer'], function (testMode) {
// next lines are pretty simple to understand :)
if (!testMode) {
angular.bootstrap(document, ['MifosX_Application']);
}
});
});
});
About Promise you can read here: What does the function then() mean in JavaScript?
I am trying to call a service in js in a asp.net model.
can you please help me find the right structure,
here is my general code.
GetSomething: function () {
var something;
System.SomeService.GetSomething(new Report.DocumentsRequest(), null, function (response) {
return something = Report.GetResponse(response);???
});
return something = Report.GetResponse(response);???
//
},
tank you for the help
It's hard to tell out of context, but it looks as though you're attempting to mix C# code with JavaScript code. If that's the case, you cannot do that. Instead, you need to provide an endpoint (controller action) that accesses your service and returns the results as something like JSON. Then, you need to utilize AJAX (the XMLHttpRequest object) in your JavaScript to make a request to that endpoint.
I am working with a mobile App which uses Parse as a backend and I have an issue with the find function. When running the find function in the format of:
var = firstQuery = (new Parse.Query("MyParseObject"))
.find(),
secondQuery = (new Parse.Query("OtherParseObject")).get(id)
// there is only one object that firstQuery can find
Parse.Promise.when(firstQuery, secondQuery)
.then( function (query1res, query2res) {
// query1res should return only one result wrapped in an array,
// instead query1res is an object without a get method
query1res.forEach (function (res) {
// this fails: cannot get .length of undefined
})
// ... do stuff with the returned data
})
Is there something i am missing? I am sure this used to work before, but now it does not.
It is quite difficult to correctly get in an debug this issue thanks to the way Parse works, but their docs outline that this should return an array, but it does not at this point of time.
Thanks for your help.
Based on the Parse docs, it looks like Parse.Promise.when expects an array, although based on this support thread, the results will be passed in as individual arguments to the then handler. Give this a try:
Parse.Promise.when([firstQuery, secondQuery])
.then(function (query1res, query2res) {
// use query1res and query2res
});
Turns out it was down to a function deeper in the code, which needed to return a promise to chain off of. after adding this the code was happy enough. The function that needed to return the promise was called in the forEach and has nothing to do with the initial two queries, which is what was throwing me.
I'm playing around with MeanJS recently and I ran into an issue that I'm not well versed in so I'm hoping someone can help here.
I have a angular controller that sets it scope like this:
// Find existing Topic
$scope.findOne = function() {
$scope.topic = Topics.get({
topicId: $stateParams.topicId
});
};
I this sets the topic in the $scope. However, I also need to get a value off of the topic as well. Lets assume it is a date such as createdAt. I need to get this value and then perform another operation on it in order to set another scope value. The problem is, the $scope.topic object is an Angular promise. How can I get the value createdAt (which is a Date) off of that promise?
You can use promise callback.
$scope.findOne = function() {
$scope.topic = Topics.get({
topicId: $stateParams.topicId
});
$scope.topic.$promise.then(function(data) {
// access data.createdAt here
});
};
Its actual also depends on that where you want to use this. So if you want to use this in the view, you can write directly as:
<span> Created at: {{topic.createdAt}}</span>
Angular will autoupdate that scope variable when data is loaded.
Watch this video,
https://www.youtube.com/watch?v=IRelx4-ISbs
You'll find that the code has a line wrote this:
$scope.twitterResult = $scope.twitter.get({q:$scope.searchTerm});
This is a litter quirk:
the 'get' method of 'twitter' is obviously a async function, how does it return a value to $scope.twitterResult???
jsFiddle(Can't work cause the twitter API has changed):
http://jsfiddle.net/johnlindquist/qmNvq/
This code $scope.twitter.get({q:$scope.searchTerm}); return defer object. Angular view arranged so that view will be update when defer object resolve. It's just a convenience.
$scope.twitterResult = $scope.twitter.get({q:$scope.searchTerm});
Your code is good for only angular binding.
But if you need to get data on run time, you need to write this below format.
If $scope.twitter is a URL,Then write it
$http.get($scope.twitter, {q:$scope.searchTerm}).success(function (response) {
$scope.twitterResult=response;
}
$http is must defined in contoller