从模板访问模型 [英] accessing the model from the template

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

问题描述

使用ember播放,我发现有时模型存储在控制器的内容属性中,有时该模型也可以在控制器上直接使用。我不明白,在这种情况下。

Playing around with ember, I found that sometimes the model is stored on the controller's content property, sometimes the model is directly available on the controller as well. I do not understand however, when this is the case.

让我通过一个例子来解释,我在组装我的ember MVC时发现。

Let me explain it by an example which I found when assembling my ember MVC.

设置A - 开始


  • 我定义了一个自定义成员对象,对应的 MemberRoute MemberView 类和一个名称为构件

  • 成员对象具有某些属性,例如 id 昵称等。

  • 注意:没有控件的形式 MemberController 被定义,因此根据ember的约定,它自己提供控制器。

  • I defined a custom Member object, corresponding MemberRoute, MemberView classes and a template with the name member.
  • The Member object had some attributes such as id, nickname, etc.
  • NOTE: no controller of the form MemberController was defined, thus by ember's convention, it provides the controller on its own.

设置B - 自定义


  • 与设置A相同,但现在有一个 MemberController 定义为包含从模板中触发的一些操作方法。

  • Same as setup A, but now there is a MemberController defined that contains some action methods that are triggered from within the template.

奇怪的行为我不完全明白)


  • 在设置A中,我可以参考会员的属性直接与 {{id}} {{nickname}}

  • 在设置B中,我必须使用 {{content.id}} {{content.nickname}}

  • in setup A, I can refer to the Member's attributes directly with {{id}} or {{nickname}}.
  • in setup B, I have to use {{content.id}} or {{content.nickname}}

如ember文档中所述, MemberView

As documented in ember's documentation, MemberView does

setupController : function(controller, member) {
    controller.set('content', member);
},

所以,有人可以帮我了解为什么差异和差异是什么?目前,我的猜测将是

So, could somebody help me to understand why the difference and where the difference is? Currently, my guess would be either


  • 模板的上下文是不同的(可能在设置中缺少代码片段控制器?)


  • 自动提供的默认控制器,具有一些额外的魔法,不能直接用于自定义控制器。

任何帮助了解这个高度赞赏已经花了我一段时间才能到达这一点。我第一次认为可能是由requireJS进行的项目设置引入的模块化(我仍然认为可能会有影响)。 Ember是v1.0pre4。

Any help to understand this is highly appreciated. It already took my quite a while to come as far as this. I first thought it could be the modularization introduced by the project setup with requireJS (well, I still think that could have a influence). Ember is v1.0pre4.

提前感谢!
Patrick

Thanks in advance! Patrick

推荐答案


所以,有人可以帮我了解为什么差异和哪里区别是目前,我的猜测是
,模板的上下文是不同的(可能在控制器的设置中缺少代码片段)

提供的默认控制器由ember自动,有一些额外的魔法,不是直接可用于定制的控制器。

So, could somebody help me to understand why the difference and where the difference is? Currently, my guess would be either that the context of the template is different (possibly there is a code piece missing in the setup of the controller?) or the default controller that is provided by ember automatically, has some additional magic that is not directly avaiable for customized controllers.

很难说,确实没有看到你的代码,但我最好的猜测是,您的 MemberController 扩展了 Ember.Controller 。 ember(在这种情况下)提供的默认值将是一个 Ember.ObjectController 。如果这是您想要的,请将您的MemberController定义更改为:

It's hard to say for sure without seeing your code, but my best guess is that your MemberController extends Ember.Controller. The default provided by ember (in this scenario) would have been an Ember.ObjectController. If that's what you want, change your MemberController definition to:

App.MemberController = Ember.ObjectController.extend({
  myProperty: 'value'
});

一个objectController充当代理它的内容属性,通常这是一个ember模型。所以如果事情正确连线,你永远不需要通过'content`属性访问模型。如果你曾经看到过:

An objectController acts as a proxy to it's content property, typically that is an ember model. So if things are wired up correctly you should never need to access a model via the 'content` property. If you ever see something like:

{{content.id}} or {{content.nickname}}

这是一个您应该更改为ObjectController的标志。请参阅 EMBER指南:代表单一模型!更详细的解释。

it's a sign that you should change to an ObjectController. See EMBER GUIDES: REPRESENTING A SINGLE MODEL! for a more detailed explanation.

这篇关于从模板访问模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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