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

标签 javascript ember.js

我想在 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/

相关文章:

javascript - 在其他 php 文件中使用 Javascript 显示表单结果

javascript - 使用 javascript 禁用与网站的连接

javascript - 纯方式修改JS对象

用于符号而非空格的 Javascript 正则表达式

google-analytics - Ember.js 和谷歌分析

javascript - 如何在一对多关系中创建对象

ember.js - 如何重写 Ember Data 1.0.0 中的属性 setter ?

javascript - React 组件生命周期 API 请求

javascript - 如何等到 find 方法完成后再在 Ember 模型中进行进一步处理

ember.js - 如何在 ember-cli Brocfile.js 中执行构建后 Hook ?