HipsterCollection class
class HipsterCollection implements Collection {
CollectionEvents on;
List<HipsterModel> models;
Map<String,Map> data;
HipsterCollection() {
on = new CollectionEvents();
models = <HipsterModel>[];
}
HipsterModel modelMaker(attrs);
String get url;
// Be List-like
void forEach(fn) {
models.forEach(fn);
}
iterator() => models.iterator();
bool get isEmpty => models.isEmpty;
map(fn) => models.map(fn);
filter(fn) => models.filter(fn);
contains(element) => models.contains(element);
reduce(intialValue, fn) => models.reduce(initialValue, fn);
every(fn) => models.every(fn);
some(fn) => models.some(fn);
int get length => models.length;
// Be Backbone like
operator [](id) {
var ret;
forEach((model) {
if (model['id'] == id) ret = model;
});
return ret;
}
fetch() {
HipsterSync.
call('read', this).
then((list) {
list.forEach((attrs) {
models.add(_buildModel(attrs));
});
on.load.dispatch(new CollectionEvent('load', this));
});
}
create(attrs) {
Future after_save = _buildModel(attrs).save();
after_save.
then((saved_model) {
this.add(saved_model);
});
after_save.
handleException(bool (e) {
print("Exception handled: ${e}");
return true;
});
}
add(model) {
models.add(model);
on.
insert.
dispatch(new CollectionEvent('add', this, model:model));
}
_buildModel(attrs) {
var new_model = modelMaker(attrs);
// Give the factory a chance to define attributes on the model, if it does
// not, explicitly set them.
if (new_model.attributes.isEmpty) new_model.attributes = attrs;
new_model.collection = this;
return new_model;
}
}
Implements
Constructors
new HipsterCollection() #
HipsterCollection() {
on = new CollectionEvents();
models = <HipsterModel>[];
}
Properties
Map<String, Map> data #
Map<String,Map> data;
final bool isEmpty #
Returns true if there is no element in this collection.
bool get isEmpty => models.isEmpty;
final int length #
Returns the number of elements in this collection.
int get length => models.length;
List<HipsterModel> models #
List<HipsterModel> models;
CollectionEvents on #
CollectionEvents on;
final String url #
String get url;
Operators
operator [](id) #
operator [](id) {
var ret;
forEach((model) {
if (model['id'] == id) ret = model;
});
return ret;
}
Methods
add(model) #
add(model) {
models.add(model);
on.
insert.
dispatch(new CollectionEvent('add', this, model:model));
}
contains(element) #
Check whether the collection contains an element equal to element.
contains(element) => models.contains(element);
create(attrs) #
create(attrs) {
Future after_save = _buildModel(attrs).save();
after_save.
then((saved_model) {
this.add(saved_model);
});
after_save.
handleException(bool (e) {
print("Exception handled: ${e}");
return true;
});
}
every(fn) #
Returns true if every elements of this collection satisify the
predicate f. Returns false otherwise.
every(fn) => models.every(fn);
fetch() #
fetch() {
HipsterSync.
call('read', this).
then((list) {
list.forEach((attrs) {
models.add(_buildModel(attrs));
});
on.load.dispatch(new CollectionEvent('load', this));
});
}
filter(fn) #
Returns a collection with the elements of this collection
that satisfy the predicate f.
The returned collection should be of the same type as the collection creating it.
An element satisfies the predicate f if f(element)
returns true.
filter(fn) => models.filter(fn);
void forEach(fn) #
Applies the function f to each element of this collection.
void forEach(fn) {
models.forEach(fn);
}
iterator() #
Returns an Iterator that iterates over this Iterable object.
iterator() => models.iterator();
map(fn) #
Returns a new collection with the elements f(e)
for each element e of this collection.
Subclasses of Collection should implement the map method
to return a collection of the same general type as themselves.
E.g., List.map should return a List.
map(fn) => models.map(fn);
HipsterModel modelMaker(attrs) #
HipsterModel modelMaker(attrs);
reduce(intialValue, fn) #
Reduce a collection to a single value by iteratively combining each element
of the collection with an existing value using the provided function.
Use initialValue as the initial value, and the function combine to
create a new value from the previous one and an element.
Example of calculating the sum of a collection:
collection.reduce(0, (prev, element) => prev + element);
reduce(intialValue, fn) => models.reduce(initialValue, fn);