var employees = [{'name': 'John', 'id': 123, 'salary': 1000}, {'name': 'Jack', 'id': 234, 'salary': 1100}] var employeesModel = new UI.ArrayListModel(employees, 'name', 'id'); assert(employeesModel.getLabel(0) == 'John'); assert(employeesModel.getValue(1) == 234); assert(employeesModel.getLength() == 2); assert(employeesModel.objects == employees); employees.push({'name': 'The new employee', 'id': 1500, 'salary': 500}); //You must tell the model that the array has changed employeesModel.changed()
The UI.ListModel 'interface' is used by widgets that shows list of things, such as a list of options.
A ListModel can be seen as two parallel arrays containg labels and values respectively. The API is pretty straightforward to implement and is detailed on 'Class UI.ListModel'. The interesting part comes with the provided implementations.
UI.ArrayListModel is a generic implementation that works for some common cases:
UI.AsyncArrayListModel inherits from ArrayListModel but enables you to get the array contents asynchronously, giving you the power of AJAX.
UI.HTMLSelectListModel is a workaround made to make the life easy to the users of mochiref:Form::UI.SelectFormInput who don't wants to make a Javascript model for their select and likes to specifiy the select options as plain HTML when declaring the form.
Fired by the list model when it changes. Not actually fired by this class, derived classes must take care of the signaling.
UI.ListModel.prototype.getLabel(n):
Returns the n-th label in the model. Derived classes must override this method.
UI.ListModel.prototype.getValue(n):
Returns the n-th value in the model. Derived classes must override this method.
UI.ListModel.prototype.getLength():
Returns the model length. Derived classes must override this moethod.
UI.ArrayListModel is a implementation of ListModel, useful for making list models from simple objects arrays.
Inherits from UI.ListModel.
This class doesn't keep observing the array looking for changes, so the :mochiref:UI.ArrayListModel.prototype.changed` must be manually called when the array changes.
UI.ArrayListModel(objects, labelField=null, valueField=null)
Constructs a ArrayListModel for the array objects. labelField and valueField are the default values for the UI.ArrayListModel.prototype.labelField and UI.ArrayListModel.prototype.valueField properties.
If labelField is not null, the labelField property of each object in the array will be used as the label values. Otherwise, labels are the toString() of each object in the array.
If valueField is
UI.ArrayListModel.prototype.objects:
The object array used by the model. If you change this property or mutate the array, make sure to invoke the :mochiref:UI.ArrayListModel.prototype.changed` method ASAP.
The field used to get the model labels. If not null, the labelField property of each object in the UI.ArrayListModel.prototype.objects array will be used as the label values. Otherwise, labels are the toString() of each object in the array. If you change this property make sure to invoke the :mochiref:UI.ArrayListModel.prototype.changed` method ASAP.
The field used to get the model values. If not null, the valueField property of each object in the array will be used as the model values. Otherwise, values will be the same as labels. If you change this property, make sure to invoke the :mochiref:UI.ArrayListModel.prototype.changed` method ASAP.
:mochidef:UI.ArrayListModel.prototype.getLabel(n)`:
Returns the n-th label in the model.
:mochidef:UI.ArrayListModel.prototype.getValue(n)`:
Returns the n-th value in the model.
:mochidef:UI.ArrayListModel.prototype.getLength(n)`:
Returns the model length.
:mochidef:UI.ArrayListModel.prototype.changed()`:
Notify the model that the array backing it has changed.
Emits the changed signal.
Inherits from UI.ArrayListModel. Adds AJAX capabilities to array list models, enabling them to retrieve the backing array from a XMLHTTP request that returs a JSON array.
Example use:
var model = new UI.AsyncArrayListModel('labelField', 'valueField'); model.retrieveData('my/url?' + queryString(['param1', 'param2'], ['value1', 'value2']));
UI.AsyncArrayListModel(labelField, valueField)
Constructs an asynchronous array list model, where labelField and valueField are the default values for the UI.ArrayListModel.prototype.labelField and UI.ArrayListModel.prototype.valueField properties.
UI.AsyncArrayListModel.prototype.retrieveData(url, method='GET').
Retrieves the array using a XMLHTTP request to the specified url. The response must be a JSON array.
By now, the only possible value for method is GET.
Returns a Deferred as returned by loadJSONDoc(url)
This class implements a ListModel backed by the option elements of a <select>. This implementation never emits the :mochiref:`changed` signal.
Inherits from UI.ListModel.
UI.HTMLSelectListModel(selectElement):
Constructs a HTMLSelectListModel using the selectElement <select> dom element
UI.ArrayListModel.prototype.getLabel(n):
Returns the n-th label in the model.
UI.ArrayListModel.prototype.getValue(n):
Returns the n-th value in the model.
UI.ArrayListModel.prototype.getLength():
Returns the model length.
Copyright 2005-2006 Leonardo Soto M. <leo.soto@gmail.com> and Imagemaker IT <http://www.imagemaker.cl>.
This program is licensed under the CDDL v1.0 license, see http://www.sun.com/cddl/cddl.html.