Skip to content

Commit

Permalink
CheckBoxWidget items are better determined when they are needed
Browse files Browse the repository at this point in the history
  • Loading branch information
agroszer committed Feb 25, 2016
1 parent dd40f6f commit a698829
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Expand Up @@ -7,6 +7,8 @@ CHANGES

- RadioWidget items are better determined when they are needed [agroszer]

- CheckBoxWidget items are better determined when they are needed [agroszer]


3.2.9 (2016-02-01)
------------------
Expand Down
27 changes: 15 additions & 12 deletions src/z3c/form/browser/checkbox.py
Expand Up @@ -40,14 +40,10 @@ class CheckBoxWidget(widget.HTMLInputWidget, SequenceWidget):
def isChecked(self, term):
return term.token in self.value

def update(self):
"""See z3c.form.interfaces.IWidget."""
super(CheckBoxWidget, self).update()
widget.addFieldClass(self)
# XXX: this is to early for setup items. See select widget how this
# sould be done. Setup the items here doens't allow to override the
# widget.value in updateWidgets, ri
self.items = []
def items(self):
if self.terms is None:
return ()
items = []
for count, term in enumerate(self.terms):
checked = self.isChecked(term)
id = '%s-%i' % (self.id, count)
Expand All @@ -56,16 +52,23 @@ def update(self):
default=term.title)
else:
label = util.toUnicode(term.value)
self.items.append(
{'id':id, 'name':self.name + ':list', 'value':term.token,
'label':label, 'checked':checked})
items.append(
{'id': id, 'name': self.name + ':list', 'value': term.token,
'label': label, 'checked': checked})
return items

def update(self):
"""See z3c.form.interfaces.IWidget."""
super(CheckBoxWidget, self).update()
widget.addFieldClass(self)

def json_data(self):
data = super(CheckBoxWidget, self).json_data()
data['options'] = self.items
data['options'] = list(self.items())
data['type'] = 'check'
return data


@zope.component.adapter(zope.schema.interfaces.IField, interfaces.IFormLayer)
@zope.interface.implementer(interfaces.IFieldWidget)
def CheckBoxFieldWidget(field, request):
Expand Down
2 changes: 1 addition & 1 deletion src/z3c/form/browser/checkbox.txt
Expand Up @@ -206,7 +206,7 @@ the value (which is used as a backup label) contains non-ASCII characters:
>>> terms = SimpleVocabulary.fromValues([b'yes\012', b'no\243'])
>>> widget.terms = terms
>>> widget.update()
>>> pprint(widget.items)
>>> pprint(list(widget.items()))
[{'checked': False,
'id': 'widget-id-0',
'label': 'yes\n',
Expand Down

0 comments on commit a698829

Please sign in to comment.