2013-07-19 160 views
1

我有一个Ember.js应用程序,顶部有一个导航栏。其中一个按钮去/home而另一个则去/book/:id/overview。如果我在/home页面,该按钮呈现良好。如果我将鼠标悬停在按钮上,我可以看到链接将带我到/book/2/overview。如果我复制链接位置并将其粘贴到地址栏,它将带我到/book/2/overview。但是,如果我只是点击链接,我会采取/book/undefined/overview。链接显然指向了正确的位置,但Ember.js却把我带到了错误的位置。 (或者更具体地说,它似乎没有找到模型,尽管如果我手动输入ID,它会发现它很好)。Ember.js linkTo帮手没有正确链接

什么可能会出错?

(我会尽快发布一些代码,因为我知道相关的位。这似乎是它可以在这一点上是这么多的事情。)

+0

模型到URL序列化使用路由的'serialize'方法来完成。如果模型的'id'属性需要定制'serialize'实现。但是,如果您在悬停时获得了正确的网址,则应该可以正常工作。尝试验证你传递给'linkTo'的模型是否有'id'。 –

回答

1

当你点击链接,而不是通过加载它在浏览器中的URL,您的路由模型函数不会被调用,而是链​​接中提供的模型直接传递给setupController。也许你在那里做些奇怪的事情。

这里有一个模型()在灰烬代码注释:

Note that for routes with dynamic segments, this hook is only 
executed when entered via the URL. If the route is entered 
through a transition (e.g. when using the `linkTo` Handlebars 
helper), then a model context is already provided and this hook 
is not called. Routes without dynamic segments will always 
execute the model hook. 

This hook follows the asynchronous/promise semantics 
described in the documentation for `beforeModel`. In particular, 
if a promise returned from `model` fails, the error will be 
handled by the `error` hook on `Ember.Route`. 
+0

这绝对是我的问题,谢谢。我没有将模型传递给linkTo帮助器,因为每当我尝试添加它时,页面都会弹出。 _Sigh_虽然这是一个单独的问题。我大概可以自己弄清楚。谢谢。 – GJK