4
我正在研究我的第一個AngularJS應用程序,以瞭解該框架。如何使用ngResource處理3級深層嵌套資源?
我目前正在尋找一種尋找處理嵌套資源的慣用方式的牆。
我地區含含單位建築。一個平面只在一個建築物內,這是隻在一個區域內。
在我的數據庫模型我並不需要與平對象存儲區域ID在一起,其父(建築ID)就足夠了。
下面是我寫的一個簡單例子來說明這個概念。我根據它在我的視頻看到了來自谷歌的I/O:http://www.youtube.com/watch?v=HCR7i5F5L8c
我的問題是:我怎麼能實例化一個特定平資源?我擁有網址中所需的所有信息,但並不嚴格對應於我的模特(缺少區域Id)。
var app = angular.module('neighborhoodApp', [
'ngResource',
'ngRoute'
]);
angular.module('neighborhoodApp')
.factory('Area', function ($resource) {
return $resource('/areas/:id', {
'id': '@_id'
});
});
angular.module('neighborhoodApp')
.factory('Building', function ($resource) {
return $resource('/areas/:aid/buildings/:id', {
'id': '@_id',
'aid': '@area_id'
});
});
angular.module('neighborhoodApp')
.factory('Flat', function ($resource) {
return $resource('/areas/:aid/buildings/:id/flats/:id', {
'id': '@_id',
'bid': '@building_id'
// 'aid': '@area_id' => my model doesn't have an area id, having a building id attached to a flat is enough to find the area
});
});
angular.module('neighborhoodApp')
.controller('AreaListCtrl', function (Area) {
this.areas = Area.query();
});
angular.module('neighborhoodApp')
.controller('AreaShowCtrl', function ($routeParams, Area) {
this.area = Area.get({
id: $routeParams.aid
});
});
angular.module('neighborhoodApp')
.controller('BuildingListCtrl', function (Building) {
this.buildings = Building.query();
});
angular.module('neighborhoodApp')
.controller('BuildingShowCtrl', function ($routeParams, Building) {
this.building = Building.get({
aid: $routeParams.aid,
id: $routeParams.rid
});
});
angular.module('neighborhoodApp')
.controller('FlatShowCtrl', function (Flat) {
this.flats = Flat.query();
});
// What is the idiomatic way to pass the area id (from $routeParams.aid)
// so that the server can be queried successfully
angular.module('neighborhoodApp')
.controller('FlatListCtrl', function ($routeParams, Flat) {
this.flat = Flat.get({
bid: $routeParams.bid,
id: $routeParams.rid
});
});
app.config(function ($routeProvider) {
$routeProvider
.when('/areas', {
templateUrl: 'views/area/list.html',
controller: 'AreaListCtrl',
controllerAs: 'list'
})
.when('/areas/:aid', {
templateUrl: 'views/area/show.html',
controller: 'AreaShowCtrl',
controllerAs: 'show'
})
.when('/areas/:aid/buildings', {
templateUrl: 'views/building/list.html',
controller: 'BuildingListCtrl',
controllerAs: 'list'
})
.when('/areas/:aid/buildings/:bid', {
templateUrl: 'views/building/show.html',
controller: 'BuildingShowCtrl',
controllerAs: 'show'
})
.when('/areas/:aid/buildings/:bid/flats', {
templateUrl: 'views/flat/list.html',
controller: 'FlatShowCtrl',
controllerAs: 'list'
})
.when('/areas/:aid/buildings/:bid/flats/:fid', {
templateUrl: 'views/flat/show.html',
controller: 'FlatListCtrl',
controllerAs: 'show'
})
.otherwise({
redirectTo: '/areas'
});
});
它看起來像API是Restful。如果是這樣,我會強烈建議Restangular庫...非常簡化的工作流程。我只是在我的第一個項目上學習角度。我大約2mo英寸Restangular非常光滑。當你獲取一個資源時,它會返回一個js對象,你修改該js對象,並且它已經知道如何將自己保存回api ..即「myRestObj.post()」https://github.com/ mgonto/restangular – timh
因此,您正在將'bid'和'rid'傳遞給您的路線,而這兩者足以實例化一個特定的'Flat'? –