首先,我很抱歉我的英文和破代码......(这里的很多词来自谷歌翻译......所以,我害怕我无法让自己清楚......所以,我粘贴所有代码...)
在 Rails 中设置路线非常容易。但是当我们想把它变成angurjs时,它变得有点冗长......这种工作有没有“最佳实践”:
给定一些铁路路线资源:
resources :users
resources :photos
...
resources :topics
如何在角度侧做到这一点?
这就是我的做法(在咖啡脚本中,使用angular 1.1.4):
以 Rails 方式使用 RESTful 服务:
# angular/services/restful.js.coffee
# RESTful resource following Rails route's convention
# index: GET '/resource.json'
# save: POST '/resource.json'
# get: GET '/resource/:id.json'
# update: PUT '/resource/:id.json'
# edit: GET '/resource/:id/edit.json'
# new: GET just use get, id: 'new'
app.factory('RESTful', ['$resource',
($resource)->
(resource_name) ->
url = "/#{resource_name}/:id:format"
defaults={format: '.json', id: '@id'}
actions = {
index:
id: ''
url: "/#{resource_name}:format"
method: 'GET'
isArray:false
edit:
url: "/#{resource_name}/:id/edit:format"
method: 'GET'
update:
method: 'PUT'
save:
url: "/#{resource_name}:format"
method: 'POST'
}
$resource url, defaults, actions
])
# index: GET '/parents/:parent_id/children.json'
# save: POST '/parents/:parent_id/children.json'
# get: GET '/parents/:parent_id/children/:id.json'
# update: PUT '/parents/:parent_id/children/:id.json'
# edit: GET '/parents/:parent_id/children/:id/edit.json'
# new: GET just use get, id: 'new'
app.factory('NESTful', ['$resource',
($resource)->
(parents, children) ->
# naive singularize
parent = parents.replace(/s$/, '')
url = "/#{parents}/:#{parent}_id/#{children}/:id:format"
defaults={ format: '.json', id: '@id' }
actions = {
index:
id: ''
url: "/#{parents}/:#{parent}_id/#{children}:format"
method: 'GET'
isArray:false
edit:
url: "/#{parents}/:#{parent}_id/#{children}/:id/edit:format"
method: 'GET'
update:
method: 'PUT'
save:
url: "/#{parents}/:#{parent}_id/#{children}:format"
method: 'POST'
}
$resource url, defaults, actions
])
路线:
app.config(['$routeProvider', '$locationProvider', ($routeProvider, $locationProvider) ->
$locationProvider.html5Mode(true)
# general RESTful routes
resource_list = ['users', 'photos', 'topics']
for resource in resource_list
# naive singularize
singular = resource.replace(/s$/, "")
captialize = singular.charAt(0).toUpperCase() + singular.slice(1)
$routeProvider
.when "/#{resource}",
templateUrl: "/tp/#{resource}/index"
controller: "#{captialize}IndexCtrl"
resolve:
index: ["#{captialize}Loader", (Loader)-> Loader('index')]
.when "/#{resource}/new",
templateUrl: "/tp/#{resource}/edit"
controller: "#{captialize}NewCtrl"
resolve:
resource: ["#{captialize}Loader", (Loader)-> Loader('new')]
.when "/#{resource}/:id",
templateUrl: "/tp/#{resource}/show"
controller: "#{captialize}ShowCtrl"
resolve:
resource: ["#{captialize}Loader", (Loader)-> Loader('show')]
.when "/#{resource}/:id/edit",
templateUrl: "/tp/#{resource}/edit"
controller: "#{captialize}EditCtrl"
resolve:
resource: ["#{captialize}Loader", (Loader)-> Loader('edit')]
# special routes
$routeProvider
.when '/',
templateUrl: '/tp/pages/home'
controller: 'PageCtrl'
.otherwise({redirectTo: '/'})
])
控制器应该超级干净,然后我们可以专注于业务
app.controller 'UserShowCtrl', ['$scope', 'resource'
($scope, resource)->
$scope.user = resource.user
]
app.controller 'UserIndexCtrl', ['$scope', 'index',
($scope, index) ->
$scope.users = index.resource.users
$scope.total_pages = index.pages.total_pages
$scope.currentPage = index.pages.current_page
getPage = (page)->
index.resource.$index
page: page
(resource, headers)->
$scope.users = resource.users
$scope.$watch 'currentPage', (newval)->
getPage(newval)
]
问题出在用于解析 obj 的加载器中:
app.factory('UserLoader', ['RESTful', '$route', '$q',
(RESTful, $route, $q) ->
(action)->
model = 'users'
delay = $q.defer()
fetcher = RESTful(model)
switch action
when 'index'
fetcher.index
page: $route.current.params.page
(resource, headers)->
delay.resolve
resource: resource
pages: JSON.parse(headers('X-Pagination'))
(resource)->
delay.reject "Unable to fetch #{model} index"
when 'show'
fetcher.get
id: $route.current.params.id
(resource)->
delay.resolve(resource)
(resource)->
delay.reject "Unable to fetch #{model} #{$route.current.params.id}"
when 'edit'
fetcher.edit
id: $route.current.params.id
(resource)->
delay.resolve(resource)
(resource)->
delay.reject "Unable to fetch #{model} #{$route.current.params.id} edit"
when 'new'
fetcher.get
id: 'new'
(resource)->
delay.resolve(resource)
(resource)->
delay.reject "Unable to fetch #{model} new"
return delay.promise
])
这里有2个问题:第一个问题是我必须复制&&粘贴上面的加载器代码,然后gsub('user','photo')等...(其实只改2个字,但我讨厌复制&&粘贴..)我尝试将它移动到一个不起作用的 for 循环中,但我不知道为什么......另一个问题是如何以 DRY 方式设置嵌套资源
最后,感谢您的耐心等待,我会再次表示抱歉……但是有任何解决方案、建议或最佳实践吗?我做错了吗?