Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 承诺兑现后是否显示数据?_Ember.js_Ember Data_Ember Cli - Fatal编程技术网

Ember.js 承诺兑现后是否显示数据?

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('

我在概念上有点搞不清楚这是怎么回事

用户在表单上输入数据,然后按next。控制器的
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
      }
   ]
}