{{action}} 与使用关系 id 的transitionTo 链接

2024-04-05

给定一个具有如下上下文的视图{ id: 1, form_id: 5},我想创建一个{{action}}使用链接到表格form_id.

我的视图代码如下所示:

<script type="text/x-handlebars" data-template-name="group">
  {{action showForm form_id href=true}}
</script>

我的路由器中的操作如下所示:

showForm: function(router, event) {
  var form_id = event.context;
  router.transitionTo('root.form', { id: form_id });
},

我收到一条错误,内容如下:

Uncaught Error: assertion failed: You must specify a target state for event 'showForm' in order to link to it in the current state 'root.index'.

我猜问题出在我设置上下文的方式上transitionTo,但我一直无法找出正确的解决方案。

这是重现问题的完整代码:

<script type="text/x-handlebars" data-template-name="application">
  {{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="group">
  {{action showForm form_id href=true}}
</script>

MyApp = Ember.Application.create({
  autoinit: false
});

MyApp.router = Ember.Router.create({
  root: Ember.Route.extend({
    index: Ember.Route.extend({
      route: '/',

      // Throws error: 
      //    You must specify a target state for event 'showForm' in 
      //    order to link to it in the current state 'root.index'
      //
      showForm: function(router, event) {
        var form_id = event.context;
        router.transitionTo('root.form', { id: form_id });
      },

      // Won't work because form deserialize finds id, not form_id 
      //showForm: Em.Route.transitionTo('root.form'),

      // This won't work either
      // showForm: Em.Route.transitionTo('root.form', { id: this.form_id }),        

      connectOutlets: function( router, context ){
        var group = Em.Object.create({ id:1, form_id: 5 });
        router.get( 'applicationController' ).connectOutlet( 'group', group );
      }
    }),
    form: Ember.Route.extend({
      route: '/form/:id',
      serialize: function( router, context ){
        return { id: context.id }
      },
      deserialize: function( router, context ){
        var form = Em.Object.create({ id: 5, name: 'my form' });
        return MyApp.Form.find( context.id );
      },
      connectOutlets: function( router, context ){
        // left out for fiddle example 
      }
    })
  })
});

MyApp.ApplicationController = Ember.Controller.extend({});

MyApp.GroupController = Em.ObjectController.extend({});
MyApp.GroupView = Em.View.extend({ templateName:  'group' });

MyApp.initialize(MyApp.router);​

以及相应的小提琴:

http://jsfiddle.net/jefflab/LJGCz/ http://jsfiddle.net/jefflab/LJGCz/


我能够使用计算属性作为我的操作的上下文来想出一个丑陋的解决方案来解决这个问题。关键片段是:

<script type="text/x-handlebars" data-template-name="group">
  <a {{action showForm view.formHash href=true}}>go to form</a>
</script>

MyApp.GroupView = Em.View.extend({ 
  templateName:  'group',
  formHash: function() {
    return { id: this.get('controller').get('form_id') };
  }.property('form_id')
});

工作小提琴在这里:

http://jsfiddle.net/jefflab/pGv8u/ http://jsfiddle.net/jefflab/pGv8u/

然而,在与 Tom Dale 交谈后,很明显解决这个问题的“正确方法”是使用物化对象而不是 id 值。如果您使用 Ember 数据,那么这是“旁加载”belongsTo 功能的一个很好的用例。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

{{action}} 与使用关系 id 的transitionTo 链接 的相关文章

随机推荐