Angular Resource - Default Model Structure
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular-resource.min.js"></script>
<div ng-app="app">
<div ng-controller="AddNeighborhoodCtrl">
<div>
<label>Name:</label>
<input type="text" ng-model="neighborhood.name"/>
</div>
<div>
<label>Categories:</label>
<select name="categories" ng-model="neighborhood.categories" multiple>
<option value="1">Category A</option>
<option value="2">Category B</option>
<option value="3">Category C</option>
</select>
</div>
<fieldset ng-repeat="building in neighborhood.buildings">
<legend>Building</legend>
<label>Name:</label>
<input type="text" ng-model="building.name" />
<fieldset ng-repeat="floor in building.floors">
<legend>Floor</legend>
<label>Level:</label>
<input type="text" ng-model="floor.level" />
<fieldset ng-repeat="unit in floor.units">
<legend>Unit</legend>
<label>Residing Family:</label>
<input type="text" ng-model="unit.familyName" />
</fieldset>
<button type="button" ng-click="add('unit', floor)">+ Add Unit</button>
</fieldset>
<button type="button" ng-click="add('floor', building)">+ Add Floor</button>
</fieldset>
<button type="button" ng-click="add('building')">+ Add Building</button>
<pre ng-bind="neighborhood | json"></pre>
</div>
</div>
CSS
select {
width: 150px;
margin-bottom: 20px;
}
label { display: block; }
pre {
margin-top: 50px;
background-color: #e5e5e5;
}
JavaScript
var mod = angular.module('app', ['ngResource']);
mod.factory('app.entity.neighborhood',
['$resource',
function($resource) { console.log($resource);
return $resource('http://xmpl.com/neighborhoods/:id', { id: '@id'});
}]);
mod.controller('AddNeighborhoodCtrl',
['$scope', 'app.entity.neighborhood',
function($scope, Neighborhood) {
$scope.neighborhood = new Neighborhood();
$scope.neighborhood.buildings = [
{
floors: [
{
units: [
{}
]
}
]
}
];
$scope.add = function(type, context) {
console.log(type, context);
switch (type) {
case 'building':
$scope.neighborhood.buildings.push({floors:[{units:[]}]});
break;
case 'floor':
context.floors.push({units:[{}]});
break;
case 'unit':
context.units.push({});
break;
};
};
}]);