Handlebars template not respecting view member when set from render helper

喜你入骨 提交于 2019-12-25 02:19:56

问题


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.

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.

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. 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. Finally, if I set the default in the EditIntroView to embedded: true, then the button is hidden like I expect.

So how can I get the child view to respect a simple parameter that has been set from another template's {{render}} call?


回答1:


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.. 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.



来源:https://stackoverflow.com/questions/22697082/handlebars-template-not-respecting-view-member-when-set-from-render-helper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!