Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

fix(api): use only Renderer2 instances #391

Merged
merged 1 commit into from
Sep 7, 2017
Merged

Conversation

ThomasBurleson
Copy link
Contributor

@ThomasBurleson ThomasBurleson commented Aug 24, 2017

NgClass and NgStyle use Renderer1 instances; which has been deprecated. Instead use Renderer2 and a RendererAdapter.

Thx @mhevery for his solution!

Refs #386. Fixes #398, #408.

@ThomasBurleson
Copy link
Contributor Author

ThomasBurleson commented Aug 24, 2017

@mhevery - would you review to confirm ?

PS Just review the last commit: ce7d828

@ThomasBurleson ThomasBurleson modified the milestone: v2.0.0-beta.10 Aug 28, 2017
@ThomasBurleson
Copy link
Contributor Author

@jelbourn - ready for your review also.

@ThomasBurleson
Copy link
Contributor Author

@crisbeto - would you mind reviewing this one ?

// we only use setElementStyle() and setElementClass()
// ******************************************************************

selectRootElement(): any { }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these methods aren't meant to be used, consider throwing an error or removing the implementation so they fall back to the inherited ones from the Renderer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot remove the implementation... We want to to appear as a Renderer1 instance but built from a Renderer2. Renderer is an abstract class with many abstract methods. This is we have to implement them EVEN if ignored.

But we can throw error as not-implemented!

Fixed.

* Curried value to function to check styles that are inline or in a stylesheet for the
* specified DOM element.
*/
function buildCompareStyleFunction(inlineOnly = true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like these changes are repeated between this PR, #394 and #396. Is this intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ^ is because PR 391 will be rebased after #390 gets merged.

@@ -73,6 +76,14 @@ function hasClass(element: any, className: string): boolean {
return element.classList.contains(className);
}

function hasAttribute(element: any, attributeName: string): boolean {
return element.hasAttribute(attributeName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like most of these DOM utilities are doing much, aside from proxying the native functions with the exact same arguments.

Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@crisbeto crisbeto added pr: lgtm This PR has been approved by the reviewer pr: merge ready This PR is ready for the caretaker to presubmit and merge labels Aug 29, 2017
@jelbourn
Copy link
Member

@ThomasBurleson ping me when this one is rebased

* This is required for older versions of NgStyle and NgClass: which require
* the v1 API (but should use the v2 instances)
*/
export class RendererAdapter extends Renderer {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are still referring to Renderer. I think you should drop that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhevery - To manually construct a NgClass I must pass it an instance of a class that extends Renderer. Or am I missing something ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean change RendererAdapter to HybridAdapter ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole point of the issue is that the Renderer symbol is being removed entirely.

Copy link
Contributor Author

@ThomasBurleson ThomasBurleson Aug 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jelbourn -If we do this, then we break support for Angular 4.x

// Create an instance NgClass Directive instance only if `ngClass=""` has NOT been defined on
    // the same host element; since the responsive variations may be defined...
    if ( !this._ngClassInstance ) {
      let adapter = new RendererAdapter(_renderer);
      this._ngClassInstance = new NgClass(_iterableDiffers, _keyValueDiffers, _ngEl, adapter);
    }

since NgClass in core (v4.3) expects a Renderer subclass.

export declare abstract class Renderer { }

Copy link
Contributor Author

@ThomasBurleson ThomasBurleson Aug 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I am not sure WHAT to do here because the darn Renderer extends an abstract base class instead of implementing an interface.

Copy link
Contributor Author

@ThomasBurleson ThomasBurleson Aug 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating an internal ngClass instance, I could just copy the class implementation methods from ngClass (in core), but that seems to be a clumsy solution. Perhaps that is the only viable solution ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. don't implement Renderer
  2. Make sure that the Shape of the classes matches what Renderer expects (this should be noop since you implemented it.)
  3. When passing it into NgClass just cast it to any so that the compiler is happy.

NgClass and NgStyle use Renderer1 instances; which has been deprecated.
Instead use Renderer2 and a RendererAdapter.

Thx @mhevery for his solution!

Refs #386.
@ThomasBurleson
Copy link
Contributor Author

@mhevery - Imports of Renderer are now gone. Your last recommendation was ridiculous-easy:

export class RendererAdapter { }

// ...

if ( !this._ngClassInstance ) {
    let adapter = new RendererAdapter(_renderer);
    this._ngClassInstance = new NgClass(_iterableDiffers, _keyValueDiffers, _ngEl, <any> adapter);
}

I was 'locked' into classic OOP and did not even try this ^ myself.

@mhevery
Copy link

mhevery commented Sep 1, 2017

Perfect LGTM

@0x-r4bbit
Copy link

ping me when this one is rebased

@jelbourn Ping! :)

Would be cool if we could get this in and release as beta.10 as this is a blocker right now to upgrade from Angular 5.0.0-beta.4.

@tinayuangao tinayuangao merged commit 816d85a into master Sep 7, 2017
@ThomasBurleson ThomasBurleson deleted the thomas/fix-renderer2 branch September 13, 2017 22:12
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes pr: lgtm This PR has been approved by the reviewer pr: merge ready This PR is ready for the caretaker to presubmit and merge pr: needs presubmit (2nd)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ClassDirective throws renderer error with Angular 5.0.0-beta.5
7 participants