Specifically, with AngularJS 1.x and a form, my code goes like this:
$scope.settings = { };
$scope.post_social = function() {
var ref = new Firebase(path);
ref.update({
twitter: $scope.settings.twitter,
facebook: $scope.settings.facebook,
google: $scope.settings.google
});
toaster.pop('success', "System", "Settings saved!");
};
This works perfectly fine on Chrome Latest for OS X - but a Windows user, Chrome Latest, reported that this form throws an error if any of the 3 fields are empty, which causes me to change the code to this:
$scope.post_social = function() {
var ref = new Firebase(path);
ref.update({
twitter: ($scope.settings.twitter ? $scope.settings.twitter : ""),
facebook: ($scope.settings.facebook ? $scope.settings.facebook : ""),
google: ($scope.settings.google ? $scope.settings.google : "")
});
toaster.pop('success', "System", "Settings saved!");
};
Firebase (my version of the framework anyways) doesn't allow an empty value; so without the inline if statement, it fails.
Is there an easy way to ensure all items in an object are at the least, not empty without having to do an inline if for each ?
Denny has the absolute right answer. I also wanted to add, however, don't use $scope if you don't need. It adds overhead to your app that you don't need. Try the following:
var self = this;
self.settings = { };
self .post_social = function() {
var ref = new Firebase(path);
ref.update({
twitter: self.settings.twitter || '',
facebook: self.settings.facebook || '',
google: self.settings.google || ''
});
toaster.pop('success', "System", "Settings saved!");
};
Also, I highly recommend putting your toaster popups in a service. It lets you swap alert services in and out easily. It doesn't seem like a big thing, but we had a customer want a different style of messages and we were able to swap out the plugin and change the alertService and not affect any other areas of our code.
Denny Trebbin
Lead Fullstack Developer. Experimenting with bleeding-edge tech. Irregularly DJ. Hobby drone pilot. Amateur photographer.
Can you use default values in ng1?
{ ... twitter: $scope.settings.twitter || "", .. }