我有这样的嵌套路线,
App.Router.map(function() {
this.resource('twod', function() {
this.resource('twoduser', {
path : '/:user_id'
});
});
this.resource('threed');
});
我有一个由{{link-to}}
生成的列表,每当我点击其中任何一个时,它会显示'twoduser'的模板,这就是我打算做的,好的,它会更新网址此,
http://ember.local/#/twod/2
之前,
http://ankur.local/#/twod
每当我刷新网址时,页面都会变成空白,我会在控制台上看到它,
Error while loading route:
TypeError: App.Twod.findBy is not a function
这是twoduser的路由方法:
App.TwoduserRoute = Ember.Route.extend({
model: function(params){
return App.Twod.findBy('id', params.user_id);
}
});
还有一点要注意的是我使用Ajax获取数据,
App.Twod.reopenClass({
findAll : function() {
return new Ember.RSVP.Promise(function(resolve, reject) {
$.getJSON("http://pioneerdev.us/users/index", function(data) {
var result = data.users.map(function(row) {
return App.Twod.create(row);
});
resolve(result);
}).fail(reject);
});
}
});
我可以做些什么来解决这个问题?
答案 0 :(得分:2)
如果要使用它,则必须定义findBy
函数。当你点击时,而不是你重新加载时,它工作的原因是你的link-to
隐含地假设当前对象是新路由的model
,它只是直接传递到控制器,绕过model
挂钩。当你点击重新加载时,model
挂钩被执行,但是因为你试图调用一个不存在的方法而失败。
如果您需要findBy
功能,则应在模型中,findAll
方法旁边创建它。
App.Twod.reopenClass({
findAll : function() {
// your find all function here
},
findBy : function(id){
// your code to find by id here
}
});
答案 1 :(得分:0)
我有类似的错误。我在嵌套资源中刷新,例如/users/1/photos/2
但我没有api端点,例如/api/photos/1
来请求单个照片对象。我添加了一个端点,它自行修复。在修复之前,我试图依赖于/api/users/
中的索引api端点或侧载,但正如Jeremy所解释的那样,当你刷新时,它将最深的路径作为模型并基于该模型进行查找。
希望这有助于某人!