Skip to content

Instantly share code, notes, and snippets.

@matthewp
Last active May 17, 2018 19:42
Show Gist options
  • Save matthewp/ea856154e9128f4458cab91c8354564f to your computer and use it in GitHub Desktop.
Save matthewp/ea856154e9128f4458cab91c8354564f to your computer and use it in GitHub Desktop.

can-view-nodelist removal

In May 2018 I spent a few days exploring what it would take to remove can-view-nodelist from CanJS. The attempt stems from canjs/can-observation#125

The idea is to be able to automatically teardown observations that are created within templates when an outer observation updates itself. This would prevent needing to use nodelists to teardown observations.

The code for this looks like:

var stopTrap = observation.trapBindings();
canReflect.onValue(observation, function(){});
ObservationRecorder.addBindings(stopTrap());

This keeps a record of bindings within the observation record. Later, when you update an observation it will unbind all of these bindings.

Later it was discovered that something similar was needed for can-diff/patcher/patcher and can-view-scope.

The changes related to the above is in:

Important, currently can-diff contains duplicate code for what can-observation has. This should be moved into a mixin they both can use (and likely can-view-scope.

Things to do

  • Move the bindings changes into a mixin, probably within can-event-queue.
  • Update can-observation, can-diff/patcher/patcher to use this new mixin. Add the same functionality to can-view-scope.

Keeping track of nodes

The other thing that using can-view-nodelists provides is a way to keep track of nodes that are inserted within a section. Removing cann-view-nodelist from can-view-live.html presented a problem for this:

{{#foo}}
<span></span>

{{#each animals}}
  <label>{{.}}</label>
{{/each}}

{{/foo}}

The #foo live.html code doesn't know about the labels and therefore won't remove them if foo is set to undefined.

To fix this we have discussed having can-stache creating placeholders. This would give us the start and end placeholder textnodes. From that we would know to replace all of the nodes in between.

Here are the changes so far to can-view-live and can-stache:

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