{"action":"created","issue":{"url":"https://api.github.com/repos/lhorie/mithril.js/issues/683","labels_url":"https://api.github.com/repos/lhorie/mithril.js/issues/683/labels{/name}","comments_url":"https://api.github.com/repos/lhorie/mithril.js/issues/683/comments","events_url":"https://api.github.com/repos/lhorie/mithril.js/issues/683/events","html_url":"https://github.com/lhorie/mithril.js/issues/683","id":89707248,"number":683,"title":"Subcomponent Controller access to latest attributes","user":{"login":"mindeavor","id":17013,"avatar_url":"https://avatars.githubusercontent.com/u/17013?v=3","gravatar_id":"","url":"https://api.github.com/users/mindeavor","html_url":"https://github.com/mindeavor","followers_url":"https://api.github.com/users/mindeavor/followers","following_url":"https://api.github.com/users/mindeavor/following{/other_user}","gists_url":"https://api.github.com/users/mindeavor/gists{/gist_id}","starred_url":"https://api.github.com/users/mindeavor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mindeavor/subscriptions","organizations_url":"https://api.github.com/users/mindeavor/orgs","repos_url":"https://api.github.com/users/mindeavor/repos","events_url":"https://api.github.com/users/mindeavor/events{/privacy}","received_events_url":"https://api.github.com/users/mindeavor/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":23,"created_at":"2015-06-20T02:58:25Z","updated_at":"2015-06-23T18:49:43Z","closed_at":null,"body":"Take the following code:\r\n\r\n```javascript\r\nvar ItemManager = {\r\n  controller: function (attrs) {\r\n    this.createItem = function() {\r\n      attrs.items.push( new Item() )\r\n    }\r\n  },\r\n  view: function (ctrl, attrs) {\r\n    return m('.items', [\r\n      m('button', { onclick: ctrl.createItem }, \"Create Item\"),\r\n      attrs.items.map(...)\r\n    ])\r\n  }\r\n}\r\n```\r\n\r\nI have too often come across the limitation of `ItemManager`'s controller only having access to the first version of `attrs`. For example:\r\n\r\n1. The parent of `ItemManager` passes in an array of items:\r\n  a. `m.component(ItemManager, { items: itemArray })`\r\n2. Everything works great. Creating an item in `ItemManager` adds to `itemArray`.\r\n3. Later, the parent of `ItemManager` generates **a new value for itemArray**, and passes that value to the same `ItemManager` subcomponent\r\n4. Things break. Due to the controller's closure to `attrs`, creating a new item will now add to **the old itemArray**. The new itemArray remains unaffected.\r\n\r\nThis is a hard issue to explain, and the workarounds are non-obvious. As it stands, trying to modify the `attrs` parameter in the controller constructor breaks the principle of least surprise.\r\n\r\nI think Mithril should have some built-in way to allow the controller to have access to the latest `attrs`. I have some options in mind, but before I present them I want to make sure the problem is clear. Thoughts?"},"comment":{"url":"https://api.github.com/repos/lhorie/mithril.js/issues/comments/114605869","html_url":"https://github.com/lhorie/mithril.js/issues/683#issuecomment-114605869","issue_url":"https://api.github.com/repos/lhorie/mithril.js/issues/683","id":114605869,"user":{"login":"lhorie","id":1637573,"avatar_url":"https://avatars.githubusercontent.com/u/1637573?v=3","gravatar_id":"","url":"https://api.github.com/users/lhorie","html_url":"https://github.com/lhorie","followers_url":"https://api.github.com/users/lhorie/followers","following_url":"https://api.github.com/users/lhorie/following{/other_user}","gists_url":"https://api.github.com/users/lhorie/gists{/gist_id}","starred_url":"https://api.github.com/users/lhorie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lhorie/subscriptions","organizations_url":"https://api.github.com/users/lhorie/orgs","repos_url":"https://api.github.com/users/lhorie/repos","events_url":"https://api.github.com/users/lhorie/events{/privacy}","received_events_url":"https://api.github.com/users/lhorie/received_events","type":"User","site_admin":false},"created_at":"2015-06-23T18:49:43Z","updated_at":"2015-06-23T18:49:43Z","body":"I could add a section on the docs talking about the perils of consuming controller arguments (thereby creating a stateful component) when you really meant to write a stateless component. The docs about stateful/stateless components go some of the way to illustrate the difference in construction between the two, but clearly this nuance got lost in translation.\r\n\r\nI don't think there should be different conceptual entities to deal with the differences between a initialization parameter, vs a refresh parameter, and I don't think controller arguments should automagically get in sync w/ view arguments (on the grounds that it breaks common assumptions about closures and arguments in js, and makes testing ridiculously convoluted)"}}