所以我试图让我的路由工作,这样我就可以在我的网址中分享永久链接了。 I.E. “/工具入门”。
我似乎无法弄清楚模型/序列化是如何工作的,那么ember-data如何知道如何用字符串查找我的对象?我已经尝试了queryFixtures,它返回了正确的模型,但没有设置
这是我的代码:
App.Router.map (match) ->
@resource 'topics', {path: "/support"}, ->
@route 'topic', {path: '/:permalink'}
# Support Routes
App.TopicsRoute = Ember.Route.extend
model: ->
this.store.find('topic')
App.TopicsTopicRoute = Ember.Route.extend
# serialize: (model, params) ->
# return { topic_id: model.get(params.topic_id) }
model: (param) ->
@get('store').findQuery App.Topic, {permalink: param.permalink}
setupController: (controller, model) ->
controller.set("content", model)
actions:
error: (error) ->
console.log error
App.ApplicationAdapter = DS.FixtureAdapter.extend
queryFixtures: (fixtures, query, type) ->
fixtures.filter (item) ->
for property, value of query
if item[property] == value
return item
else
false
App.Topic = DS.Model.extend
name: DS.attr('string')
permalink: DS.attr('string')
position: DS.attr('number')
# Fiture data
App.Topic.FIXTURES = [
{
id: 1
name: "Getting Started"
permalink: "getting-started"
position: 1
articles: [
{ name: "test 1", content: "Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et." },
{ name: "test 2", content: "Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et." },
{ name: "test 3", content: "Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et." }
]
},
{
id: 2
name: "Frequently Asked Questions"
permalink: "frequently-asked-questions"
position: 2
articles: [
{ name: "test 1", content: "Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et." },
{ name: "test 2", content: "Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et." }
]
},
{
id: 3
name: "Tips & Tricks"
permalink: "tips-tricks"
position: 3
articles: [
{ name: "test 1", content: "Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et." }
]
}
]
答案 0 :(得分:0)
对于这个答案,我假设您使用的是Ember版本1.0.0和Ember Data的Beta版本1.0.0。
对于您描述的路由器,您应该设置以下路由对象:
App.TopicsIndexRoute = Ember.Route.extend
和
App.TopicIndexRoute = Ember.Route.extend
否则Ember会将正确的模型传递给路线。
现在在TopicIndexRoute(代码中的TopicsTopicRoute)中使用模型钩子,它只在输入URL或重新加载页面时调用,而不是在通过{{link-to}}助手进入路径时调用({ {3}})。
当您从{{link-to}}帮助者转到TopicIndexRoute时," ember magic"仅当动态细分受众群是模型的ID时,才会将动态细分受众群(http://emberjs.com/api/classes/Ember.Route.html#method_model)与相应的模型匹配。我还不知道如何解决这个问题。这可能会有所帮助:http://emberjs.com/guides/routing/specifying-a-routes-model/#toc_dynamic-models
我不知道您使用的灯具是否有效,但在设置您应该使用的关系时(至少说明它是如何在指南中显示的:{{3 }})模型ID而不是声明实例。它看起来像这样:
App.Article.FIXTURES = [
{
id: 1,
name: "test 1",
content: "Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et."
},
{
id: 2,
name: "test 2",
content: "Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et."
},
{
id: 3,
name: "test 3",
content: "Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et."
}
]
App.Topic.FIXTURES = [
{
id: 1
name: "Getting Started"
permalink: "getting-started"
position: 1
articles: [1,2,3]
}
]
我希望这会对你有所帮助。