Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat($compile): Allow using functions as templates in directives #1849

Closed
wants to merge 1 commit into from

Conversation

lrlopez
Copy link
Contributor

@lrlopez lrlopez commented Jan 20, 2013

Added dynamic template support in directives:

  • The call will include the directive attributes common object as a parameter
  • This allows generating templates on the fly
  • It also applies to templateUrl

Closes #1039

@lrlopez
Copy link
Contributor Author

lrlopez commented Jan 20, 2013

By the way, I already signed the CLA

@schmuli
Copy link

schmuli commented Jan 20, 2013

A simple but elegant solution!

@sudhakar
Copy link

+1. Simple and more customizable..

@olostan
Copy link
Contributor

olostan commented Jan 23, 2013

as I understand, in function of template I can't use DI?

May be there could be quite easy but very powerfull solution to have "templateExpression"/"templateUrlExpression" properties, where I can use filters.

Use case:I could use: templateUrlExpression: ' "index.html" | localizeUrl' that will transform path using current user locale and get localized template.

So this simple solution (that can use $interpolate service) will give power of DI

@lrlopez
Copy link
Contributor Author

lrlopez commented Jan 23, 2013

Hmmm... I don't know if that is feasible without touching many lines of code.

When using functions as templates, they are called before all the dependencies of the directive get loaded. In order to fix it, we would need to defer the function call after all the other dependencies load.

Let me think if I can figure out how to do it as cleanly as possible...

@schmuli
Copy link

schmuli commented Jan 24, 2013

Why do you need DI for the template/templateUrl function? You can use DI for the directive declaration function, which should cover any needs for the template function.

module.directive('directiveName', ['dependency', function (dependency) {
    return {
        template: function(attrs) {
            // use 'dependency'
            return 'template';
        },
        ....
    };
};

@lrlopez
Copy link
Contributor Author

lrlopez commented Jan 24, 2013

@schmuli is right... that should work as expected.

I got confused with my other PR (#1524) that applied the same feature to routes. Dependencies in the route controller solve after the template function is called. I mixed up, sorry :)

@ghost ghost assigned IgorMinar Feb 15, 2013
@IgorMinar
Copy link
Contributor

at first glance I like it. I'll sleep on it and will likely merge it tomorrow.

@lrlopez
Copy link
Contributor Author

lrlopez commented Feb 21, 2013

@mhevery: I've just updated my local repository in order to pass "$compileNode" into the call as you suggested.

@IgorMinar, I can't think of a use-case where it would be advantageous to use an asynchronous templateUrl function call. May be I'm missing something...

Just to be sure, I intend to follow these steps now:

  1. Commit the new changes.
  2. Squash the original and new commits into one.
  3. Rebase with latest master.
  4. Force a push operation with -f so the branch will be replaced with my local one.

Am I right? I fear that this could disrupt source code line comments, but I think that could ease the integration of the PR.

@lrlopez
Copy link
Contributor Author

lrlopez commented Feb 23, 2013

I've just forced the push with the new changes. The original comments from Miško and Igor are still available here:
lrlopez@d40bc9f#L1R599

@IgorMinar
Copy link
Contributor

I'll take a look tonight. Thanks.

@IgorMinar
Copy link
Contributor

PR Checklist (Minor Feature)

  • Contributor signed CLA now or in the past (if you just signed, leave a comment here with your real name for cross reference)
  • Feature improves existing core functionality
  • API is compatible with existing Angular apis and relevant standards (if applicable)
  • PR doesn't contain a breaking change
  • PR contains unit tests
  • PR contains e2e tests (if suitable)
  • PR contains documentation update
  • PR passes all tests on Travis (sanity)
  • PR passes all tests on ci.angularjs.org (cross-browser compatibility)
  • PR is rebased against recent master
  • PR is squashed into one commit per logical change
  • PR's commit messages are descriptive and allows us to autogenerate release notes (required commit message format)
  • All changes requested in review have been implemented

@IgorMinar
Copy link
Contributor

please rebase against latest master (there is a conflict)

@lrlopez
Copy link
Contributor Author

lrlopez commented Feb 26, 2013

Specs have been reworked and the commit has been rebased. Old comments are still available at lrlopez@1de1f85#test-ng-compilespec-js-P8

Added dynamic template support in directives:
- The call will pass the element and directive attributes as parameters
- This allows generating templates on the fly
- It also applies to templateUrl
@lrlopez
Copy link
Contributor Author

lrlopez commented Feb 26, 2013

Oops, I pushed the wrong branch. I hope it works now :)

@IgorMinar
Copy link
Contributor

I reworked the docs and tests and landed the change as eb53423

in the future please try to write focused tests - tests that test only the functionality you are adding without any other distractions.

thanks for the contribution, I'm quite happy about this feature!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dynamic templateUrl in directives
5 participants