承诺得到履行后,Emberjs会显示数据吗?

时间:2014-09-09 19:17:41

标签: ember.js ember-data ember-cli

我在概念上对这是如何运作有点困惑。

用户在表单上输入数据,然后按下。控制器的nextStep动作触发,我要求模型从服务器获取数据。当数据到达时,我转换到结果路线。此时,我将路径存储在控制器上的变量上的先前模型。然后我想迭代模板上的数据。

问题是什么都没有出现。

这就是我所拥有的(在适当的文件中):

//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
      }
   ]
}

2 个答案:

答案 0 :(得分:0)

找到它,如果我改变

controller.set('businessmatches', this.store.find('businessmatch'));

controller.set('businessmatches', this.store.all('businessmatch')); 它按预期工作。

有兴趣知道为什么,如果有人可以解释。

答案 1 :(得分:0)

@tstirrat是正确的,store.all()方法immediately returns an array of cached records,而store.find()方法returns a promise of fetched records

您是否有特定原因在路线中使用模型挂钩?他们承诺知道,并等待你的store.find()解决然后将模型传递给你的setupController。然后,您可以使用modelFor('step1')引用之前的模型,或再次使用store.all()