Skip to content

Instantly share code, notes, and snippets.

@clintberry
Forked from thehashrocket/gist:9942715
Last active August 29, 2015 13:58
Show Gist options
  • Save clintberry/9942785 to your computer and use it in GitHub Desktop.
Save clintberry/9942785 to your computer and use it in GitHub Desktop.
CONTROLLER
angular.module('mean.properties').controller('PropertiesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Properties','PropertiesByUser',
function($scope, $stateParams, $location, Authentication, Properties, PropertiesByUser) {
$scope.authentication = Authentication;
$scope.create = function() {
var property = new Properties({
businessName: this.businessName,
content: this.content,
userID: Authentication.user._id
});
property.$save(function(response) {
$location.path('properties/' + response._id);
});
this.businessName = '';
this.content = '';
this.userID = '';
};
$scope.remove = function(property) {
if (property) {
property.$remove();
for (var i in $scope.properties) {
if ($scope.properties[i] === property) {
$scope.properties.splice(i, 1);
}
}
} else {
$scope.property.$remove(function() {
$location.path('properties');
});
}
};
$scope.update = function() {
var property = $scope.property;
if (!property.updated) {
property.updated = [];
}
property.updated.push(new Date().getTime());
property.$update(function() {
$location.path('properties/' + property._id);
});
};
$scope.find = function() {
Properties.query(function(properties) {
$scope.properties = properties;
});
};
$scope.findPropertyByUser = function() {
PropertiesByUser.get({userID: Authentication.user._id},
function(OwnerProperty) {
$scope.hasProperty = OwnerProperty;
console.log($scope.hasProperty);
});
};
$scope.findOne = function() {
Properties.get({
propertyId: $stateParams.propertyId
}, function(property) {
$scope.property = property;
});
};
}
]);
VIEW
<section data-ng-controller="PropertiesController" data-ng-init="findPropertyByUser()">
<p>{{hasProperty}}</p>
</section>
SERVICE
'use strict';
//Properties service used for articles REST endpoint
angular.module('mean.properties')
.factory('Properties', ['$resource', function($resource) {
return $resource('properties/:propertyId', {
propertyId: '@_id'
}, {
update: {
method: 'PUT'
}
});
}]).factory('PropertiesByUser', ['$resource',
function($resource){
return $resource('properties/user/:userID',{}, {
query: {method:'GET', params:{userID:'userID'}, isArray:true}
});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment