gpt4 book ai didi

routes - model 和 setupController 钩子(Hook)与未定义的模型

转载 作者:行者123 更新时间:2023-12-02 05:41:05 24 4
gpt4 key购买 nike

我的 route 有一个 setupControllermodel Hook 。 model.find(param) 保存对 Rails 后端的 ajax 调用。

发生的情况是我的模型钩子(Hook)触发并调用后端。但在后端应答和成功回调实际随模型返回之前,setupController 钩子(Hook)会触发尚未定义的模型。

最终结果是,虽然模型本身通过绑定(bind)系统进行更新,但需要在 setupController 中进行的附加设置不起作用。我感觉我在某种程度上将模型的内容设置错误,但无法弄清楚。

只有当我使用后退和前进按钮进行导航时,才会发生这种情况。如果我通过 #linkTo 进入路线并指定对象,一切都会按预期工作。

我的代码如下,如果有人有一个想法那就太好了。

App.ProjectsEditRoute = Ember.Route.extend({
model: function(params) {
return App.Project.find(params.project_id);
},

setupController: function(controller, model) {
this.controllerFor('activedataset.index').set('content', App.ActiveDataSet.findAll(model.id));
}
});

App.Project = Ember.Object.extend({
id: '',
name: ''
});

App.Project.reopenClass({
findAll: function() {
var result = Ember.ArrayProxy.create({content: []});
var self = this;
$.ajax({
url: '/projects.json',
type: 'GET',
data: {'user_id': App.currentUser.id},
success: function(data, textStatus, xhr) {
result.set('content', data.projects);
},
error: function(xhr, textStatus, errorThrown) {
alert('error');
}
});

return result;
},

find: function(project_id) {
var result = Ember.Object.create({content: null});
var self = this;
$.ajax({
url: '/projects/' + project_id + '.json',
type: 'GET',
success: function(data, textStatus, xhr) {
result.setProperties(data.project);
},
error: function(xhr, textStatus, errorThrown) {
alert('not found');
}
});

return result;
}
});

根据 Mike 提供的解决方案进行编辑:

App.ProjectsEditRoute = Ember.Route.extend({
model: function(params) {
var record = App.Project.find(params.project_id);
var promise = Ember.Deferred.create();
record.addObserver('isLoaded', function() {
promise.resolve(record);
});

return promise;
},

setupController: function(controller, model) {
this.controllerFor('activedataset.index').set('content', App.ActiveDataSet.findAll(model.id));
}
});

App.Project.reopenClass({
find: function(project_id) {
var result = Ember.Object.create({content: null, isLoaded: false});
var self = this;
$.ajax({
url: '/projects/' + project_id + '.json',
type: 'GET',
success: function(data, textStatus, xhr) {
result.setProperties(data.project);
result.set('isLoaded', true);
},
error: function(xhr, textStatus, errorThrown) {
alert('not found');
}
});

return result;
}
});

最佳答案

I have the feeling that I'm somehow setting the content of the model wrong, but can't figure it out.

你的做法本身并没有错。如果您需要 ember 路由器等待模型 ajax 完成,然后再继续 setupController Hook ,您模型的 find() 应该返回一个 promise 。如果路由的模型钩子(Hook)返回一个 promise ,ember 路由器将等待 promise 解决后再继续。

默认情况下,当您调用 find({}) 时,ember-data 的模型对象会返回一个 Promise。对您的模型执行相同操作应该很容易,请参阅此 Ember initializing route model with query获取延迟模式的示例。

This only happens by the way if I use the back and forward button to navigate. If I go in the route through an #linkTo with the object specified everything works as expected.

有道理。当您使用#linkTo 时,数据已加载。

关于routes - model 和 setupController 钩子(Hook)与未定义的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15508471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com