如何在 Ember.js 中为路由生成 url [英] How to generate url for a route in Ember.js

查看:22
本文介绍了如何在 Ember.js 中为路由生成 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何为给定的路由生成 url.

I am wondering how is possible to generate url for a given route.

我有通话列表(数据库实体),用户可以选择多个通话并通过电子邮件与其他人分享.

I have list of calls (db entity) and user can select several calls and share them with other people via email.

提交选定的调用后,会创建带有散列的数据库行,并按关系包含选定的调用.现在我需要生成可以通过电子邮件发送的链接.此链接与呼叫路线列表的路线不同.

After submition of selected calls is created db row with hash and by relation contains selected calls. Now I need generate link which can be sended by e-mail. This link is not the same route as list of call's route.

那么问题是:是否可以在 Ember.js 中通过路由和参数生成 url?谢谢.

推荐答案

您可以使用 Router#generate 委托给 router.js 库.

App = Ember.Application.create();

App.Router.map(function() {
  this.resource('post', { path: '/posts/:post_id' }, function(){
    this.route('edit');
  });
});

App.Post = Ember.Object.extend();

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return [
      App.Post.create({
        id: 5,
        title: 'I am post 5'
      }),
      App.Post.create({
        id: 6,
        title: 'I am post 6'
      }),
      App.Post.create({
        id: 7,
        title: 'I am post 7'
      })];
  },
  actions: {
    showUrl: function(post) {
      alert(this.router.generate('post.edit', post));
    }
  }
});

Ember 1.3 示例

App = Ember.Application.create();

App.Router.map(function() {
  this.resource('post', { path: '/posts/:post_id' }, function(){
    this.route('edit');
  });
});

App.Post = Ember.Object.extend();

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return [
      App.Post.create({
        id: 5,
        title: 'I am post 5'
      }),
      App.Post.create({
        id: 6,
        title: 'I am post 6'
      }),
      App.Post.create({
        id: 7,
        title: 'I am post 7'
      })];
  },
  actions: {
    showUrl: function(post) {
      alert(this.router.generate('post.edit', post));
    }
  }
});

这是 {{#link-to ...}} 助手在幕后使用的内容.

This is what the {{#link-to ...}} helper uses under the hood.

这篇关于如何在 Ember.js 中为路由生成 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆