使用可选参数和默认模型定义路由
define route with optional parameter and default model
我想在 emberjs 中定义一个路由,它有一个可选参数例如:
/video
和 /video/123
如果没有提供参数,我想使用默认模型/夹具。如果提供了参数,那么我想显然使用该参数查找模型。
如果我随后转到其他路由,并返回到没有参数的路由,则我想使用先前加载的模型。
例如:
startup app
/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必须重定向到VideoItemRoute,没有任何id。
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);
}
});
有一种干净的方法可以做到这一点,尽管它有点"棘手"。这个想法是使用嵌套路由来保存 id,但不渲染它,而是让父路由负责使用渲染帮助程序渲染它。当你这样做时,所有的逻辑都可以存在于VideoChoiceController中,它将用于显示默认视频或特定视频。这样做时,无需显式"记住"最后一个视频,路由引擎表示的状态机会为您执行此操作。
App.Router.map(function) {
this.resource('video', function(){
this.route('choice', {path: ':video_id'});
});
});
App.VideoRoute = Ember.Route.extend({
model: function(params) {
return App.get('defaultVideo');
},
setupController: function(controller, model) {
var video_choice = this.controllerFor('video.choice')
// this is necessary if you want, for example,
// to display the name or a link to the default video
// when a specific video is being displayed
controller.set('model', model);
if(Ember.isEmpty(video_choice.get('model'))){
video_choice.set('model', model);
}
}
});
App.VideoChoiceRoute = Ember.Route.extend({
model: function(params) {
return this.get('store').find('video', params.video_id);
},
renderTemplate: function() {
// if you don't override renderTemplate to do nothing,
// everything will still work but you will get an assertion
// error that render should only be used once with a
// singleton controller
}
});
<script type="text/x-handlebars">
<div>
{{outlet}}
</div>
</script>
<script type="text/x-handlebars" data-template-name='video'>
<div> attrributes here always refer to the default video: {{name}} </div>
{{render "video.choice"}}
</script>
<script type="text/x-handlebars" data-template-name='video'>
<div>
attrributes here always refer to specific or last video,
or default if a specific video has never been loaded: {{name}}
</div>
</script>
当然,你必须做一些有点时髦的事情,比如将最后一个视频存储在某个全局变量中,但这取决于你。
http://emberjs.jsbin.com/uhoQozu/1/edit
http://emberjs.jsbin.com/uhoQozu/1#/video
http://emberjs.jsbin.com/uhoQozu/1#/video/32
App.Router.map(function() {
this.resource('videoModel', {path:'/video/:video_id'});
this.resource('video'); // this resource can be accessed at /video
});
- 骨干模型默认值-todos.js示例中不必要的代码
- 当显式定义控制器参数时,默认模型绑定器会发生异常
- 如何创建具有默认值的JavaScript对象字段?(AngularJS模型相关)
- Angular.js默认选中不'不适用于ng模型
- 为 getter 提供默认值,而不会在 Angular 2 模型类中导致堆栈溢出
- 主干模型默认值不会被模型数据覆盖
- 主干类模型未继承默认值
- 默认情况下,一个模型属性等于 sail.js 模型中的另一个模型属性
- Jquery OOP 模型阻止默认然后调用基处理程序
- 使用可选参数和默认模型定义路由
- 为什么我的模型中的默认属性返回未定义
- 设置主干.js模型默认使用函数
- 如何初始化没有默认值的 BackboneJS 模型
- 如果属性值为 null,则使用主干模型默认值
- 在 YUI3 模型中,有没有办法重置为默认值
- 默认情况下,如何将ng模型值设置为空字符串
- 当文本区域绑定到模型时,angularjs中的默认文本区域值
- 如何设置具有可选ID的路由以进行查找,以及回退默认模型
- 如何在Backbone.js中使用set方法更改默认模型属性
- 创建用于淘汰的默认模型