[英]Filtered list not updated on pagination
I have a list of objects that I can filter on a certain attribute value, eg all posts vs. published posts only. 我有一个对象列表,可以根据某个属性值进行过滤,例如,所有帖子与仅发布的帖子。 I also created 2 routes, so both lists can be accessed using an url: 我还创建了2条路由,因此可以使用url访问两个列表:
App.Router.map ->
@resource 'posts', -> # '/posts'
@route 'published' # '/posts/published'
Both lists use the same template: 这两个列表使用相同的模板:
App.PostsRoute = Em.Route.extend
model: ->
@get('store').find 'post'
App.PostsIndexRoute = Em.Route.extend
model: ->
@modelFor 'posts'
App.PostsPublishedRoute = Em.Route.extend
model: ->
@modelFor('posts').filter( (item) ->
item.get('publishAt') <= new Date()
)
renderTemplate: (controller) ->
@render 'posts/index',
controller: controller
# posts.hbs
...
{{#link-to 'posts.index'}}All{{/link-to}}
{{#link-to 'posts.published'}}Published{{/link-to}}
{{outlet}}
# posts/index.hbs
{{#each}}
<li>...</li>
{{/each}}
{{link-to showMore}}Show More{{/link-to}}
The showMore
action in a base controller sends a request to a (Rails) backend which responds with the next page. 基本控制器中的showMore
操作将请求发送到(Rails)后端,该后端用下一页进行响应。
App.PostsController = Em.ArrayController.extend
actions:
showMore: ->
@store.find 'post',
page: 2
This showMore
works as expected when the un-filtered list is shown. 显示未过滤的列表时,此showMore
可以按预期工作。 To keep the example simple, I hard-coded the page value. 为了使示例简单,我对页面值进行了硬编码。
However, when the filtered list is displayed, the showMore
does send a AJAX request, but the page does not get updated with the extended list of objects. 但是,当显示过滤的列表时, showMore
确实会发送AJAX请求,但是页面不会使用扩展的对象列表进行更新。 When I toggle between All
and Published
the list of published is updated. 当我在All
和已Published
之间切换时,已Published
列表会更新。
When the showMore
action is called, the AJAX request is sent, but the list isn't updated. 调用showMore
操作时,将发送AJAX请求,但不会更新列表。 This is due to the fact that the route's model
hook is called only on entering the route. 这是由于以下事实:仅在输入路线时才调用路线的model
挂钩。
You need to make sure the list is updated when the posts change by filtering the collection in the App.PostPublishedController
: 您需要通过过滤App.PostPublishedController
的集合来确保在帖子更改时更新列表:
App.PostPublishedController = Ember.ArrayController.extend
needs: ['posts']
posts: Ember.computed.alias('controllers.posts.content')
content: ( ->
@get('posts').filter( (item) ->
item.get('publishAt') <= new Date()
)
).property('posts.[]')
App.Router.map ->
@resource 'firstPostsPage', { path: '/posts' }
@resource 'posts', { path: '/posts/:page' }, ->
@route 'published'
App.PostsRoute = Em.Route.extend
model: (params) ->
@get('store').find 'post',
page: params.page
App.FirstPostsPageRoute = Em.Route.extend
redirect: ->
// Redirect '/posts' to '/posts/1'
this.transitionTo('posts', { page: 1 });
Then the following routes definitions would work as expected, without the need to filter in PostPublishedController
然后,以下路由定义将按预期工作,而无需在PostPublishedController
进行过滤
App.PostsIndexRoute = Em.Route.extend
model: ->
@modelFor 'posts'
App.PostsPublishedRoute = Em.Route.extend
model: ->
@modelFor('posts').filter( (item) ->
item.get('publishAt') <= new Date()
)
renderTemplate: (controller) ->
@render 'posts/index',
controller: controller
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.