Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.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
Javascript 设置多个控制器型号时,ApplicationRoute中的Ember.js返回承诺_Javascript_Php_Ember.js - Fatal编程技术网

Javascript 设置多个控制器型号时,ApplicationRoute中的Ember.js返回承诺

Javascript 设置多个控制器型号时,ApplicationRoute中的Ember.js返回承诺,javascript,php,ember.js,Javascript,Php,Ember.js,我需要在applicationRoutemodels挂钩中设置多个控制器型号: model: function() { this.controllerFor('markets').set('model', this.store.pushPayload('market', marketsCache)); this.controllerFor('pages').set('model', this.store.pushPayload('page', pagesCache));

我需要在
applicationRoute
models挂钩中设置多个控制器型号:

model: function() {    
    this.controllerFor('markets').set('model', this.store.pushPayload('market', marketsCache)); 
    this.controllerFor('pages').set('model', this.store.pushPayload('page', pagesCache)); 
    this.controllerFor('categories').set('model', this.store.pushPayload('category', categoriesCache)); 
    this.controllerFor('employees').set('model', this.store.pushPayload('employee', employeesCache));
}
在我的webservers index.php文件中,我设置了javascript变量
marketsCache
pagesCache
categoriesCache
,以及
employeesCache
。它们是从APC缓存中检索的,因为API是查询密集型的


如您所见,我希望应用程序等待模型完全填充。然而,据我所知,承诺只适用于AJAX请求。因此,问题是,有没有可能将这些
控制器的
行包装成一个承诺?

您可以使用jQuery Deferred/在以下情况下完成此任务:

var market = this.store.pushPayload('market', marketsCache);
this.controllerFor('markets').set('model', market); 

var page = this.store.pushPayload('page', pagesCache);
this.controllerFor('pages').set('model', page); 

var category = this.store.pushPayload('category', categoriesCache);
this.controllerFor('categories').set('model', category); 

var employee = this.store.pushPayload('employee', employeesCache);
this.controllerFor('employees').set('model', employee);

return $.when(market, page, category, employee);

文档:

pushPayload
根本不返回承诺或模型,但您可以推送负载,然后调用
all
,返回存储中该类型的所有记录,然后您可以将其分配给控制器

model: function() {    
    this.store.pushPayload('market', marketsCache);
    this.store.pushPayload('page', pagesCache)
    ....

    var market = this.store.all('market'),
        pages = this.store.all('page');
    ....

    this.controllerFor('markets').set('model', model.market); 
    this.controllerFor('pages').set('model', model.pages); 
    ....
}

pushPayload没有返回承诺。@kingpin2k的确,我编辑了我的代码和
$。当
不起作用时。根据您的回答,我还创建了一个带有
RSVP.hash
的版本:。不过,这现在也起作用了,因为pushPayload不会返回承诺。谢谢您的评论!但是,当我执行:
this.store.find时,我想会调用REST适配器吗?那真的不是我想要的。因为API将得到模型的另一个调用。对不起,我刚刚考虑过,我意识到你可能不想这样做,你可以忽略hash和setupController,只需使用
all
Hi kingpin2k,我现在疯狂地摆弄我的代码,尝试我脑海中突然出现的一切。但我确信
this.store.all
不会返回承诺。就像文档中说的:我现在就试试你建议的代码!它不会返回承诺,但不是必需的,它会返回一个记录数组。你说得很对,它工作得很完美!我不知道返回一个记录数组就足以返回。