Knockout: Knockout foreach scope binding

Created on 23 May 2018  路  8Comments  路  Source: knockout/knockout

Hi,

I've been testing knockout 3.5.0-rc and I've noticed that the foreach binding behaviour has changed.

I've created a sample trying to replicate something I'll do with the 3.4.x version hoping that you can help me understand how we should deal with these cases from now on.

Since the context is no longer bound to the item in the array, I need to manually bind functions inside the foreach with the proper arguments. However, I'm not sure if this is the appropriate solution.

Best regards

documentation

Most helpful comment

You've made some good points. I'll take another look at how we present this option.

All 8 comments

I initially went with click: () => removeUser(user), but ultimately refactored to avoid using foreach as entirely as I needed the mouse events to apply ripples.

I'll make sure to explain this better in the release notes.

If you want the previous behavior, you can set a global option:

ko.options.createChildContextWithAs = true

Hi @avickers,

Thanks for your input. I might follow that approach 馃憤.


Hi @mbest,

Sorry, I actually saw that option in the release notes. Is there any drawback with that approach? What's the reasoning behind the behaviour change?

Thanks

This is breaking applications of mine as well.

I think this is more serious than a documentation issue. As I discuss in https://github.com/knockout/knockout/pull/907#issuecomment-392603922 I suggest

  • change ko.options.createChildContextWithAs default value to true (or better, remove ko.options.createChildContextWithAs altogether)
  • introduce another option, perhaps named createChildContext (default true) or noChildContext (default false), to allow specifying the behavior on an individual basis, for each usage of foreach

Hi @fastfasterfastest,

Thanks for your input. I also agree that is more serious than a documentation issue. I don't mind having breaking changes provided they are explicit in the release notes. However, in the past, knockout has been pretty cautious about breaking changes (e.g. knockout deferred updates are false by default) which enforces that this kind of changes should be very explicit.

I would like to know which are the consequences of enabling ko.options.createChildContextWithAs since I believe this comes with a performance hit.

You've made some good points. I'll take another look at how we present this option.

I've finally gotten some time to work on this. Please take a look at #2403.

Hi Michael,

Sorry for the late reply.

Thanks for fixing this issue! The merged PR seems good to me 馃憤 I'll give it a try as soon as we have a new release.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

craxal picture craxal  路  7Comments

while0pass picture while0pass  路  9Comments

priyank-eschool picture priyank-eschool  路  7Comments

IPWright83 picture IPWright83  路  7Comments

nkosi23 picture nkosi23  路  5Comments