Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added postInitialize #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,65 @@ var people = new AmpersandCollection([{ name: 'phil' }, { name: 'bob' }, { name:
});
```

### postInitialize `collection.postInitialize()`

If you have defined a `postInitialize` function for your subclass of Collection, it will be invoked at creation time, just after it's parent or (parent-)collection `initialize` functions has been triggerd.

```
var State = require('ampersand-state');
var Collection = require('ampersand-collection');

var Widget = State.extend({
props: {
name: 'string',
funLevel: 'number'
},
postInitialize: function() {
console.log('code to run just AFTER this.parent.initialize has been invoked');
}
});

var Hat = State.extend({
props: {
color: 'string'
},
postInitialize: function() {
console.log('code to run just AFTER this.parent.initialize has been invoked');
}
});

var WidgetCollection = Collection.extend({
model: Widget,
postInitialize: function() {
console.log('code to run just AFTER this.parent.initialize has been invoked');
}
});

var Person = AmpersandState.extend({
props: {
name: 'string'
},
children: {
hat: Hat
}
collections: {
widgets: WidgetCollection
}
});

var me = new Person({
name: 'Bob',
hat: {
color: 'red'
}
widgets: [
{ name: 'music', funLevel: 10 },
{ name: 'coding', funLevel: 10 }
]
});

```

### mainIndex `collection.mainIndex`

Specify which property the collection should use as the main index (and unique identifier) for the models/objects it holds. This is the property that [`get`](#ampersand-collection-get) uses to retrieve models, and what `add`, `set`, and `remove` uses to determine whether a collection already contains a model or not.
Expand Down
8 changes: 8 additions & 0 deletions ampersand-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ function Collection(models, options) {
this._reset();
this.initialize.apply(this, arguments);
if (models) this.reset(models, assign({silent: true}, options));
if (options.postInit !== false) {
this.forEach(function(model) {
if(model.postInitialize) model.postInitialize.apply(model);
});
this.postInitialize.apply(this);
}
}

assign(Collection.prototype, AmpersandEvents, {
initialize: function () {},

postInitialize: function () {},

isModel: function (model) {
return this.model && model instanceof this.model;
},
Expand Down
39 changes: 39 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,42 @@ test('Collection should rethrow change events on a model', function (t) {

model.name = 'shmoe';
});


test('Add a postInitialize-functionality over States and Collections, Children and subCollections', function (t) {
var result = [];
/**** Model Test *****/
var MyModel = State.extend({
props: {
val: 'number'
},
initialize: function() {
result.push(this.val);
},
postInitialize: function() {
result.push(this.val);
}
});

/**** Collection Test *****/
var MyCollection = Collection.extend({
model: MyModel,
initialize: function() {
result.push(-1);
},
postInitialize: function() {
result.push(-1);
}
});

var a = new MyCollection([{
val: 0
}, {
val: 1
}, {
val: 2
}]);
t.deepEqual(result, [ -1, 0 , 1 ,2, 0, 1, 2, -1 ]);

t.end();
});