Ember.js 承诺兑现后是否显示数据?
我在概念上有点搞不清楚这是怎么回事 用户在表单上输入数据,然后按next。控制器的Ember.js 承诺兑现后是否显示数据?,ember.js,ember-data,ember-cli,Ember.js,Ember Data,Ember Cli,我在概念上有点搞不清楚这是怎么回事 用户在表单上输入数据,然后按next。控制器的nextStep操作触发,我请求模型从服务器获取数据。当数据到达时,我转换到结果路径。在这一点上,我让路由将以前的模型存储在控制器上的一个变量上。然后我想遍历模板上的数据 问题是什么都没有出现 这是我拥有的(在适当的文件中): 找到了,如果我换的话 controller.set('businessmatches',this.store.find('businessmatch') 到 controller.set('
nextStep
操作触发,我请求模型从服务器获取数据。当数据到达时,我转换到结果路径。在这一点上,我让路由将以前的模型存储在控制器上的一个变量上。然后我想遍历模板上的数据
问题是什么都没有出现
这是我拥有的(在适当的文件中):
找到了,如果我换的话
controller.set('businessmatches',this.store.find('businessmatch')代码>
到
controller.set('businessmatches',this.store.all('businessmatch')代码>
它按预期工作
如果有人能解释的话,我想知道为什么。@tstirrat是正确的,store.all()
方法,而store.find()方法是正确的
在路线中不使用模型挂钩有什么具体原因吗?他们知道承诺,将等待您的store.find()
解决问题,然后将模型传递给您的setupController。然后,您可以使用modelFor('step1')
引用以前的模型,或者再次使用store.all()
。this.store.all('businessmatch')
是一个同步操作。它只返回存储中已存在的缓存副本。如果您想让它实际查询api,您需要像承诺一样使用它:this.store.find('businessmatch')。然后(函数(结果){//set controller property here})
@tstirrat问题是,如果我在不等待承诺兑现的情况下执行transitionroute
,则使用.all()
),页面将加载为空,随后将填充值。我原以为find()
也会发生同样的情况,但事实并非如此。非常感谢您的回答!通过模型钩子,您的意思是model:function(){返回this.store.find('businessmatch')}
?我假设setupController
,这是等效的,唯一的区别是,在后者中,我可以指定属性的名称,而前者是model
。假设的内容有这么多:|为了记录在案,我不止一次地测试了all()
,如果转换设置在承诺的部分之外,那么在收到数据时更新模板RecordArray
有一个update
方法,因此可能会发生一些神奇的事情?是的,路由有beforeModel
、model
和afterModel
,如中所述setupController
在afterModel
之后立即发生,并且在继续之前不会等待承诺解决。
//Step 1 Controller (partial - nextStep action)
var businessmatch = this.store.find('businessmatch', {businessname: businessname, phonenumber: phonenumber, zipcode: zipcode})
.then(function (result) {
// The model has data at this point
controller.transitionToRoute('step2');
});
// step2 Router
import Ember from 'ember';
export default Ember.Route.extend({
setupController: function(controller, model) {
controller.set('businessmatches', this.store.find('businessmatch'));
}
});
// Step2 template (partial)
{{#each businessmatches}}
{{businessname}}
{{/each}}
// Model
import DS from 'ember-data';
var Businessmatch = DS.Model.extend({
businessname: DS.attr('string'),
phonenumber: DS.attr('string'),
address: DS.attr('string'),
pageurl: DS.attr('string'),
thubmurl: DS.attr('string')
});
export default Businessmatch;
// Sample response from server
{
"businessmatches":[
{
"businessname":"El Farolito",
"pageurl":"/biz/el-farolito-san-francisco-2",
"thumburl":"/bphoto/ohpxQWg-hB9Sb27HkVg-yQ/90s.jpg",
"address":"780 El Camino RealMillbrae, CA 94030",
"phonenumber":"(650) 583-0487",
"id":1
},
{
"businessname":"El Farolito",
"pageurl":"/biz/el-farolito-san-francisco-4",
"thumburl":"/photo/AW76YTovuu9YsO69_BcLKQ/30s.jpg",
"address":"2779 Mission StSan Francisco, CA 94110",
"phonenumber":"(415) 824-7877",
"id":2
},
{
"businessname":"El Farolito",
"pageurl":"/biz/el-farolito-san-francisco",
"thumburl":"/bphoto/LgTOTIicRY6XArigmPhBpw/90s.jpg",
"address":"2950 24th StSan Francisco, CA 94110",
"phonenumber":"(415) 641-0758",
"id":3
}
]
}