Skip to content

Instantly share code, notes, and snippets.

@damanm24
Last active June 9, 2017 03:52
Show Gist options
  • Save damanm24/447227f0b6b5ba4844d96e9dfc50edd9 to your computer and use it in GitHub Desktop.
Save damanm24/447227f0b6b5ba4844d96e9dfc50edd9 to your computer and use it in GitHub Desktop.
Accessing properties/indexes of unknown object in JavaScript
function NewPollCtrl($scope, $http, $location, $q) {
$scope.poll = {
options: [{
text: ''
}, {
text: ''
}]
};
$scope.cbstate = "not-pressed";
inputTyped = function() {
$scope.cbstate = "pressed";
}
$scope.smartText = function(input1Text, input2Text) {
var p1, p2;
input1Text === "" ? p1 = "____ " : p1 = input1Text.replace(/\b\w/g, function(l) {
return l.toUpperCase()
}) + " ";
input2Text === "" ? p2 = "..." : p2 = " " + input2Text.replace(/\b\w/g, function(l) {
return l.toUpperCase()
});
return p1 + "and" + p2;
}
$scope.addChoice = function() {
if ($scope.poll.options.length > 3) {
//$scope.error;
console.log("Too many choices");
} else {
$scope.poll.options.push({
text: ''
});
}
};
$scope.getRequest = function() {
$http.post('/api/pollPost', JSON.stringify($scope.poll)).then(
function successCallback(response) {
console.log(response.data);
$location.path('/getPoll/' + JSON.stringify(response.data));
//console.log(response);
},
function failedCallback(response) {
}
);
};
$scope.addProperties = function() {
for (var i = 0; i < $scope.poll.options.length; i++) {
$scope.poll.options[i].votes = 0;
$scope.poll.options[i].imageURL = "";
}
};
$scope.textBoxValidation = function(poll) {
var isValid = true;
for (var i = 0; i < poll.options.length; i++) {
if (poll.options[i].text.length == 0) {
isValid = false;
console.log("Error blank answers");
break;
}
}
return isValid
};
$scope.submitChoice = function() {
var poll = $scope.poll;
var isValid = $scope.textBoxValidation(poll);
if (isValid) {
var defer = $q.defer();
defer.promise
.then(function() {
$scope.addVotesProperty();
}).then(function() {
console.log('test');
}).then(function() {
$scope.getRequest();
});
defer.resolve();
} else {
console.log('Your poll is invalid');
}
};
$scope.submitChoice = function() {
var poll = $scope.poll;
var isValid = $scope.textBoxValidation(poll);
if (isValid) {
var defer = $q.defer();
defer.promise
.then(function() {
$scope.addProperties();
}).then(function() {
$scope.getPictures().then(function(result) {
console.log(result);
console.log("RESULT: " + result.length);
console.log("$SCOPE: " + $scope.poll.options.length);
for(var i = 0; i < $scope.poll.options.length; i++) {
console.log(result[i]);
$scope.poll.options[i].imageURL = result[i];
}
console.log($scope.poll);
});
});
defer.resolve();
} else {
console.log('Your poll is invalid');
}
};
$scope.getPictures = function() {
var q = $q.defer();
var promises = [];
angular.forEach($scope.poll.options, function(value) {
$scope.bingRequest(value.text).then(function(result) {
promises.push(result.value[0].contentUrl);
})
})
q.resolve(promises);
return q.promise;
};
$scope.bingRequest = function(searchTerm) {
var q = $q.defer();
console.log(searchTerm);
$http.get('https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=' + searchTerm + '&count=1&offset=0&mkt=en-us&safeSearch=Strict', {
headers: {
'Ocp-Apim-Subscription-Key': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
}
}).success(function(response) {
q.resolve(response);
}).error(function(err, status) {
q.reject(err);
});
return q.promise;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment