gpt4 book ai didi

javascript - ember.js - 使用可选参数和默认模型定义路由

转载 作者:可可西里 更新时间:2023-11-01 02:39:35 27 4
gpt4 key购买 nike

我想在 emberjs 中定义一个路由,它有一个可选参数例如:

/视频/video/123

如果没有提供参数,我想使用默认模型/夹具。如果提供了参数,那么我显然想使用该参数查找模型。

如果我然后转到不同的路线,并返回到没有参数的路线,我想使用之前加载的模型。

例如:

启动应用

/video - 显示我的默认/夹具模型

/video/123 - 显示模型 123

/another-route - 显示新路线

/video - 显示模型 123

这可能吗?

最佳答案

我最终使用了不同的解决方案:

  this.resource('video', function() {
this.route('index', {path: '/'});
this.route('item', {path: ':id'});
});

这些路由支持:

/video - 显示我的默认/夹具模型

/video/123 - 显示模型 123

当用户访问/video时,VideoIndexRoute必须重定向到没有任何id的VideoItemRoute。

var VideoIndexRoute = Em.Route.extend({

afterModel: function() {

// this is the tricky part
this.replaceWith('video.item', '');

}

});

现在,VideoItemRoute 必须检查是否有关联的任何模型,如果缺少,则应使用默认灯具或新灯具。

var VideoItemRoute = Em.Route.extend({

model: function(param) {
if (param.id) {
return this.store.find('video', param.id);
}
},

setupController: function (controller, model) {
if (!model) {
model = this.store.createRecord('video',{
name: 'default Name'
});
// or use fixture...
}
this._super(controller, model);

}

});

关于javascript - ember.js - 使用可选参数和默认模型定义路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19991135/

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