Skip to content

Instantly share code, notes, and snippets.

@workmanw
Created July 3, 2012 19:44
Show Gist options
  • Save workmanw/3042409 to your computer and use it in GitHub Desktop.
Save workmanw/3042409 to your computer and use it in GitHub Desktop.
Feedback on Ember `VIEW_PRESERVES_CONTEXT`

After a mind numbing week of updating all of the templates in our app and thinking about the implications of this change, I'd like to provide a little bit of feedback on this change.

Overall, I view this change as unfavorable. It's not that I dislike the change, it's that I don't find much value in it and it was a very daunting task to push to Ember users.

Template Simplification

From what I've gathered the purpose of this change is to "significantly reduce the verbosity of templates". I did not find that my templates either grew or shrank in size, they really remained constant. I believe one of the intended ways for reducing size was the elimination for the need of contentBinding="this" on {{view}} helpers. E.g.

{{#each App.photosController}}
  Photo Title: {{title}}
  {{#view App.InfoView}}
    {{date}}
    {{cameraType}}
    {{view.otherViewProperty}}
  {{/view}}
{{/each}}

However, it seems that the entire reason you would implement App.InfoView in the first place would be to add a bit of functional logic to your application. If this is true, App.InfoView likely needs a handle on the model (content) to preform it's actions. This is the case in our application and if this holds true for others, then you really just switching from content.xyz to view.abc. E.g.

App.InfoView = Ember.View.extend({
  otherViewProperty: function() {
    return (this.getPath('content.xyz') % 2 == 0) ? 'even' : 'odd';
  }.property('content')
});
// Before:
{{#each App.photosController}}
  Photo Title: {{title}}
  {{#view App.InfoView contentBinding="this"}}
    {{content.date}}
    {{content.cameraType}}
    {{otherViewProperty}}
  {{/view}}
{{/each}}

// After:
{{#each App.photosController}}
  Photo Title: {{title}}
  {{#view App.InfoView contentBinding="this"}}
    {{date}}
    {{cameraType}}
    {{view.otherViewProperty}}
  {{/view}}
{{/each}}

Notice that both before and after have a contentBinding because App.InfoView requires content for it's computed properties.

###Context Chain

I've found that this change introduces differences in the context chain between Ember.Views and Handlebars. Now I realize that's the point here because one of Ember's mission statements is to not be exclusively handlebars. But up until this point binding values within a template and a view were interchangeable. That is now no longer the case. For example, if I build a view with Ember.TargetActionSupport and wish to target the containing view's content for an action, the target value must be parentView.content. While if I use the {{action}} helper, the target value must be view.content.

This in it's self isn't too bad, but it feels like the first step in a bad direction.

###Action targets

This change makes it impossible in some cases to have models become the target of an action because the shift to focus on context.

// Before:
{{#each App.usersController}}
  {{#view App.MyView contentBinding="this"}}
    <a {{action "updateUser" target="content"}}>Update "{{content.userName}}"</a>
  {{/view}}
{{/each}}

// After:
{{#each App.usersController}}
  {{#view App.MyView}}
    <!-- Target cannot be view.this or this -->
    <a {{action "updateUser" target="???"}}>Update "{{userName}}"</a>
  {{/view}}
{{/each}}

With this shifting focus to context it would really be nice if the action helped supported target="this".

Conclusion

The purpose of this write up to provide feedback and impressions from a medium to large size ember application. I hope these comments / concerns were fair and unbiased. It is not my intent to endlessly complain about changes, but to help illustrate how these changes have impacted our application.

-- Wesley

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment