Ember链接到模型中的下一个对象 [英] Ember Link To Next Object In Model
问题描述
http://emberjs.jsbin.com/aGUguJA/10
使用bloggr示例,我构建了一个帖子列表和一个帖子视图,并且已经向Post模板添加了2个操作,上一个和下一个。
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
}}
});
我无法弄清楚如何使上一个和下一个工作。我有一个强烈的感觉,我需要使用一个ArrayController,但我仍然不知道从哪里去。
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.
推荐答案
一个简单的解决方案是在您的帖子对象中添加两个字段/变量,即
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
}
所以你可以在你的数据中提供这些信息,或者创建从数据中实现的App.Post对象,并且编写一个简单的迭代填充您的App.PostsRoute模型部分中的字段。
然后,您可以相应地修改您的模板,并从下一个和上一个按钮的链接传递对象或ID。
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.
查看一个工作示例,
http://emberjs.jsbin.com/OxajiVi/1/
编辑
以下示例演示了使用Ember类和对象创建链接列表的解决方案,json数据中没有前一个和下一个字段,也可以直接访问一个帖子,
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
这篇关于Ember链接到模型中的下一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!