Ember将模型传递给transitionToRoute [英] Ember passing a model to transitionToRoute

查看:313
本文介绍了Ember将模型传递给transitionToRoute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ember-cli与ember 1.11



我正在尝试在控制器中使用transitionToRoute方法转换到路由并将其动态生成的对象作为模型。



这是我的控制器:

  import Ember从'ember'; 

导出默认Ember.Controller.extend({
actions:{
launch_scanner:function(){
console.log('launch_scanner');
var model = {name:Edward,电话:123,电子邮件:email@test.com};
//最终的应用程序将从QR扫描仪中提取模型变量

this.transitionToRoute('addcontact',model);
}
}
});

当我触发此操作时,transitionToRoute方法会导致此错误:


未捕获的错误:更多的上下文对象被传递,而不是路由的
动态段:addcontact


如果我省略模型参数,它将转换到addcontact路由。我做错了什么?



这是我的路由器文件:

 从ember导入Ember 
从'./config/environment'导入配置;

var Router = Ember.Router.extend({
location:config.locationType
});

Router.map(function(){
this.route('home',{path:'/'});
this.resource('addcontact',{路径:'/ addcontact'});
});

导出默认路由器;


解决方案

您正在处理许多Ember中发生的经典问题应用程序是如何处理新/创建的情况。



您无法定义路线,例如

  this.route 'addcontact',{path:/ addcontact /:thing_id} 

因为新的联系人不有一个id,直到它保持到服务器,直到它不再是新的。



然而,在你的应用程序,在你的情况下,我想在家页面,用户按下新联系人按钮,它可能有关于要创建或如何创建它的有用信息,甚至可能想要正确创建对象使用 this.store.createRecord - 但是如何将该信息或新记录传递给 addcontacdt 路线,可以没有动态段?



一些想法包括:


  1. home 路线或其他任何地方创建新联系人,并将其存储在控制器中,然后在模型中钩子的新路由使用 this.controllerFor('home')获取它。get('newContact')


  2. 将 addcontact route的新对象(如果有)创建为查询参数的必要参数,使用 transitionTo 'newcontact',queryParameters)。然后,在模型钩子中,使用 this.store.createRecord('contact',transition.queryParameters)创建新对象或相当的东西。



I'm using Ember-cli with ember 1.11

I'm trying to use the transitionToRoute method in a controller to transition to a route and feed it a dynamically generated object as the model.

Here's my controller:

import Ember from 'ember';

export default Ember.Controller.extend({
  actions: {      
      launch_scanner: function(){
        console.log('launch_scanner');
        var model = {name: "Edward", phone: "123", email: "email@test.com"};
        //the final app will pull the model variable from a QR scanner

        this.transitionToRoute('addcontact', model);
     }
  }      
});

When I trigger this action, the transitionToRoute method causes this error:

Uncaught Error: More context objects were passed than there are dynamic segments for the route: addcontact

If I leave out the model parameter, it transitions to the addcontact route just fine. What am I doing wrong?

Here is my router file:

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
  this.route('home', {path: '/'});
  this.resource('addcontact', {path: '/addcontact'});
});

export default Router;

解决方案

You are dealing with a classic problem occurring in many Ember applications which is how to handle "new"/"create" situations.

You cannot define a route such as

this.route('addcontact', { path: /addcontact/:thing_id }

because the the new contact doesn't have an id, and won't until it is persisted to the server, at which point it won't be new any more.

Yet there is some point in your application, in your case I suppose on the "home" page, where the user pressed a "New Contact" button, and it may have useful information about what to create or how to create it, and might even want to create the object right there using this.store.createRecord--yet how does one pass that information, or the new record, to the addcontacdt route, which can have no dynamic segment?

Some ideas include:

  1. Create the new contact in your home route or whatever, and store it in the controller. Then, in the model hook of the new route, retrieve it using this.controllerFor('home').get('newContact').

  2. Pass necessary parameters for creating the new object, if any, to the addcontact route as query parameters, using transitionTo('newcontact', queryParameters). Then, in the model hook, create the new object using this.store.createRecord('contact', transition.queryParameters) or something equivalent.

这篇关于Ember将模型传递给transitionToRoute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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