Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CanJS 4.0 migration guide #3781

Closed
JaneOri opened this issue Dec 4, 2017 · 30 comments
Closed

CanJS 4.0 migration guide #3781

JaneOri opened this issue Dec 4, 2017 · 30 comments
Assignees

Comments

@JaneOri
Copy link

JaneOri commented Dec 4, 2017

Changes to watch for

Stache:

  • %event and other special symbols are now on scope
    • for example, %event is now scope.event
  • can-view-scope doesn't do implicit scope walking, so change to use ../ and whatnot. Or use {{scope.find('foo')}}

Routes:

  • can.route(":page", { page: "home" }); is now can.route("{page}", { page: "home" });
  • can.route.ready(); is now can.route.start();
  • can.route() is now can.route.register()

Packages

  • can-stache/helpers/route is now can-stache-route-helpers

Code/ViewModels

  • can.batch.start() is now can.queues.batch.start()
  • can.batch.stop() is now can.queues.batch.stop()
@matthewp
Copy link
Contributor

matthewp commented Dec 4, 2017

can-stache/helpers/route is now can-stache-route-helpers.

@matthewp
Copy link
Contributor

matthewp commented Dec 4, 2017

can-view-scope doesn't do implicit scope walking, so change to use ../ and whatnot. Or use {{scope.find('foo')}}

@chasenlehara chasenlehara changed the title Migration to 4.0 notes CanJS 4.0 migration guide Dec 8, 2017
@phillipskevin
Copy link
Collaborator

The .each method has been removed from can-define/map/map, can-define/list/list, can-map, and can-list.

You should use .forEach or canReflect.each instead.

@chasenlehara
Copy link
Member

can-define uses can.keys as the event name for when keys are dispatched instead of __keys

@phillipskevin
Copy link
Collaborator

phillipskevin commented Dec 13, 2017

stache's log helper is now console.log (the other console methods are available also). It will not be passed the current context by default. You should use console.log(this) if you want that.

@phillipskevin
Copy link
Collaborator

phillipskevin commented Dec 14, 2017

These will no longer automatically call bar as a function:

{{bar}}
{{#bar}}
  ...
{{else}}
  ...
{{/bar}}

You need to now call them manually:

{{bar()}}
{{#bar()}}
  ...
{{else}}
  ...
{{/bar}}

Also, functions in bindings are not automatically called... so you need to call them if you're doing
on:click="foo" or on:click="foo.bar.baz()".

@chasenlehara
Copy link
Member

chasenlehara commented Dec 26, 2017

  • can-route-pushstate should now allow the browser to open a new tab/window when the user is holding down a modifier key (e.g. control, command, etc.)

  • Changing a component element’s attribute will not update the viewmodel property; see can-component’s ViewModel “Using attribute values” docs for more details.

  • The default ViewModel type has changed; if you were relying on it being a specific type, it’s changed.

  • No need for @ when importing or exporting functions in stache.

  • route.param no longer accepts route as a property on the object passed to it [this was an undocumented API]

@phillipskevin
Copy link
Collaborator

inserted and removed events on components should be moved to connectedCallback and disconnectedCallbacks on the component's ViewModel.

Or you should run this to make inserted and removed work:

import domEvents from "can-dom-events";
import domMutateDomEvents from "can-dom-mutate/dom-events";

domEvents.addEvent(domMutateDomEvents.inserted);
domEvents.addEvent(domMutateDomEvents.removed);

@matthewp
Copy link
Contributor

matthewp commented Jan 4, 2018

Should we create can-dom-mutate-events-legacy or something that does the above?

@andrejewski
Copy link
Contributor

@matthewp I would prefer a can-3-4-compat package that does more than add two events if there is anything else we could restore.

@matthewp
Copy link
Contributor

matthewp commented Jan 4, 2018

The downside to that is it would be harder to remove the compat package if you depend on several of its internal behaviors.

@andrejewski
Copy link
Contributor

We could have can-3-4-compat/dom-mutation-events be a module that is imported by can-3-4-compat/index and what not, allowing people more granularity without scattering legacy behavior across entire packages.

@matthewp
Copy link
Contributor

matthewp commented Jan 4, 2018

I would favor not allowing importing can-3-4-compat and forcing people to import the individual things they need. Meaning you have to import can-3-4-compat/dom-mutation-events

@phillipskevin
Copy link
Collaborator

The default helper can no longer be called as function.

@justinbmeyer
Copy link
Contributor

@phillipskevin is it possible to make {{default()}} still work?

@justinbmeyer
Copy link
Contributor

DefineMap's value behavior should now be called default.

@matthewp
Copy link
Contributor

matthewp commented Jan 17, 2018

can.route.deparam(routeString) no longer includes a .route property when there is a matching route. Use !!route.rule(routeString) instead.

JUSTIN NOTE: I'd use route.rule(...) !== undefined

@matthewp
Copy link
Contributor

can-view-model no longer uses domData to get an element's viewModel. Instead it uses the can.viewModel symbol.

@matthewp matthewp self-assigned this Jan 22, 2018
@matthewp
Copy link
Contributor

Are there any uses cases for keeping can-event in a project @justinbmeyer @phillipskevin? I think everything it did is covered by can-queues.batch and can-event-queue, right?

@matthewp
Copy link
Contributor

@chasenlehara @phillipskevin @m-mujica @justinbmeyer Here is the first version of the migration guide: https://canjs.github.io/next/doc/migrate-4.html . You can leave feedback here and I'll update in a PR.

@phillipskevin
Copy link
Collaborator

Some comments from reviewing the guide:

  1. We might want to mention that not all ecosystem packages have been updated to 4.0. Especially if using the can package, it might not be super easy to tell if everything you're using has a 4.0 version. Maybe we have people check the site for the packages they're using. Or we could give a list of what has not been updated.

  2. In the inserted/removed event section, this sentence reads a little strange:

Most if what was done in the inserted event can be replaced in places like value.

  1. The connectedCallback is only on the ViewModel (pretty sure).
Component.extend({
    connectedCallback: function(){

should be

Component.extend({
    ViewModel: {
        connectedCallback: function(){
        }
    }
  1. I think this is a typo:

In can-define 1.0, you would default a default value for a property

@justinbmeyer
Copy link
Contributor

I have been thinking if we should call connected callback on both the component and the VM. Sometimes I feel like it makes more sense on the component.

@justinbmeyer
Copy link
Contributor

Unlike previous versions of CanJS, in CanJS 4 there are a mixture of different package versions. This is because CanJS is now developed as separate independent packages.

Was this not true for 3.0? can-define was 1.0 for can@3.0.

Use can-queues instead of can-event

can-queues replaces most closely can-event/batch/batch.

If you were using can-event, then you likely want to use can-event-queue/map/map.

I actually think can-event-queue/map/map is the best thing to use at first. It includes .start() and .stop() and other old can-event/batch/batch methods:

https://github.com/canjs/can-event-queue/blob/master/map/map.js#L821

These warn, but fix those later ...

Implicit scope walking

I think this section should tell people that upgrading to the latest 3.0 will warn them of where they are using implicit scope walking.

@matthewp
Copy link
Contributor

@justinbmeyer

Was this not true for 3.0? can-define was 1.0 for can@3.0.

No, can-define was not part of CanJS 2.3, so it wasn't part of the migration guide.

matthewp added a commit that referenced this issue Jan 25, 2018
This upgrades the migration guide based on feedback provided in
#3781.

Also adds a section on the old stache symbols such as `%event`.
@phillipskevin
Copy link
Collaborator

@matthewp there are a few things missing from the migration guide:

Do you want me to add these? Or do you want to take care of it?

@matthewp
Copy link
Contributor

I'll get it, thanks for the notes!

@matthewp
Copy link
Contributor

@phillipskevin what was the reason for .each being dropped? Was it that it conflicted with something else?

@phillipskevin
Copy link
Collaborator

stache lookups will now check the context before checking helpers, so {{#each(list)}} would have called list.each instead of the each helper.

@phillipskevin
Copy link
Collaborator

About #3781 (comment) - I'm going to try to get {{#default()}} working today so we don't have to explain/codemod this.

@matthewp
Copy link
Contributor

These are all added to https://canjs.github.io/next/doc/migrate-4.html

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

No branches or pull requests

6 participants