简体   繁体   English

从渲染助手中设置后,把手模板不尊重视图成员

[英]Handlebars template not respecting view member when set from render helper

I have a form that can filled out on its own or embedded into another form, as part of a larger object. 我有一个可以单独填写的表格,也可以作为更大对象的一部分嵌入到另一个表格中。 When I {{render}} the form from the containing handlebars template, the child template does not respect the observable on the view. 当我{{render}}来自包含的车把模板的表单时,子模板不尊重视图上的可观察对象。

Parent template: 父模板:

{{render "EditIntro" introModule embedded=true}}

Where introModule is a property on the containers model which returns a the specific submodel for the intro, which is a part of the parent. 其中introModule是容器模型上的一个属性,该属性返回该intro的特定子模型,该子模型是父级的一部分。

Child View: 子视图:

App.EditIntroView = Ember.View.extend({
  embedded: false,
  isEmbedded: function() {
    return !!this.get('embedded');
  }.property('embedded'),

  templateName: 'intros/edit_intro',
  // etc.

Child Template (relevant part): 子模板(相关部分):

{{! If this form is embedded, user must use the save button for the parent }}
  {{#unless isEmbedded}}
  <button type="submit" class="btn btn-success">
    <span class="glyphicon glyphicon-save"></span>&nbsp;Save
  </button>
  {{/unless}}

I can see the property in the Ember Inspector Chrome plugin, where it is shown to be boolean true . 我可以在Ember Inspector Chrome插件中看到该属性,该属性显示为boolean true I can set a breakpoint on the isEmbedded function and see that it does not get called when the child template renders, but it does get called when I crack open the Ember Inspector or when I use the Inspector to change the value manually. 我可以在isEmbedded函数上设置一个断点,并看到子模板渲染时不会调用它,但是当我打开Ember Inspector或使用Inspector手动更改值时,它确实会被调用。 Finally, if I set the default in the EditIntroView to embedded: true , then the button is hidden like I expect. 最后,如果我在EditIntroView中将默认值设置为embedded: true ,则该按钮将像我期望的那样被隐藏。

So how can I get the child view to respect a simple parameter that has been set from another template's {{render}} call? 那么,如何才能使子视图尊重通过另一个模板的{{render}}调用设置的简单参数?

The template apparently doesn't have properties which are only defined on the view as part of its context. 模板显然没有仅在视图中作为其上下文的一部分定义的属性。 Since the isEmbedded property is only on the view and not the model or controller, it needs to be prefaced with view. 由于isEmbedded属性仅在视图上,而不在模型或控制器上,因此需要在view. . So the working code fix is as simple as: 因此,工作代码修复很简单:

{{#unless view.isEmbedded}}

And that's it. 就是这样。

Found the answer in this Ember question about class views vs instance views . 这个关于类视图与实例视图的Ember问题中找到了答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM