Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

javascript - How to generate url for a route in Ember.js

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

My scenario

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.

So the question is: Is it possible to generate url by route and params in Ember.js? Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use Router#generate which delegates to the router.js library.

Ember 2.5 Example

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 Example

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));
    }
  }
});

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...