-
-
Notifications
You must be signed in to change notification settings - Fork 928
Closed
Description
Take the following code:
var ItemManager = {
controller: function (attrs) {
this.createItem = function() {
attrs.items.push( new Item() )
}
},
view: function (ctrl, attrs) {
return m('.items', [
m('button', { onclick: ctrl.createItem }, "Create Item"),
attrs.items.map(...)
])
}
}I have too often come across the limitation of ItemManager's controller only having access to the first version of attrs. For example:
- The parent of
ItemManagerpasses in an array of items:
a.m.component(ItemManager, { items: itemArray }) - Everything works great. Creating an item in
ItemManageradds toitemArray. - Later, the parent of
ItemManagergenerates a new value for itemArray, and passes that value to the sameItemManagersubcomponent - 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.
This 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.
I 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?
Metadata
Metadata
Assignees
Labels
No labels