问题
http://emberjs.jsbin.com/aGUguJA/10
Using the bloggr example I built a list of posts and a post view and I've added 2 actions to the post template, Previous and Next.
App.PostController = Ember.ObjectController.extend({
actions:{
next: function(){
//Go to next object in a model
},
prev: function(){
//Go to previous object in a model
}}
});
I can't figure out how to make the Previous and Next to work. I have a strong feeling I need to be using an ArrayController but I still wouldn't know where to go from there.
回答1:
One simple solution would be to add two fields/variables in your post object i.e.
{
id: '1',
title: "Object 1",
author: { name: "Awesome Person 1" },
date: new Date('01-01-2013'),
excerpt:"Lorem ..",
body:"Lore.."
next:2,
prev:null
}
So you either have this information available in your data or create App.Post objects that get instanciated from the data and you write a simple iteration to populate the fields within the model part of your App.PostsRoute. Then you can modify your template accordingly and pass the object or id from the link-to of next and previous buttons.
look at a working example, http://emberjs.jsbin.com/OxajiVi/1/
EDIT The following example demostrates the solution using Ember class and objects to create the linked list, no previous and next fields in json data and also works when visiting a post directly,
http://emberjs.jsbin.com/uWAmUba/1
回答2:
This could be helpful:
<button {{action "next"}}>Next</button>
来源:https://stackoverflow.com/questions/19424714/ember-link-to-next-object-in-model