gpt4 book ai didi

javascript - Ember 不调用路由器的 setupController

转载 作者:行者123 更新时间:2023-11-29 21:04:10 26 4
gpt4 key购买 nike

因此,我的 route 有两条路径。我将这两条路线创建为 doc recommends .

我的路由器如下:

// router.js
Router.map(function() {
this.route('photos');
this.route('photo', { path: '/photo/:photo_id' });
});

如果我先访问路径/photo/ID,然后访问/photos,它只会在后者上显示一个对象。 (错误)

如果我先访问/photos,它会显示所有对象,稍后我可以访问/photo/ID,这样就没问题了。 ()

我想让它双向工作。这个怎么做?您可以在下面看到我为每条路线编写的代码:

// photo.js
export default Ember.Route.extend({
model(params) {
return this.get('store').findRecord('photo', params.photo_id);
}
});

// photos.js
export default Ember.Route.extend({
setupController(controller, model) {
let photos = this.get('store').findAll('photo');
console.log('add all...');
// convert to an array so I can modify it later
photos.then(()=> {
controller.set('photos', photos.toArray());
});
},
});

无论用户走到哪里,我总是可以调用 findAll() 函数,但我认为这并不聪明。

我处理页面转换的方式:

转到我使用的照片:

{{#link-to 'photos'}}All{{/link-to}}

为了转到/photo/ID,我注入(inject)了“-routing”服务,我在一个事件中使用了这样的点击:

routing: Ember.inject.service('-routing'),
actions() {
selectRow(row) {
this.get("routing").transitionTo('photo', [row.get('id')]);
}
}

最佳答案

findAll 将从商店获取它并立即返回,稍后它将请求服务器并更新商店。但在你的情况下,因为你没有使用路由模型 Hook ,所以这个实时数组不会更新,所以它不会反射(reflect)在模板中。

If I visit firstly the route /photo/ID and then go to /photos, it will only show one object on the latter.

在上述情况下,store 将只包含一个 reocrd,因此当您使用 findAll 请求 store 数据时,它将返回现有的单个记录。

另一种选择是,
避免这个 photos.toArray() - 它会破坏实时阵列更新,我不确定你为什么在这里需要它。因为 photosDS.RecordArray

Note: It's important to note that DS.RecordArray is not a JavaScript array, it's an object that implements Ember.Enumerable. This is important because, for example, if you want to retrieve records by index, the [] notation will not work--you'll have to use objectAt(index) instead.

关于javascript - Ember 不调用路由器的 setupController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44891740/

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